问题描述:
使用Nginx部署后,登录页面刷新一下就出来404,如下图:
刷新以后 ,页面变成404 Not Found
解决方案:
查看了一下nginx配置,出现问题的配置是这样的:
server {
listen 8088;
server_name localhost;
location / {
root html/dist;
index index.html index.htm;
}
location /gateway/ {
rewrite ^/gateway/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5;
proxy_pass http://192.168.0.11:9000/;
}
修改后的配置是这样的
server {
listen 8088;
server_name localhost;
location / {
root html/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /gateway/ {
rewrite ^/gateway/(.*) /$1 break;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_connect_timeout 5;
proxy_pass http://192.168.0.11:9000/;
}
添加了try_files $uri $uri/ /index.html,
然后重启一下nginx问题就解决了。
解释:
- try_files 表示检查文件是否存在,返回第一个找到的文件,这里设置是index.html内部重定向。
另外,还有一种404报错的问题,可能是nginx访问文件权限问题,
打开nginx.conf,第一行默认是这样的
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
以下省略
可能是你访问的路径,需要root权限,而你启动nginx使用的普通用户,权限不足导致访问不到文件,所以可以这么修改:
user root;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
以下省略