Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在nginx中刷新后响应项目给出错误_Reactjs_Nginx_React Router_Nginx Config - Fatal编程技术网

Reactjs 在nginx中刷新后响应项目给出错误

Reactjs 在nginx中刷新后响应项目给出错误,reactjs,nginx,react-router,nginx-config,Reactjs,Nginx,React Router,Nginx Config,我已经在nginx中设置了wordpress(/)和react build文件夹(/map)。conf文件如下所示 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 d

我已经在nginx中设置了wordpress(/)和react build文件夹(/map)。conf文件如下所示

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;
    }

}
当我正常使用时,应用程序运行良好。但当我刷新地图中的子页面时 例如,
/map/explore towns
,我在Wordpress页面上看到404未找到


这里可能有什么问题?

因为react应用程序的路由是客户端路由。如果直接转到
/map/something
,nginx将尝试将其重定向到属于WP的
/index.php
。所以它将抛出404未找到

要修复它,您需要配置nginx,将
/map
的每个请求重定向到
/map/index.html
。然后,react应用程序将按预期工作

也许此配置将有助于:

// add below your `location / {}`
location /map {
 try_files $uri /index.html;
}