nginx proxy_cache 與 POST

提供一個能使用在POST的 proxy_cache 設定方式與注意事項

#user  nobody;
worker_processes  1;
events {
    worker_connections  1024;
}

http {
    proxy_cache_path /tmp/cache levels=1:2 keys_zone=quanto_cache:128m max_size=10g inactive=10s;
    include       mime.types;
    default_type  application/octet-stream;
    server {
        listen       80;
        server_name  localhost;
        add_header 'Access-Control-Allow-Origin' '*';       #允許所有來源訪問
        add_header 'Access-Control-Allow-Methods' 'POST'; #允許所有方式訪問,可改成只有POST

        proxy_cache_lock on;                #確保同時只會發一個Request到後端
        proxy_cache_lock_age 300s;
        proxy_cache_lock_timeout 300s;
        
        location / {
            add_header Access-Control-Allow-Origin *;
            try_files $uri @backend;
        }

        location @backend {
            proxy_pass http://123.123.123.123:80;
            proxy_set_header X-Real-IP $remote_addr;            #把IP送給反向代理的 server
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    #把Protocol送給反向代理的 server
            proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;     #把Protocol送給反向代理的 server

            proxy_cache quanto_cache;
            proxy_buffering on;
            proxy_cache_methods POST;                       #這邊cache POST
            proxy_cache_key "$request_uri|$request_body";   #需要重新設定key
            proxy_buffers 8 64k;
            proxy_buffer_size 128k;
            proxy_cache_valid any 10s;                      #只保留 1s有效
            proxy_cache_min_uses 1;                         #至少一次訪問才開啟cache功能
            proxy_cache_use_stale updating;                 #還在更新時會勉強用舊的
            proxy_cache_background_update on;               #如果用了舊的,也會更新,下次用
            proxy_ignore_headers Cache-Control Set-Cookie;  #使用POST cache時需過濾掉這些
            add_header X-Cached-Edge $upstream_cache_status;
        }
    }
}

大部分都寫在範例註解內了,該範例是拿來將前端 POST ajax request 轉傳至後端伺服器,並且將會在本機上cache 10秒

  1. 因為預設的proxy_cache_key 並無法區分 POST 的內容差別,所以必須重新設定一個key
  2. proxy_cache_valid 跟 inactive 有相似功能,前者為cache 功能內的設定,後者指的是cache 使用的檔案可以存在多久,通常檔案可以久一點或者兩者一樣。
  3. $upstream_cache_status 可以顯示目前這次是否使用 cache 內容 (MISS HIT… 等等)
  4. proxy_ignore_headers 對於 POST 很重要,需要加上才能正常使用cache,因為正常來說POST內容是獨一無二的,預設不使用cache。
  5. proxy_pass 可以指定單一網址作為後端,也可以另外設置upstream 做負載平衡機制。
  6. 記得先mkdir 檔案目錄 mkdir /tmp/cache
  7. #user nobody; 把註解拿掉後,可以指定用誰的名義跑nginx

參考連結:

https://cloud.tencent.com/developer/article/1051461

https://www.app-scope.com/tutorial/configure-nginx-as-load-balancer.html

https://blog.csdn.net/abu935009066/article/details/130115516?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_baidulandingword~default-1-130115516-blog-118713588.235^v43^pc_blog_bottom_relevance_base2&spm=1001.2101.3001.4242.2&utm_relevant_index=2

https://sharefunyeh.gitbooks.io/webdev/content/articles/understand-nginx-proxy-load-balancing-buffer-and-cache.html

https://www.cnblogs.com/Soy-technology/p/16357860.html

https://ithelp.ithome.com.tw/m/articles/10300110

https://www.cnblogs.com/lvzhenjiang/p/14022161.html

https://stackoverflow.com/questions/50228180/post-response-caching-does-not-work-in-nginx

https://my.f5.com/manage/s/article/K000088625

https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_key

https://docs.nginx.com/nginx/admin-guide/content-cache/content-caching/

發佈留言