Nginx

Nginx 不記憶體數據

  • March 4, 2021

我在 nginx 代理後面有一個 REST API。代理工作正常,但我無法記憶體任何響應。任何幫助將非常感激:

Nginx 配置:

worker_processes  10;
error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

pid        logs/nginx.pid;


events {
   worker_connections  1024;
}


http {
       proxy_cache_path /path/to/cache/dir keys_zone=one:60m;
       proxy_cache_methods GET HEAD POST;

    upstream backend {
       server server1 backup;
       server server2 weight=5;
   }
   access_log  logs/access.log;

   sendfile        on;
   #tcp_nopush     on;

   #keepalive_timeout  0;
   keepalive_timeout  65;

   #gzip  on;

   server {
       listen       7076;
       server_name  localhost;
       #charset koi8-r;
       access_log  logs/host.access.log;

       location / {
           add_header 'Access-Control-Allow-Origin' *;
           add_header 'Access-Control-Allow-Credentials' 'true';
           add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept';
           add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';

           proxy_cache one;
           proxy_cache_key $host$uri$is_args$args;

           add_header X-Proxy-Cache $upstream_cache_status;

           proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
           proxy_ignore_headers Set-Cookie;
           proxy_ignore_headers Cache-Control;

           proxy_hide_header Cache-Control;
           proxy_hide_header Set-Cookie;
           proxy_pass http://backend;
       }
   }
}

無論我嘗試了什麼,代理記憶體總是以 MISS 的形式返回:

請求標頭是:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:max-age=0
Connection:keep-alive
Host:nginxserver:portnumber
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36

響應標頭是:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:Content-Type,Accept
Access-Control-Allow-Methods:GET, POST, OPTIONS
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Type:text/plain;charset=UTF-8
Date:Wed, 15 Oct 2014 16:30:18 GMT
Server:nginx/1.7.4
Transfer-Encoding:chunked
X-Proxy-Cache:MISS

我懷疑它與客戶端標頭有關,但即使我通過 curl 發出呼叫並檢查標頭,也沒有響應。

提前致謝

您沒有告訴 NGINX 響應的有效時間以及必須從記憶體中提供的時間。

這必須用proxy_cache_valid指令指定。

proxy_cache one;
proxy_cache_key $host$uri$is_args$args;
proxy_cache_valid 200 10m;

但是,這不適用於 POST 請求,因為如果它們沒有相同的內容,則在相同 URL 上沒有與 POST 請求不同的記憶體鍵。

因此,您需要將記憶體鍵調整為$host$request_uri|$request_body. 您將必須監控記憶體大小(proxy_cache_path參數max_size)和代理響應緩衝區proxy_buffer_size ,以便滿足您的需求。

引用自:https://serverfault.com/questions/637237