Skip to content

Instantly share code, notes, and snippets.

@nosun
Forked from mrluanma/api.example.net.conf
Created August 22, 2017 06:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nosun/9e85fb2ca2c31f758abcfbb3991e5be7 to your computer and use it in GitHub Desktop.
Save nosun/9e85fb2ca2c31f758abcfbb3991e5be7 to your computer and use it in GitHub Desktop.
cache POST request with Nginx
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