V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Flands
V2EX  ›  问与答

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

  •  
  •   Flands · 2021-10-26 20:59:57 +08:00 · 1105 次点击
    这是一个创建于 1106 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请求的路径是 请求 -> 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 。 试了很多配置都不行,想问一下。

    wellsc
        1
    wellsc  
       2021-10-26 21:20:56 +08:00
    只用一个代理不行嘛
    Flands
        2
    Flands  
    OP
       2021-10-27 09:40:56 +08:00
    @wellsc 项目架构是这样的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5550 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:30 · PVG 09:30 · LAX 17:30 · JFK 20:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.