0

I have set up of wordpress (/) and react build folder(/map) as setup in nginx. the conf file looks like this

upstream index_php_upstream {
    server 127.0.0.1:8090;
}

upstream direct_php_upstream {
    server 127.0.0.1:8091;
}
upstream react_point {
server 127.0.0.1:3000;
}
server {
    listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;
        server_name _;
    root        /var/www/html/wordpress/public_html;
index index.html index.php;
# log files
access_log /var/log/nginx/sample.com.access.log;
error_log /var/log/nginx/sample.com.error.log;

location = /favicon.ico {
log_not_found off;
access_log off;
}

location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}

location / {
try_files $uri $uri/ /index.php?$args;
}

location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)$ {
expires max;
log_not_found off;
}
   location ^~ /map {
       alias /opt/urbanwater/build/;
index index.html index.html;
       try_files $uri $uri/ /map/index.html?$args =404;
    }

}

the application is working fine when I use It normally. but when I refresh the sub page in map for e.g. /map/explore-towns it is taking me to Wordpress page saying 404 not found.

what might be the problem here?

krishna lodha
  • 381
  • 3
  • 9

1 Answers1

1

Because routing for react app is client route. If you go directly to /map/some-thing, nginx will try to redirect it to /index.php which belongs to WP. So it'll throw 404 not found.

To fix it, you need to config your nginx, redirect every request of /map to /map/index.html. Then, react app will work as expected.

Maybe this config will help:

// add below your `location / {}`
location /map {
 try_files $uri /index.html;
}
Thành Trang
  • 591
  • 2
  • 5