gpt4 book ai didi

linux - NGINX - 忽略 js 和 css 文件的获取参数

转载 作者:太空宇宙 更新时间:2023-11-04 09:12:19 28 4
gpt4 key购买 nike

目前,我正在尝试镜像一个 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


}

最佳答案

使用 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$ {
...
}

关于linux - NGINX - 忽略 js 和 css 文件的获取参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53784002/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com