【问题标题】:Nginx can't return 304 when using http proxy with ngx_http_sub_module使用带有 ngx_http_sub_module 的 http 代理时,Nginx 无法返回 304
【发布时间】:2016-07-07 10:12:43
【问题描述】:

我想用 Nginx 反向代理网站 www.gstatic.com。

  • 使用站点 gstatic.178.re 反向代理 www.gstatic.com
  • 替换资源中包含“www.gstaic.com”的内容,以便“gstatic.178.re”仍然可以处理进一步的请求。

基本上它有效,但如果我再次访问相同的链接,它不会返回 304。我在 Nginx 上启用了缓存,还尝试设置 etag 和 expires 属性。 这是我的 Nginx 配置,

upstream gstatic { 
server www.gstatic.com; 
} 

proxy_buffering on; 
etag on; 
proxy_temp_file_write_size 1024k; 
proxy_temp_path /var/cache/nginx/temp; 
proxy_cache_path /var/cache/nginx/cache levels=1:2 keys_zone=cache_one:100m inactive=19d max_size=5g; 

server { 
listen 80; 

server_name gstatic.178.re; 

resolver 8.8.8.8; 

location / { 
sub_filter "www.gstatic.com" "gstatic.178.re"; 
sub_filter "https" "http"; 
sub_filter_once off; 
sub_filter_types *; 
proxy_pass_header Server; 
proxy_set_header Host www.gstatic.com; 
proxy_set_header Accept-Encoding ''; 
proxy_redirect off; 
proxy_set_header X-Real-IP $remote_addr; 
proxy_set_header X-Scheme $scheme; 
proxy_pass http://gstatic; 
proxy_cache cache_one; 
proxy_cache_valid 200 304 365d; 
proxy_cache_key $host$uri$is_args$args; 
proxy_cache_min_uses 1; 
expires max; 
proxy_cache_revalidate on; 
add_header X-Proxy-Cache $upstream_cache_status; 
add_header Cache-Control "public"; 
if_modified_since before; 
} 


}

这是一个用于测试的示例链接:http://gstatic.178.re/charts/loader.js

【问题讨论】:

    标签: caching nginx proxy


    【解决方案1】:

    我在所提供资源的响应标头中发现了几个问题。 首先,您违反了RFC 规则之一,该规则说:

    HTTP/1.1 服务器不应发送多个过期日期 未来一年。

    因此,请尝试降低您的 max-age 和 Expires 值以满足该要求。

    其次,响应标头中没有 ETag。

    【讨论】:

    • 我尝试更改 expires 和 max-age 但仍然无法正常工作。我注意到与普通的 304 请求相比,来自浏览器的这些请求不发送标头:“ If-Modified-Since ”。
    • 请分享带有标题的截图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-17
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多