How to reuse NGINX proxy settings in multiple places

I need to restrict access to certain files based on the query string parameter. I have a NGINX proxy that is in front of several other nginx web servers for load balancing. I decided to apply this query string parameter at the proxy level to consolidate the configuration changes. This complicated my setup a bit because the request cannot fall into the trap if it should be sent upstream.

server { listen 443; # SSL Settings server_name staging.xxxx.com; location / { proxy_pass http://webdav-cluster; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; } # Unless the correct application token is passed in as a query parameter # then deny access. location ~ \/protected\/.*txt$ { if ($arg_secret != abc) { return 403; } proxy_pass http://webdav-cluster; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $http_host; } } 

Is there a way to store these 4 proxy lines in a location or variable and then redirect internally to one line? I can also use the same settings in different virtual hosts.

+7
source share
1 answer

In this case, you should use the include directive: http://nginx.org/r/include

+2
source

All Articles