NGINX-忽略在js和css文件上获取参数

时间:2018-12-14 16:59:44

标签: linux nginx

当前,我正在尝试镜像WordPress网站,但遇到了有关nginx的问题。这些文件(特别是js和css)在文件末尾使用?ver = key保存,因此,当nginx尝试提供文件时,会出现404错误。

这是一个示例文件: wp-embed.min.js?ver=cce7ff2a6851b83ee00fd3873407f90c

如何让nginx在不给出404错误的情况下提供此文件?我不想重命名所有这些文件,因为它们有成千上万个。这是我的Nginx配置:

server {

    server_name [redacted];

    root /home/[redacted];
    index index.html index.php;

  location / {
        try_files $uri $uri/ =404;
    }
  location ~ \.php$ {
        include snippets/fastcgi-php.conf;
    #
    #   # With php7.0-cgi alone:
    #   fastcgi_pass 127.0.0.1:9000;
        # With php7.0-fpm:
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }


    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/[redacted]/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/[redacted]/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 {
    if ($host = [redacted]) {
        return 301 https://$host$request_uri;
    } # managed by Certbot



    server_name [redacted]

    listen 80;
    listen [::]:80;
    return 404; # managed by Certbot


}

1 个答案:

答案 0 :(得分:2)

使用try_files,您可以添加诸如$uri?$args之类的其他术语,以查找包含文字?和查询字符串的文件名。

一个问题是Nginx对文件扩展名感到困惑,无法确定正确的Content-Type。您可以通过为每种文件类型创建特定的位置来纠正此问题。

例如:

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

location ~ \.css$ {
    types {}
    default_type  text/css;
    try_files $uri $uri?$args =404;
}

location ~ \.js$ {
    types {}
    default_type  application/javascript;
    try_files $uri $uri?$args =404;
}

location ~ \.php$ {
    ...
}