2
\$\begingroup\$

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;
    }
}
\$\endgroup\$
0

1 Answer 1

2
\$\begingroup\$

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;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.