Skip to main content
Post Reopened by Jamal
edited body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I needed to IP block something in nginx and I ended up with duplicated proxy_forwardproxy_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;
    }
}

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;
    }
}

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;
    }
}
Post Closed as "Needs details or clarity" by Jamal
Tweeted twitter.com/#!/StackCodeReview/status/228764545106186240
edited title
Link
Kit Sunde
  • 690
  • 5
  • 15

Unduplicating prox_pass in duplicate nginx location blocks

Source Link
Kit Sunde
  • 690
  • 5
  • 15

Unduplicating prox_pass in duplicate nginx location blocks

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;
    }
}