Skip to content

Instantly share code, notes, and snippets.

@JarenGlover
Last active November 14, 2021 18:01
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save JarenGlover/d7ffab312ea756834218 to your computer and use it in GitHub Desktop.
Save JarenGlover/d7ffab312ea756834218 to your computer and use it in GitHub Desktop.
Nginx - Reverse Proxy | Backend & Front End Example
upstream fuel {
# Defines a group of servers. Servers can listen on different ports.
server IP:PORT fail_timeout=0;
}
upstream frontend {
server IP:PORT fail_timeout=0;
}
server {
listen IP:80;
# domains and IP address you want to serve
server_name .data.jarenglover.com SERVER_IP_ADDRESS;
access_log /path/to/log/on/server/nginx_access.log; # set your logs
# note: uses diff logs for diff sub domains
error_log /path/to/log/on/server/nginx_error.log;
location / {
# requests on '/' will be proxy_pass
#proxy server for sending data to node.js
proxy_pass http://frontend;
}
location /img {
# remember let nginx handle static files not app code
root /path/to/static/files/frontend/app/;
}
location /api {
# requests to the API will be proxy_pass to the backend API infra
# read this -> http://en.wikipedia.org/wiki/X-Forwarded-For
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# pass the host header from the client to help with redirects
proxy_set_header Host $http_host;
# stops nginx from doing something silly
proxy_redirect off;
# proxy_pass to backend API Django in my case
proxy_pass http://api;
# send the IP address and remote server address for secuirty
proxy_set_header X-Real-IP $remote_addr;
# Adds headers to the HTTP response
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
}
server {
# If the remote server is not listed its goes to the default_server
listen IP:PORT default_server;
return 404;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment