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