刷新页面后,JS单页面站点上出现404错误

时间:2020-04-10 13:59:57

标签: javascript nginx http-status-code-404

我有nginx和JS单页站点。 刷新页面“ mysite.com/user”后出现404错误。首次使用时页面工作正常,但页面刷新后nginx返回404错误。

[error] 30149#0: *45 open() "/var/www/users" failed (2: No such file or directory), client: , server: mysite.com, request: "GET /users HTTP/1.1", host: "mysite.com"`

请帮助。

网站配置

server {
    listen       80;
    server_name  mysite.com;
    root    /var/www/;

    location / {
        try_files $uri $uri/ /index.html;
 if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '$http_origin';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';

        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '$http_origin';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '$http_origin';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
    }

    location /static/ {
        add_header Cache-Control max-age=31536000;
    }

    location /index.html {
        add_header Cache-Control no-cache;
    }

    location /config.json {
        add_header Cache-Control no-cache;
    }

    location /api {
        proxy_pass http://backend;
    }


    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

我不明白为什么这样做(

2 个答案:

答案 0 :(得分:0)

您需要匹配/user URI。使用自定义位置或使用 location / { rewrite (.*) index.html break; }

请在最后使用它,否则它将破坏您的/static/位置

答案 1 :(得分:0)

您需要添加:

location / {
    try_files $uri $uri/ =404;
}

有关更多信息,check