How can I configure nginx locations to share common configuration options?

Create a common proxy config and include as-needed.

/etc/nginx/api_proxy.conf

proxy_pass        http://127.0.0.1:9000/;
proxy_redirect    off;
proxy_set_header  Host             $http_host;
proxy_set_header  X-Real-IP        $remote_addr;
proxy_set_header  X-Forwarded-For  $proxy_add_x_forwarded_for;

proxy_cache cache-test;
proxy_cache_valid 200 302 24h;
proxy_cache_valid 404 60s;
add_header X-Cache-Status $upstream_cache_status;

Your Host Config File

...
location /api/0.1/user {
    include /etc/nginx/api_proxy.conf;
    proxy_cache_key /user/$http_authorization;
}
...

Leave a Comment