Skip to content

Instantly share code, notes, and snippets.

@joshuaquek
Last active July 19, 2018 03:23
Show Gist options
  • Save joshuaquek/f370a27bd12d0048e6aca643b640bcd1 to your computer and use it in GitHub Desktop.
Save joshuaquek/f370a27bd12d0048e6aca643b640bcd1 to your computer and use it in GitHub Desktop.
Simple examples to use for configuring NGINX.
Summary: Simple examples to use for configuring NGINX.
# Assuming this file is created inside /etc/nginx/conf.d
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://125.0.1.2;
}
}
# Request flow: `localhost:80` ===> `http://125.0.1.2;`
# Assuming this file is created inside /etc/nginx/conf.d
server {
listen 80;
server_name example.com;
location /johnsmith {
proxy_pass http://125.0.1.2;
}
}
# Assuming that your server has a domain name of `example.com`
# Request flow: `example.com/johnsmith` =====loads=====>> `http://125.0.1.2;`
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://125.0.1.2/janedoe;
}
}
# Assuming that your server has a domain name of `example.com`
# Request flow: `example.com` =====loads=====>> `http://125.0.1.2/janedoe;`
server {
listen 80;
server_name example.com;
location /alanlee {
proxy_pass http://125.0.1.2/annasamuels;
}
}
# Assuming that your server has a domain name of `example.com`
# Request flow: `example.com/alanlee` =====loads=====>> `http://125.0.1.2/annasamuels;`
# Assuming this file is created inside /etc/nginx/conf.d
upstream cluster_of_hosts {
server 125.2.3.4:4000;
server 125.2.3.4:4001;
server 125.2.3.4:4002;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://cluster_of_hosts;
}
}
# Assuming that your server has a domain name of `example.com`
# Request flow: `example.com` then would hit `125.2.3.4:4000`, `125.2.3.4:4001`, `125.2.3.4:4002` in a round-robin manner.
# Assuming this file is created inside /etc/nginx/conf.d
# Let's say you want this configuration:
# `example.com` =====loads=====>> `http://127.0.0.1:3000`
# and `example.com/backend` =====loads=====>> `http://127.0.0.1:9000`
# assuming that the server running on `http://127.0.0.1:3000` does not have any '/backend' routes
# ----- The WRONG way to do it (it will not work): -----
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
}
server {
listen 80;
server_name example.com;
location /backend {
proxy_pass http://127.0.0.1:9000;
}
}
# ----- The RIGHT way to do it (this will work): -----
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
}
location /backend {
proxy_pass http://127.0.0.1:9000;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment