Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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;
    }
}
share|improve this question

1 Answer

up vote 1 down vote accepted

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;
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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