Nginx解决前端history路由模式下页面刷新404

发布时间 2023-09-04 15:42:08作者: 邹姣姣

原配置:

server {
    listen 80;
    server_name localhost;

    gzip on;
    gzip_static on;     # 需要http_gzip_static_module 模块
    gzip_min_length 1k;
    gzip_comp_level 4;
    gzip_proxied any;
    gzip_types text/plain text/xml text/css;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
    client_max_body_size 100m;
        root /data/; 
}

问题:页面刷新404

解决办法:

server {
    listen 80;
    server_name localhost;

    gzip on;
    gzip_static on;     # 需要http_gzip_static_module 模块
    gzip_min_length 1k;
    gzip_comp_level 4;
    gzip_proxied any;
    gzip_types text/plain text/xml text/css;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    
    client_max_body_size 100m;

    # 解决history路由模式404的配置
    location / {
        root /data/; 
        index index.html index.htm; 
        try_files $uri $uri/ /index.html; 
    }
}