Nginx - 之前加载某些资源时未找到它们

Nginx - 之前加载某些资源时未找到它们

我刚刚遇到了一个非常奇怪的问题。我网站中使用的一些字体和 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错误。

答案1

查看此处的注释行:

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

您还从 try_files 语句中删除了该$uri术语。该术语负责提供与文件名直接匹配的任何 URI。

您应该恢复它,例如:

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

这个文件了解详情。

相关内容