Nginx

NGINX:js/css 不在

  • May 25, 2020

在嘗試在我的一個網站上設置 404 網頁時,我找不到包含斜杠的不存在連結的解決方案,例如,如果我嘗試連接到https://supra.tf/anything,它將返回正確的頁面,但在嘗試https://supra.tf/anything/anything正確的頁面後是被返回,但沒有格式化(又名 css/js 文件)。這是我的 nginx 配置:

       root /var/www/supra.tf/public_html;
       index index.html;
       server_name supra.tf;
       error_page 404 /404.html;
       location / {
               rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
               rewrite ^/(.*)/$ /$1 permanent;
               try_files $uri/index.html $uri.html $uri/ $uri =404;
       }
       location = /404.html {
               root /var/www/errors;
}
       location ~* ^.+\.(js|css|img)$ {
       root /var/www/errors;
       try_files $uri/index.html $uri.html $uri/ $uri =404;
}
       add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";
       listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
       listen 443 ssl http2; # managed by Certbot
       ssl_certificate /etc/letsencrypt/live/supra.tf/fullchain.pem; # managed by Certbot
       ssl_certificate_key /etc/letsencrypt/live/supra.tf/privkey.pem; # managed by Certbot
       include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
       ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
       listen 80;
       listen [::]:80;
       server_name supra.tf;
       return 301 https://supra.tf$request_uri;
}

這裡可能有什麼問題?

答案就在這裡: https ://stackoverflow.com/questions/37860639/nginx-custom-404-css-js-not-found 這是將 404.html 中的 css/js 文件位置編輯為絕對位置的問題 - 在無論站點位置如何,都會使用該案例文件。

引用自:https://serverfault.com/questions/1018554