Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
nginx proxy server example
upstream my_upstream { #this line starts the list of real resources that exist behind the nginx server
server; #these servers are used in the order they are defined, in round robin fashion. there's more ways of load balancing in the docs
server backup; #amazingness no.1, the keyword "backup" means that this server should only be used when the rest are non-responsive
keepalive 64;
server {
listen 80;
location /resource.html { #in my example the server only responds to requests for the file /request.html
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_pass http://my_upstream; #this sets the name of the server list that will actually process the request
proxy_intercept_errors on; #amazingness no.2 if all of the proxied servers are down, this will return a 50x error (bad gateway) with this line we tell nginx to intercept this error instead of giving it back to the user
error_page 502 503 504 =200 #in combination with the line above, if a 50x error exists nginx will redirect to the example host that can server the static content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.