Nginx

Nginx - 以前載入時未找到某些資源

  • February 1, 2021

我剛剛遇到了一個非常奇怪的問題。我在我的網站和 svg 中使用的一些字型沒有載入,而昨天一切正常。我最近所做的唯一更改是一些重寫以刪除副檔名。

每個路徑都是正確的,並且主nginx.conf文件包含include /etc/nginx/mime.types; 再次,昨天一切正常。

這是我的配置文件,這是我唯一更改的內容。


server {
   listen 80;
   listen [::]:80;
   
   server_name mysite.app;
   
   rewrite ^(/.*)\.html(\?.*)?$ $1$2 permanent;
   rewrite ^/(.*)/$ /$1 permanent;

   root /var/www/html/;
 
   index index.php index.html index.htm index.nginx-debian.html;
      
   location / {
       #try_files $uri $uri.html $uri/ @extensionless-php;
       try_files $uri/index.html $uri.html $uri/ @extensionless-php;
   }

   location ~*  \.(jpg|jpeg|png|gif|ico|css|js)$ {
               expires 365d;
       }

   location ~*  \.(pdf)$ {
               expires 30d;
       }
   
   location ~ \.php$ {
       include snippets/fastcgi-php.conf;

       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
   
       if ($request_uri ~ ^/([^?]*)\.php$) { 
            return 307 /$1; 
       }
       
       if ($request_uri ~ ^/([^?]*)\.php(\?+)) { 
            return 302 /$1?$args; 
        }
   }

   location @extensionless-php {
           rewrite ^(.*)$ $1.php last;
   }   

   location ~ /\.ht {
       deny all;
   }

   listen [::]:443 ssl ipv6only=on; # managed by Certbot
   listen 443 ssl; # managed by Certbot
   ssl_certificate /etc/letsencrypt/live/mysite.app/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/mysite.app/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 443 ssl http2; # managed by Certbot
   server_name www.mysite.app;
   ssl_certificate /etc/letsencrypt/live/mysite.app/fullchain.pem; # managed by Certbot
   ssl_certificate_key /etc/letsencrypt/live/mysite.app/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

   return 301 https://mysite.app$request_uri;
}

server {
   if ($host = www.mysite.app) {
       return 301 https://$host$request_uri;
   } # managed by Certbot


   listen 80 ;
   listen [::]:80 ;
   server_name www.mysite.app;
   return 404; # managed by Certbot
}

執行curl -IL <resource_location>時出現HTTP/1.1 404 Not Found錯誤。

查看此處的註釋行:

location / {
   #try_files $uri $uri.html $uri/ @extensionless-php;
   try_files $uri/index.html $uri.html $uri/ @extensionless-php;
}

您還$uri從 try_files 語句中刪除了該術語。該術語負責提供與文件名直接匹配的任何 URI。

您應該恢復它,例如:

location / {
   try_files $uri $uri/index.html $uri.html $uri/ @extensionless-php;
}

有關詳細資訊,請參閱此文件

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