我使用 NGINX 作为反向代理,并具有以下设置将所有请求重定向到我的 SPA 容器
I am using NGINX as a reverse proxy and have the following setup that redirects all requests to my SPA container
location / { proxy_pass 172.x.x.x; proxy_intercept_errors on; error_page 404 = /index.html; }这有效,但所有图像的到期时间都设置为默认值 (max-age=3600).当我添加以下应该为所有图像类型设置到期时间的块时,所有图像都不再起作用(404 NOT FOUND).
This works but all images have their expiry set to the default (max-age=3600). When I add the following block that is supposed to set the expiry for all image types then none of the images work anymore (404 NOT FOUND).
location ~* \.(jpg|png)$ { add_header Cache-Control public; expires 1M; }我尝试将这个块嵌套在第一个块中,但随后我也得到了 404 响应.
I tried nesting this block inside the first one but then I also get the 404 responses.
为什么这不起作用,我应该更改什么以便所有图像都具有扩展的 max-age?
Why doesn't this work and what should I change so that all images have an extended max-age?
推荐答案Nginx 选择一个 location 到 处理请求.它使用位置块内的指令或从周围块继承.
Nginx chooses one location to process a request. It uses the directives within the location block or inherited from the surrounding block.
如果 .jpg 和 .png URI 的请求要向上游传递,location 还需要包含一个 proxy_pass 语句.
If the requests for .jpg and .png URIs are to be passed upstream, the location also needs to contain a proxy_pass statement.
或者,使用 map 设置 expires 值.有关详细信息,请参阅本文档.
Alternatively, use a map to set the expires value. See this document for details.
例如:
map $request_uri $expires { default off; ~*\.(jpg|png)(\?|$) 1M; } server { ... expires $expires; location / { ... } }我认为 expires 已经设置了缓存控制标头,但您也可以使用另一个 map 来设置 add_header 的值指令.
I think that expires already sets the cache-control header, but you can also use another map to set the value of an add_header directive.
更多推荐
使用 proxy
发布评论