Difference between revisions of "Nginx reverse proxy BE by ip address"
Jump to navigation
Jump to search
(Created page with "https://www.cyberciti.biz/faq/nginx-redirect-backend-traffic-based-upon-client-ip-address/") |
|||
Line 1: | Line 1: | ||
https://www.cyberciti.biz/faq/nginx-redirect-backend-traffic-based-upon-client-ip-address/ | https://www.cyberciti.biz/faq/nginx-redirect-backend-traffic-based-upon-client-ip-address/ | ||
+ | |||
+ | ``` | ||
+ | ## apachereadonly backend ## | ||
+ | upstream apachereadonly { | ||
+ | server 10.10.11.10:8011; | ||
+ | server 10.10.11.11:8011; | ||
+ | server 10.10.11.12:8011; | ||
+ | ip_hash; | ||
+ | } | ||
+ | ## apachereadwrite backend ## | ||
+ | upstream apachereadwrite { | ||
+ | server 10.10.11.13:8011; | ||
+ | |||
+ | } | ||
+ | |||
+ | ## config ## | ||
+ | location / { | ||
+ | proxy_set_header Accept-Encoding ""; | ||
+ | proxy_set_header Host $http_host; | ||
+ | proxy_set_header X-Forwarded-By $server_addr:$server_port; | ||
+ | proxy_set_header X-Forwarded-For $remote_addr; | ||
+ | proxy_set_header X-Forwarded-Proto $scheme; | ||
+ | proxy_set_header X-Real-IP $remote_addr; | ||
+ | ## default backend | ||
+ | proxy_pass http://apachereadonly; | ||
+ | ## send traffic to apachereadwrite backend if ip is 1.2.3.4 ## | ||
+ | if ( $remote_addr ~* 1.2.3.4 ) { | ||
+ | proxy_pass http://apachereadwrite; | ||
+ | } | ||
+ | proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; | ||
+ | } | ||
+ | ## rest of config ## | ||
+ | ``` |
Latest revision as of 20:22, 10 October 2022
https://www.cyberciti.biz/faq/nginx-redirect-backend-traffic-based-upon-client-ip-address/
## apachereadonly backend ## upstream apachereadonly { server 10.10.11.10:8011; server 10.10.11.11:8011; server 10.10.11.12:8011; ip_hash; } ## apachereadwrite backend ## upstream apachereadwrite { server 10.10.11.13:8011; } ## config ## location / { proxy_set_header Accept-Encoding ""; proxy_set_header Host $http_host; proxy_set_header X-Forwarded-By $server_addr:$server_port; proxy_set_header X-Forwarded-For $remote_addr; proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Real-IP $remote_addr; ## default backend proxy_pass http://apachereadonly; ## send traffic to apachereadwrite backend if ip is 1.2.3.4 ## if ( $remote_addr ~* 1.2.3.4 ) { proxy_pass http://apachereadwrite; } proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; } ## rest of config ##