禁用缓存时,Nginx对CSS文件给出400响应

时间:2017-10-27 10:22:46

标签: css caching nginx cache-control

我尝试在nginx中实现以下指令以避免缓存css文件:

upstream turntown_stream {
    ip_hash;
    server turntown_host:9443;
}

location ~* \.(?:css)$ {
    root /data/nginx/html;
    expires 0;
}

location / {
    root /data/nginx/html;
    index index.html index.htm;
}

location /turntown/ {
proxy_cache_bypass $http_upgrade;
proxy_pass https://turntown_stream/turntown/;
# Web sockets
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
}

但是,每当我尝试应用上述指令时,在输入https:// {DOMAIN} .com / turntown addrress后到达网络浏览器的任何css文件都会以404响应到达。

有人可以告诉我我做错了什么。

1 个答案:

答案 0 :(得分:0)

nginx选择一个locationprocess a request

处理以location ~* \.(?:css)$结尾的URI时,您的新location /turntown/块优先于.css块。

因此,任何以/turntown/生成并以.css结尾的URI都将被视为本地文件,并且该请求将不再向上游发送。

作为替代方案,expires指令可以与map一起使用。有关详细信息,请参阅this document

例如:

map $request_uri $expires {
    default          off;
    ~*\.(css)(\?|$)  0;
}

map指令放在http块中。

expires $expires;

如果expires指令放在server块中,它将适用于所有匹配的URI,而不管用location处理它们。