nginx 过期 header 和反向代理不起作用
softidea
阅读:64
2025-06-02 22:19:02
评论:0
我正在尝试为 nginx (0.7.67) 上的静态文件配置一个过期 header 。静态文件由 Golang 反向代理提供:
location /rev/ {
proxy_pass http://localhost:8910/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
# I am putting this here, because nginx only uses one location. Is this OK?
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
expires 30d;
}
}
当我这样做时,重启 nginx 没有错误,但不再提供静态文件。
我已经尝试过以下星座,但它不起作用:
server {
...
location /rev/ {
proxy_pass http://localhost:8910/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
location ~* \.(js|css|jpg|jpeg|gif|png|svg|ico|pdf|html|htm)$ {
expires 30d;
}
}
问题:如何为位于反向代理后面的应用程序上的静态文件应用过期 header ?
请您参考如下方法:
我设法完成它的唯一方法是这样的:
location / {
proxy_pass http://tomcat;
}
# CACHING WITH NO LOGGING
location ~* ^.+\.(atom|bmp|bz2|doc|docx|eot|exe|gif|gz|ico|jpeg|jpg|mid|midi|mp4|ogg|ogv|otf|pdf|png|ppt|pptx|rar|rss|rtf|svg|svgz|swf|tar|tgz|ttf|txt|wav|woff|xls|zip)$ {
access_log off;
log_not_found off;
expires max;
proxy_pass http://tomcat;
}
# CACHING WITH 404 LOGGING
location ~* ^.+\.(css|js|xml)$ {
access_log off;
log_not_found on;
expires max;
proxy_pass http://tomcat;
}
希望对您有所帮助!
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。



