AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 901125
Accepted
Lucio Crusca
Lucio Crusca
Asked: 2018-03-12 13:24:57 +0800 CST2018-03-12 13:24:57 +0800 CST 2018-03-12 13:24:57 +0800 CST

Nginx 作为反向代理不缓存

  • 772

我正在尝试将 Nginx 配置为缓存反向代理。源服务器是 Apache,它托管一个 WordPress 实例,如果这很重要的话。

反向代理功能按预期工作,但缓存似乎不起作用。如果我连续两次获得相同的静态资源,我会获得x-proxy-cache: MISS两次。

assodigitale.it 是域,138.201.87.123 是源服务器 IP 地址,138.201.87.124 是 Nginx 代理 IP 地址。

源服务器似乎回复允许代理缓存资源:

$ curl --connect-to ::138.201.87.123:443 --http2 -I https://assodigitale.it/wp-content/uploads/2018/03/aereo.jpg
HTTP/2 200 
date: Sun, 11 Mar 2018 20:59:39 GMT
server: Apache/2.4.25 (Debian)
content-length: 32989
strict-transport-security: max-age=31536000; includeSubdomains; preload
last-modified: Wed, 07 Mar 2018 09:34:41 GMT
etag: "80dd-566cf44ca2952"
accept-ranges: bytes
vary: Accept-Encoding
cache-control: max-age=1209600, public
x-content-type-options: nosniff
content-type: image/jpeg

正如预期的那样,对代理服务器的第一个请求会导致 MISS:

$ curl --connect-to ::138.201.87.124:443 --http2 -I https://assodigitale.it/wp-content/uploads/2018/03/aereo.jpg
HTTP/2 200 
server: nginx/1.13.9
date: Sun, 11 Mar 2018 21:04:00 GMT
content-type: image/jpeg
content-length: 32989
strict-transport-security: max-age=31536000; includeSubdomains; preload
last-modified: Wed, 07 Mar 2018 09:34:41 GMT
etag: "80dd-566cf44ca2952"
vary: Accept-Encoding
cache-control: max-age=1209600, public
x-content-type-options: nosniff
x-proxy-cache: MISS
strict-transport-security: max-age=4838400; includeSubDomains; preload
accept-ranges: bytes

对 Nginx 代理的第二个请求应该会导致 HIT,但会导致另一个 MISS:

$ curl --connect-to ::138.201.87.124:443 --http2 -I https://assodigitale.it/wp-content/uploads/2018/03/aereo.jpg
HTTP/2 200 
server: nginx/1.13.9
date: Sun, 11 Mar 2018 21:05:52 GMT
content-type: image/jpeg
content-length: 32989
strict-transport-security: max-age=31536000; includeSubdomains; preload
last-modified: Wed, 07 Mar 2018 09:34:41 GMT
etag: "80dd-566cf44ca2952"
vary: Accept-Encoding
cache-control: max-age=1209600, public
x-content-type-options: nosniff
x-proxy-cache: MISS
strict-transport-security: max-age=4838400; includeSubDomains; preload
accept-ranges: bytes

这是我的 nginx 配置的相关部分:

proxy_cache_path /srv/cache/nginx levels=1:2 keys_zone=revproxy:2000m inactive=2880m use_temp_path=off;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_methods GET HEAD;
proxy_cache_valid any 1m;
proxy_cache_valid 200 1440m;

server {
    listen 443 ssl http2;
    ssl on;
    server_name assodigitale.it;

    ssl_certificate /etc/letsencrypt/live/assodigitale.it/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/assodigitale.it/privkey.pem;

    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    ssl_protocols TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256';
    ssl_prefer_server_ciphers on;

    location / {
        proxy_cache revproxy;
        add_header X-Proxy-Cache $upstream_cache_status;
        add_header Strict-Transport-Security "max-age=4838400; includeSubDomains; preload";

        proxy_pass  https://138.201.87.123;
        proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
        proxy_cache_bypass $http_x_forceflushcacheurl;
        proxy_cache_lock on;
        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_hide_header Upgrade;
        proxy_buffering off;
        proxy_connect_timeout       600;
        proxy_send_timeout          600;
        proxy_read_timeout          600;
        send_timeout                600;
        proxy_ignore_headers Set-Cookie;

        http2_push_preload on;
        client_max_body_size 64M;
    }
}

该/srv/cache/nginx目录有 755 个权限和www-data所有者,Nginx 运行为www-data. 事实上,Nginx 确实在其中写入了它的文件夹,即0 1 2 3 4 5 6 7 8 9 a b c d e f,但是现在占用的总空间是 344Kb,对于一个非常大的站点来说,它的流量远远超过临时流量。

尝试curl上面相同的命令,但使用页面而不是图像,产生相同的结果,它总是一个 MISS。

为什么 Nginx 拒绝缓存资源?

nginx
  • 3 3 个回答
  • 4475 Views

3 个回答

  • Voted
  1. Best Answer
    zzhRex
    2018-06-06T00:24:20+08:002018-06-06T00:24:20+08:00

    你应该设置 proxy_buffering on,否则 nginx 不会缓存响应!

    官方文件说:

    当缓冲被禁用时,响应会在收到时立即同步传递给客户端。nginx will not try to read the whole response from the proxied server.nginx 一次可以从服务器接收的最大数据大小由 proxy_buffer_size 指令设置。

    • 5
  2. Lucio Crusca
    2018-03-21T03:03:12+08:002018-03-21T03:03:12+08:00

    我已经从我运行的另一个类似的 Nginx 代理中复制了配置,将其调整到网站上,现在它可以工作了。

    这是我目前使用的配置:

    proxy_cache_path /srv/cache/nginx levels=1:2 keys_zone=revproxy:2000m inactive=2880m use_temp_path=off;
    proxy_cache_key "$scheme$request_method$host$request_uri";
    proxy_cache_methods GET HEAD;
    proxy_cache_valid any 1m;
    proxy_cache_valid 200 1440m;    
    server {
        listen 443 ssl http2;
        ssl on;
        server_name assodigitale.it;
    
        ssl_certificate /etc/letsencrypt/live/assodigitale.it/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/assodigitale.it/privkey.pem;
    
            ssl_protocols        SSLv3 TLSv1 TLSv1.1 TLSv1.2;    
            ssl_ciphers RC4:HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers on;
            keepalive_timeout    60;
            ssl_session_timeout  10m;
    
        location / {
                proxy_cache revproxy;
                add_header X-Proxy-Cache $upstream_cache_status;
    
                proxy_pass  https://138.201.87.123;
                proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
                proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
                proxy_cache_lock on;
                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_hide_header      Upgrade;
                proxy_buffering on;
                proxy_connect_timeout       600;
                proxy_send_timeout          600;
                proxy_read_timeout          600;
                send_timeout                600;
                client_max_body_size 64M;
        }
    }
    

    虽然这可能是一个答案,因为它解决了问题,但我不明白它为什么有效(或者更准确地说,为什么以前的配置没有),所以我不会接受我自己的答案。

    也许有人可以发现使缓存工作的两种配置的特殊差异:这将是一个可以接受的答案。

    • 1
  3. Mario Olivio Flores
    2018-12-07T05:56:11+08:002018-12-07T05:56:11+08:00

    我们遇到了类似的问题。我们使用 nginx 作为 s3 存储桶的代理缓存(因此我们可以支持 IP 白名单)。我们注意到,即使我们的代理在 https 上提供服务,我们也没有从 https 端点采购。好像nginx不喜欢那样。一旦我们从 切换http到https,就没有问题了。

    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve