\$\begingroup\$
\$\endgroup\$
0
I needed to IP block something in nginx and I ended up with duplicated proxy_forward
code. How can I refactor and un-duplicate this?
server{
location /admin{
allow 123.90.250.0/24;
allow 123.66.148.0/24;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://foo;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://foo;
}
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Jul 27, 2012 at 3:45
1 Answer 1
\$\begingroup\$
\$\endgroup\$
You may move proxy settings to 'server' block. But proxy_pass should stay at 'location' block
upstream foo {
server 127.0.0.1:8080;
}
server{
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
location /admin {
allow 123.90.250.0/24;
allow 123.66.148.0/24;
proxy_pass http://foo;
}
location / {
proxy_pass http://foo;
}
}
answered Jul 27, 2012 at 9:24
default