使用 proxy

编程入门 行业动态 更新时间:2024-10-23 22:25:50
本文介绍了使用 proxy_pass 为 NGINX 上的图像设置浏览器缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 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

本文发布于:2023-11-01 02:23:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1547980.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:proxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!