問題描述:
使用Nginx部署后,登錄頁面刷新一下就出來404,如下圖:
刷新以后 ,頁面變成404 Not Found
解決方案:
查看了一下nginx配置,出現(xiàn)問題的配置是這樣的:
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 表示檢查文件是否存在,返回第一個找到的文件,這里設(shè)置是index.html內(nèi)部重定向。
另外,還有一種404報錯的問題,可能是nginx訪問文件權(quán)限問題,
打開nginx.conf,第一行默認是這樣的文章來源:http://www.zghlxwxcb.cn/news/detail-544200.html
#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權(quán)限,而你啟動nginx使用的普通用戶,權(quán)限不足導(dǎo)致訪問不到文件,所以可以這么修改:文章來源地址http://www.zghlxwxcb.cn/news/detail-544200.html
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;
}
以下省略
到了這里,關(guān)于前端部署nginx刷新后404,解決Nginx刷新頁面后404的問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!