【发布时间】:2021-04-01 05:32:43
【问题描述】:
我目前正在使用 react 和 react 路由器构建一个应用程序,该应用程序在由 nginx 和 docker 提供服务时运行良好,但是一旦我重新加载或复制/粘贴 url,nginx 会给出 404 错误,因为配置无法访问我指定的位置并呈现错误页面,而不是 500。 此外,我的整个应用程序在一个子域下运行,这使得我看到并尝试过的一些答案不起作用,因为我似乎错过了正确告诉 nginx 在哪里可以找到我的 index.html 在相应目录中的位置。
现在我的配置文件如下所示:
nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
默认在网站内启用
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ index.html =404;
}
location = /example1/example2/ {
try_files $uri $uri/ index.html =404;
}
}
我也在使用 browserHistory 和我的 react 路由器
【问题讨论】:
标签: reactjs nginx react-router