Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created February 22, 2018 16:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/fa7074f482d30a0759a569299b3211d2 to your computer and use it in GitHub Desktop.
Save chrisguitarguy/fa7074f482d30a0759a569299b3211d2 to your computer and use it in GitHub Desktop.
How to proxy requests from nginx to an upstream backend. Could be use for Python or JavaScript (express) apps.
http {
# other stuff here
# The upstream backend server this can be named whatever you desire
# See http://nginx.org/en/docs/http/ngx_http_upstream_module.html
upstream backend {
# this is the actual hostname + port of the backend server
server backend:8080;
# how many keep alive connections to allow, only set this if you're going
# to specify the `Connection` header in the proxy below, nginx sets the
# proxy to "Connection: close" by default.
keepalive 32;
}
# the main entrypoint server
server {
server_name yourapp.example.com;
listen 80;
# maybe HTTPS stuff here too...
# document root!
root /app/public;
# this is the important bit! Try to load the request URI or fallback to the named
# @backend location
try_files $uri @backend;
# you may also want to do something like this if you have index files in your app
# this will try the actual URI, then the URI as a directory, then fallback to
# the backend location if those two don't work.
# index index.html;
# try_files $uri $uri/ @backend;
# This is a "named" location (see http://nginx.org/en/docs/http/ngx_http_core_module.html#location)
# it defines some proxy headers then pases the request to the upstream `backend` defined above
location @backend {
proxy_set_header Host $http_host;
proxy_set_header Connection ""; # use keepalive connections
proxy_set_header X-Forwarded-For $remote_addr; # pass the real IP
# any other headers you may want!
proxy_http_version 1.1;
proxy_pass http://backend;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment