簡體   English   中英

刷新頁面后JS單頁站點出現404錯誤

[英]404 error at JS single page site after refresh page

我有 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;
    }

}

我不明白為什么它是工作(

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

最后使用它,否則它會破壞你的/static/位置

您需要添加:

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

欲了解更多信息檢查

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM