-
-
Save nosun/9e85fb2ca2c31f758abcfbb3991e5be7 to your computer and use it in GitHub Desktop.
cache POST request with Nginx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
upstream api_example_net { | |
server api.example.net:4000; | |
keepalive 600; | |
} | |
proxy_cache_path /var/cache/nginx/tag levels=1:2 keys_zone=tag:10m inactive=1d max_size=10g; | |
server { | |
listen 80; | |
server_name api.example.net; | |
default_type application/json; | |
access_log /var/log/nginx/api.example.net.access.log main; | |
error_log /var/log/nginx/api.example.net.error.log; | |
client_max_body_size 2m; | |
client_body_buffer_size 2m; | |
set $request_body_md5 ""; | |
rewrite_by_lua ' | |
ngx.req.read_body() | |
local body = ngx.req.get_body_data() | |
ngx.var.request_body_md5 = ngx.md5(body) | |
'; | |
location / { | |
proxy_http_version 1.1; | |
proxy_set_header Host $host; | |
proxy_set_header Connection ""; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; | |
proxy_cache tag; | |
proxy_cache_key "$uri|$query_string|$request_body_md5"; | |
proxy_cache_valid 200 1d; | |
proxy_cache_methods POST; | |
add_header X-Cache-Status $upstream_cache_status; | |
proxy_pass http://api_example_net; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment