Nginx

Intro

General description & explanation about nginx

HTTP Proxying

Capability to pass requests off to backend HTTP servers for further processing.

  1. Handle many concurrent connections at once; ideal for acting/proxying as Point-of-Contant for clients.
  2. Fan-out to arbitrary backend servers, great for maintenance and scale-up-down.
  3. Proxying achieved by manipulating a request aimed at Nginx server and passing it to other servers for actual processing; results passing back, relayed to client. The other servers (remote, local, virtual) aka upstream servers**

Basic HTTP Proxy Pass

# server context

location /match/here {
    proxy_pass http://example.com;
}

Alternative Scenario

# server context

location /match/here {
    proxy_pass http://example.com/new/prefix;
}

sometimes this kind of replacement is impossible: then URI at end of proxy_pass is ignored and either the original URI from the client or the URI as modified by other directives will be passed to upstream server

Header Processing

Setting or Resetting Headers

# server context

location /match/here {
    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
    proxy_pass http://example.com/new/prefix;
}
# server context

proxy_set_header HOST $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

location /match/here {
    proxy_pass http://example.com/new/prefix;
}

location /different/match {
    proxy_pass http://example.com;
}

Defining Upstream Context for LB Proxied Connections

# http context

upstream backend_hosts {
    server host1.example.com;
    server host2.example.com;
    server host3.example.com;
}    

server {
    listen 80;
    server_name example.com;
    
    location /proxy-me {
        proxy_pass http://backend_hosts;
    }
}

Changing Upstream Balancing Algorithm

# http context

upstream backend_hosts {
    
    least_conn;
    
    # hash $remote_addr$remote_port consistent;
    
    server host1.example.com;
    ...
}

Setting Server Weight for Balancing

# http context

upstream backend_hosts {
    server host1.example.com weight=3;
    ...
}

Using Buffers to Free Up Backend Servers

# server context

proxy_buffering on;
proxy_buffer_size 1k;
proxy_buffers 24 4k;
proxy_busy_buffers_size 8k;
proxy_max_temp_file_size 2048m;
proxy_temp_file_write_size 32k;

location / {
    proxy_pass http://example.com;
}
# server context

proxy_buffering off;
proxy_buffer_size 4k;

...

High Availability

Image

Caching to Reduce Response Times

# http context

proxy_cache_path /var/lib/nginx/cache levels=1:2 keys_zone=backcache:8m max_size=50m;
proxy_cache_key "$scheme$request_method$host$request_uri$is_args$args";
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
sudo mkdir -p /var/lib/nginx/cache
sudo chown www-data /var/lib/nginx/cahce
sudo chmod 700 /var/lib/nginx/cache
# server context

location /proxy-me {
    proxy_cache backcache;
    proxy_cache_bypass $http_cache_control;
    add_header X-Proxy-Cache $upstream_cache_status;
    
    proxy_pass http://backend;
}

Notes

location / {
    expires 60m;
}

location /check-me {
    expires -1;
}
location /private {
    expires -1;
    add_header Cache-Control "no-store";
}

HAProxy to LB MySQL

https://www.digitalocean.com/community/tutorials/how-to-use-haproxy-to-set-up-http-load-balancing-on-an-ubuntu-vps

https://www.digitalocean.com/community/tutorials/how-to-use-haproxy-to-set-up-mysql-load-balancing--3


code · notebook · prose · gallery · qui et quoi? · main