Refresh the browser after nginx deploys the front-end project and report an error 404

Problem: After the Vue project is packaged and deployed on the Nginx server, a 404 problem occurs after refreshing the page.

Reason: After the single-page application is loaded, the routing changes are handled by the browser, and the current link will be requested when refreshing, but Nginx cannot find the corresponding page.

Solution: Add the following configuration to the Nginx configuration file nginx.conf: 

try_files $uri $uri/ /index.html;

# Variable explanation
try_files fixed syntax
$uri refers to the home file (the path after the ip address, if it is 127.0.0.1/index/a.png, then refers to index/a.png)
$uri/ refers to the home folder
/ index.html Initiate a request to the ip/index.html address
 
try_files $uri $uri/ /index.html;
try to parse the following 2 files/folders (automatically distinguish whether the path behind the IP is a file or a folder), $uri /$uri/,
if resolved, return the first one,
if not resolved, initiate a request jump to 127.0.0.1/index.html (the route must be real, otherwise an error will be reported)


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


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8100;
        server_name  localhost;
        charset utf-8;
        location / {
            root   D:/deploy/web/http;
            index  index.html index.htm;
        }
    }   

     server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        location / {
            root   D:/deploy/gn/http;
            index  index.html index.htm;
            try_files $uri $uri/ /index.html;
        }
    }  

}

Guess you like

Origin blog.csdn.net/weixin_39709134/article/details/128298268