Reverse Proxy
http {
upstream app1 {
server 192.168.1.10:8080;
server 192.168.1.11:80;
server 192.168.1.12:8080;
}
server {
listen 80;
location / {
proxy_pass http://app1;
}
}
}
Header
server {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $hostname;
proxy_set_header X-Real_IP $reomote_addr;
}
Keep-Alive
server {
...
keepalive_timeout 60;
}
http {
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:80;
server 192.168.1.12:8080;
}
server {
listen 80;
location / {
proxy_http_verssion 1.1;
proxy_set_header Connection "";
proxy_pass http://backend;
}
}
}
Redirect
http {
upstream backend {
server 192.168.1.10:8080;
server 192.168.1.11:80;
server 192.168.1.12:8080;
}
server {
listen 80;
server_name example.com;
location /one/ {
proxy_pass http://backend/two;
proxy_redirect http://backend/two http://example.com/one/;
}
location /two/ {
rewrite ~/three/1 /three/2
proxy_pass http://backend/for/;
proxy_redirect http://backend/four/2 http://example.com/three/1/;
}
}
}
Cookie
server {
listen 80;
server_name example.com;
location /one/ {
proxy_pass http://backend/two;
proxy_cookie_path /two/ /one/
proxy_cookie_domain backened $server_name
}
}
weight
upstream backend {
server 192.168.1.10 weight=2;
server 192.168.1.11:weight=1;
server 192.168.1.12;
}
basckup server
upstream backend {
server 192.168.1.10;
server 192.168.1.11 backup;
}
Connect to Min Conection counts server
least_conn;
upstream backend {
server 192.168.1.10;
server 192.168.1.11;
}
Hash
upstream backend {
hash $http_user_agent
server 192.168.1.10;
server 192.168.1.11;
}
upstream backend {
hash $url consistent
server 192.168.1.10;
server 192.168.1.11;
}
timeout
location /one/ {
proxy_pass http://backend/two;
proxy_content_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;
}
cache
proxy_cache_path /var/cache/nginx/rproxy
levels=1:2 keys_zone=proxy1:10m
inactive=1d;
http {
upstream app1 {
server 192.168.1.10;
}
server {
listen 80;
location / {
proxy_cache proxy1
proxy_chache_bypass $http_authorization $jttp_cookie;
proxy_no_chache $http_authorization $jttp_cookie;
proxy_pass http://app1;
}
}
}
SSL
http {
upstream app1 {
server 192.168.1.10;
}
server {
listen 443 ssl;
ssl_certificate /etc/...../server.crt
ssl_certificate_key /etc/....../server.key
location / {
proxy_cache proxy1
proxy_chache_bypass $http_authorization $jttp_cookie;
proxy_no_chache $http_authorization $jttp_cookie;
proxy_pass http://app1;
}
}
}
http {
upstream app1 {
server 192.168.1.10:443;
}
server {
location / {
proxy_no_chache $http_authorization $jttp_cookie;
proxy_pass http://app1;
}
}
}
webSocket
http {
upstream app1 {
server 192.168.1.10:443;
}
server {
location /chat/ {
proxy_pass http://app1;
proxy_http_verssion 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Upgrade Connection $connection_upgrade;
proxy_read_timeout 1h;
}
}
}
File upload size & Buffer
server {
location /chat/ {
...
cliecnt_max_body_size 100m;
client_body_buffer_size 1m;
client_body_temp_path /var/tmp/nginx_temp 1 2;
}
}
server {
location /chat/ {
...
proxy_reqest_buffering off;
proxy_http_verssion 1.1;
}
}
For maintenance
geo $maintenance{
default 1;
192.168.1.0/24 0;
}
server {
error_page 503 /503.html;
location = /503.html{
root /path/to/eroorpage;
internal
}
location /image/ {
alias /path/to/image/;
}
location /css/ {
alias /path/to/css/;
}
location / {
if($maintenace){
return 503;
}
proxy_pass ....;
}