User Tools

Site Tools


Sidebar

https://trobinson.me TomR.me

Navigation

155 pages, 85 files


Status

lab:nginx

NGINX Config

Hide Version Info

server_tokens off;
 
# to set custom header:
more_set_headers 'Server: Something';

Proxy Example

example.org.conf
server {
    listen 80;
    server_name example.org;
 
    real_ip_header CF-Connecting-IP;
 
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_ssl_verify off;
        proxy_pass http://example.com/;
    }
 
}

PHP FastCGI

php.conf
location ~* \.php$ {
  fastcgi_pass unix:/run/php/php7.0-fpm.sock;
  include         fastcgi_params;
  fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}

Return a Message

message.conf
location ~ ^/ {
  default_type text/html;
  return 200 "response body here";
}

Rate Limiting

ratelimit.conf
# 1 request a second, using the site hostname instead of an IP (so it's 1r/s no matter the IP)
limit_req_zone "$http_host" zone=app_name:10m rate=1r/s;
 
# 5 requests a minute, per IP
limit_req_zone "$http_cf_connecting_ip" zone=app_name:10m rate=5r/m;
 
# "10m" = 10MB. Configures how large the rambuffer should be for storing requests.
 
# Use the correct status code for ratelimits
limit_conn_status 429;
limit_req_status 429;
 
server {
    # ...
 
    location ^~ /app {
        # Use the app_name zone
        limit_req zone=app_name;
 
        # Allow bursts
        limit_req zone=app_name burst=5;
    }
 
    # Custom error page if the req is being rate limited
    error_page 429 /ratelimit.html;
 
    # ...
}
/var/www/dokuwiki/data/pages/lab/nginx.txt · Last modified: 2021-05-18 17:09 by wiki@tomr.me