想问一下为何 nginx 缓存不生效

2021-10-26 20:59:57 +08:00
 Flands

请求的路径是 请求 -> nginx(第一层) -> 前端 docker -> docker 内 nginx(第二层,指向 html)

服务有两层 nginx ,分别配置如下

第一层

location / {
     proxy_pass http://xxx/;
     proxy_max_temp_file_size         0k;
     proxy_set_header Host            $host;
     proxy_set_header X-Real-IP       $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
     add_header X-Frame-Options "SAMEORIGIN";

proxy_buffering on;
proxy_cache_valid any 60m;
}

第二层

server {
    listen        8080;
    
    server_name  172.18.162.xx;

    add_header X-Frame-Options sameorigin always;

    root   /data/app/dist;
    try_files $uri $uri/ /index.html;
    index  index.html index.htm;

add_header Cache-Control no-cache;
    add_header Cache-Control private;

    # gzip
    location ~ .*\.(js|css)?$ {
        gzip  on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.0;
        gzip_comp_level 6;
        gzip_types       text/plain application/x-javascript text/css text/javascript application/javascript application/json image/jpeg image/png image
        gzip_disable "MSIE [1-6]\.";
        expires 30d;
add_header Pragma public;
    add_header Cache-Control "public";
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

响应头

cache-control: max-age=2592000
content-encoding: gzip
content-type: text/css
date: Tue, 26 Oct 2021 12:50:14 GMT
etag: W/"61729535-86694"
expires: Thu, 25 Nov 2021 12:50:14 GMT
last-modified: Fri, 22 Oct 2021 10:40:53 GMT
server: nginx/1.20.1
strict-transport-security: max-age=63072000; includeSubdomains; preload
x-frame-options: sameorigin
x-frame-options: SAMEORIGIN

请求头

:authority: 172.18.162.190
:method: GET
:path: /css/common.06e66be2.css
:scheme: https
accept: text/css,*/*;q=0.1
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6
cookie: 
dnt: 1
referer: https://172.18.162.190/training/client/mobile/examing/1
sec-ch-ua: "Microsoft Edge";v="93", " Not;A Brand";v="99", "Chromium";v="93"
sec-ch-ua-mobile: ?1
sec-ch-ua-platform: "Android"
sec-fetch-dest: style
sec-fetch-mode: no-cors
sec-fetch-site: same-origin
user-agent: Mozilla/5.0 (Linux; Android 8.0.0; Pixel 2 XL Build/OPD1.170816.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Mobile Safari/537.36 Edg/93.0.961.47

主要问题是浏览器拿 js/css 文件时,只会 200 ,快速刷新时使用内存缓存,过几分钟请求依旧是 200 ,不会走磁盘缓存或者 304 。 试了很多配置都不行,想问一下。

1124 次点击
所在节点    问与答
2 条回复
wellsc
2021-10-26 21:20:56 +08:00
只用一个代理不行嘛
Flands
2021-10-27 09:40:56 +08:00
@wellsc 项目架构是这样的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/810755

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX