Nginx: How can I re-use this proxy pass config in multiple locations?

Brian asked:

I have a location block like this:

    location / {
            proxy_pass http://mediacluster;
            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;
    }

Which passes requests to an upstream block. I am adding more complicated location blocks now, some of which I also want to pass to that upstream block. How can I achieve that without copying the proxy code over and over?

Can I set it up as an internal location and then reference that location somehow, or maybe I just need to place this is an external config file and include it in each area?

My answer:


You’re already aware of include. This is the way it’s normally done, especially when you have to reuse the configuration in different servers.


View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.