trimstray / nginx-admins-handbook

How to improve NGINX performance, security, and other important things.
MIT License
13.52k stars 1.09k forks source link
best-practices cheatsheet hacks handbook http https nginx nginx-configuration nginx-proxy notes openresty performance reference security snippets ssllabs tengine

Nginx Admin's Handbook

My notes on NGINX administration basics, tips & tricks, caveats, and gotchas.


Meme


Hi-diddle-diddle, he played on his
fiddle and danced with lady pigs.
Number three said, "Nicks on tricks!
I'll build my house with EN-jin-EKS!".
The Three Little Pigs: Who's Afraid of the Big Bad Wolf?


Pull Requests MIT License

Created by trimstray and contributors



Table of Contents

Other chapters
- **[HTTP Basics](doc/HTTP_BASICS.md#http-basics)** * [Introduction](doc/HTTP_BASICS.md#introduction-1) * [Features and architecture](doc/HTTP_BASICS.md#features-and-architecture) * [HTTP/2](doc/HTTP_BASICS.md#http2) * [How to debug HTTP/2?](doc/HTTP_BASICS.md#how-to-debug-http2) * [HTTP/3](doc/HTTP_BASICS.md#http3) * [URI vs URL](doc/HTTP_BASICS.md#uri-vs-url) * [Connection vs request](doc/HTTP_BASICS.md#connection-vs-request) * [HTTP Headers](doc/HTTP_BASICS.md#http-headers) * [Header compression](#header-compression) * [HTTP Methods](doc/HTTP_BASICS.md#http-methods) * [Request](doc/HTTP_BASICS.md#request) * [Request line](doc/HTTP_BASICS.md#request-line) * [Methods](doc/HTTP_BASICS.md#methods) * [Request URI](doc/HTTP_BASICS.md#request-uri) * [HTTP version](doc/HTTP_BASICS.md#http-version) * [Request header fields](doc/HTTP_BASICS.md#request-header-fields) * [Message body](doc/HTTP_BASICS.md#message-body) * [Generate requests](doc/HTTP_BASICS.md#generate-requests) * [Response](doc/HTTP_BASICS.md#response) * [Status line](doc/HTTP_BASICS.md#status-line) * [HTTP version](doc/HTTP_BASICS.md#http-version-1) * [Status codes and reason phrase](doc/HTTP_BASICS.md#status-codes-and-reason-phrase) * [Response header fields](doc/HTTP_BASICS.md#response-header-fields) * [Message body](doc/HTTP_BASICS.md#message-body-1) * [HTTP client](doc/HTTP_BASICS.md#http-client) * [IP address shortcuts](doc/HTTP_BASICS.md#ip-address-shortcuts) * [Back-End web architecture](doc/HTTP_BASICS.md#back-end-web-architecture) * [Useful video resources](doc/HTTP_BASICS.md#useful-video-resources) - **[SSL/TLS Basics](doc/SSL_TLS_BASICS.md#ssltls-basics)** * [Introduction](doc/SSL_TLS_BASICS.md#introduction-2) * [TLS versions](doc/SSL_TLS_BASICS.md#tls-versions) * [TLS handshake](doc/SSL_TLS_BASICS.md#tls-handshake) * [In which layer is TLS situated within the TCP/IP stack?](doc/SSL_TLS_BASICS.md#in-which-layer-is-tls-situated-within-the-tcpip-stack) * [RSA and ECC keys/certificates](doc/SSL_TLS_BASICS.md#rsa-and-ecc-keyscertificates) * [Cipher suites](doc/SSL_TLS_BASICS.md#cipher-suites) * [Authenticated encryption (AEAD) cipher suites](doc/SSL_TLS_BASICS.md#authenticated-encryption-aead-cipher-suites) * [Why cipher suites are important?](doc/SSL_TLS_BASICS.md#why-cipher-suites-are-important) * [What does insecure, weak, secure and recommended mean?](doc/SSL_TLS_BASICS.md#what-does-insecure-weak-secure-and-recommended-mean) * [NGINX and TLS 1.3 Cipher Suites](doc/SSL_TLS_BASICS.md#nginx-and-tls-13-cipher-suites) * [Diffie-Hellman key exchange](doc/SSL_TLS_BASICS.md#diffie-hellman-key-exchange) * [What exactly is the purpose of these DH Parameters?](doc/SSL_TLS_BASICS.md#what-exactly-is-the-purpose-of-these-dh-parameters) * [Certificates](doc/SSL_TLS_BASICS.md#certificates) * [Chain of Trust](doc/SSL_TLS_BASICS.md#chain-of-trust) * [What is the main purpose of the Intermediate CA?](doc/SSL_TLS_BASICS.md#what-is-the-main-purpose-of-the-intermediate-ca) * [Single-domain](doc/SSL_TLS_BASICS.md#single-domain) * [Multi-domain](doc/SSL_TLS_BASICS.md#multi-domain) * [Wildcard](doc/SSL_TLS_BASICS.md#wildcard) * [Wildcard SSL doesn't handle root domain?](doc/SSL_TLS_BASICS.md#wildcard-ssl-doesnt-handle-root-domain) * [HTTPS with self-signed certificate vs HTTP](doc/SSL_TLS_BASICS.md#https-with-self-signed-certificate-vs-http) * [TLS Server Name Indication](doc/SSL_TLS_BASICS.md#tls-server-name-indication) * [Verify your SSL, TLS & Ciphers implementation](doc/SSL_TLS_BASICS.md#verify-your-ssl-tls--ciphers-implementation) * [Useful video resources](doc/SSL_TLS_BASICS.md#useful-video-resources) - **[NGINX Basics](doc/NGINX_BASICS.md#nginx-basics)** * [Directories and files](doc/NGINX_BASICS.md#directories-and-files) * [Commands](doc/NGINX_BASICS.md#commands) * [Processes](doc/NGINX_BASICS.md#processes) * [CPU pinning](doc/NGINX_BASICS.md#cpu-pinning) * [Shutdown of worker processes](doc/NGINX_BASICS.md#shutdown-of-worker-processes) * [Configuration syntax](doc/NGINX_BASICS.md#configuration-syntax) * [Comments](doc/NGINX_BASICS.md#comments) * [End of lines](doc/NGINX_BASICS.md#end-of-lines) * [Variables, Strings, and Quotes](doc/NGINX_BASICS.md#variables-strings-and-quotes) * [Directives, Blocks, and Contexts](doc/NGINX_BASICS.md#directives-blocks-and-contexts) * [External files](doc/NGINX_BASICS.md#external-files) * [Measurement units](doc/NGINX_BASICS.md#measurement-units) * [Regular expressions with PCRE](doc/NGINX_BASICS.md#regular-expressions-with-pcre) * [Enable syntax highlighting](doc/NGINX_BASICS.md#enable-syntax-highlighting) * [Connection processing](doc/NGINX_BASICS.md#connection-processing) * [Event-Driven architecture](doc/NGINX_BASICS.md#event-driven-architecture) * [Multiple processes](doc/NGINX_BASICS.md#multiple-processes) * [Simultaneous connections](doc/NGINX_BASICS.md#simultaneous-connections) * [HTTP Keep-Alive connections](doc/NGINX_BASICS.md#http-keep-alive-connections) * [sendfile, tcp_nodelay, and tcp_nopush](doc/NGINX_BASICS.md#sendfile-tcp_nodelay-and-tcp_nopush) * [Request processing stages](doc/NGINX_BASICS.md#request-processing-stages) * [Server blocks logic](doc/NGINX_BASICS.md#server-blocks-logic) * [Handle incoming connections](doc/NGINX_BASICS.md#handle-incoming-connections) * [Matching location](doc/NGINX_BASICS.md#matching-location) * [rewrite vs return](doc/NGINX_BASICS.md#rewrite-vs-return) * [URL redirections](doc/NGINX_BASICS.md#url-redirections) * [try_files directive](doc/NGINX_BASICS.md#try_files-directive) * [if, break, and set](doc/NGINX_BASICS.md#if-break-and-set) * [root vs alias](doc/NGINX_BASICS.md#root-vs-alias) * [internal directive](doc/NGINX_BASICS.md#internal-directive) * [External and internal redirects](doc/NGINX_BASICS.md#external-and-internal-redirects) * [allow and deny](doc/NGINX_BASICS.md#allow-and-deny) * [uri vs request_uri](doc/NGINX_BASICS.md#uri-vs-request_uri) * [Compression and decompression](doc/NGINX_BASICS.md#compression-and-decompression) * [What is the best NGINX compression gzip level?](doc/NGINX_BASICS.md#what-is-the-best-nginx-compression-gzip-level) * [Hash tables](doc/NGINX_BASICS.md#hash-tables) * [Server names hash table](doc/NGINX_BASICS.md#server-names-hash-table) * [Log files](doc/NGINX_BASICS.md#log-files) * [Conditional logging](doc/NGINX_BASICS.md#conditional-logging) * [Manually log rotation](doc/NGINX_BASICS.md#manually-log-rotation) * [Error log severity levels](doc/NGINX_BASICS.md#error-log-severity-levels) * [How to log the start time of a request?](doc/NGINX_BASICS.md#how-to-log-the-start-time-of-a-request) * [How to log the HTTP request body?](doc/NGINX_BASICS.md#how-to-log-the-http-request-body) * [NGINX upstream variables returns 2 values](doc/NGINX_BASICS.md#nginx-upstream-variables-returns-2-values) * [Reverse proxy](doc/NGINX_BASICS.md#reverse-proxy) * [Passing requests](doc/NGINX_BASICS.md#passing-requests) * [Trailing slashes](doc/NGINX_BASICS.md#trailing-slashes) * [Passing headers to the backend](doc/NGINX_BASICS.md#passing-headers-to-the-backend) * [Importance of the Host header](doc/NGINX_BASICS.md#importance-of-the-host-header) * [Redirects and X-Forwarded-Proto](doc/NGINX_BASICS.md#redirects-and-x-forwarded-proto) * [A warning about the X-Forwarded-For](doc/NGINX_BASICS.md#a-warning-about-the-x-forwarded-for) * [Improve extensibility with Forwarded](doc/NGINX_BASICS.md#improve-extensibility-with-forwarded) * [Response headers](doc/NGINX_BASICS.md#response-headers) * [Load balancing algorithms](doc/NGINX_BASICS.md#load-balancing-algorithms) * [Backend parameters](doc/NGINX_BASICS.md#backend-parameters) * [Upstream servers with SSL](doc/NGINX_BASICS.md#upstream-servers-with-ssl) * [Round Robin](doc/NGINX_BASICS.md#round-robin) * [Weighted Round Robin](doc/NGINX_BASICS.md#weighted-round-robin) * [Least Connections](doc/NGINX_BASICS.md#least-connections) * [Weighted Least Connections](doc/NGINX_BASICS.md#weighted-least-connections) * [IP Hash](doc/NGINX_BASICS.md#ip-hash) * [Generic Hash](doc/NGINX_BASICS.md#generic-hash) * [Other methods](doc/NGINX_BASICS.md#other-methods) * [Rate limiting](doc/NGINX_BASICS.md#rate-limiting) * [Variables](doc/NGINX_BASICS.md#variables) * [Directives, keys, and zones](doc/NGINX_BASICS.md#directives-keys-and-zones) * [Burst and nodelay parameters](doc/NGINX_BASICS.md#burst-and-nodelay-parameters) * [NAXSI Web Application Firewall](doc/NGINX_BASICS.md#naxsi-web-application-firewall) * [OWASP ModSecurity Core Rule Set (CRS)](doc/NGINX_BASICS.md#owasp-modsecurity-core-rule-set-crs) * [Core modules](doc/NGINX_BASICS.md#core-modules) * [ngx_http_geo_module](doc/NGINX_BASICS.md#ngx_http_geo_module) * [3rd party modules](doc/NGINX_BASICS.md#3rd-party-modules) * [ngx_set_misc](doc/NGINX_BASICS.md#ngx_set_misc) * [ngx_http_geoip_module](doc/NGINX_BASICS.md#ngx_http_geoip_module) - **[Helpers](doc/HELPERS.md#helpers)** * [Installing from prebuilt packages](doc/HELPERS.md#installing-from-prebuilt-packages) * [RHEL7 or CentOS 7](doc/HELPERS.md#rhel7-or-centos-7) * [Debian or Ubuntu](doc/HELPERS.md#debian-or-ubuntu) * [FreeBSD](doc/HELPERS.md#freebsd) * [Installing from source](doc/HELPERS.md#installing-from-source) * [Automatic installation on RHEL/Debian/BSD](doc/HELPERS.md#automatic-installation-on-rheldebianbsd) * [Nginx package](doc/HELPERS.md#nginx-package) * [Dependencies](doc/HELPERS.md#dependencies) * [Patches](doc/HELPERS.md#patches) * [3rd party modules](doc/HELPERS.md#3rd-party-modules) * [Configure options](doc/HELPERS.md#cconfigure-options) * [Compiler and linker](doc/HELPERS.md#compiler-and-linker) * [Debugging Symbols](doc/HELPERS.md#debugging-symbols) * [SystemTap](doc/HELPERS.md#systemtap) * [stapxx](doc/HELPERS.md#stapxx) * [Installation Nginx on CentOS 7](doc/HELPERS.md#installation-nginx-on-centos-7) * [Pre installation tasks](doc/HELPERS.md#pre-installation-tasks) * [Dependencies](doc/HELPERS.md#dependencies) * [Get Nginx sources](doc/HELPERS.md#get-nginx-sources) * [Download 3rd party modules](doc/HELPERS.md#download-3rd-party-modules) * [Build Nginx](doc/HELPERS.md#build-nginx) * [Post installation tasks](doc/HELPERS.md#post-installation-tasks) * [Installation OpenResty on CentOS 7](doc/HELPERS.md#installation-openresty-on-centos-7) * [Installation Tengine on Ubuntu 18.04](doc/HELPERS.md#installation-tengine-on-ubuntu-1804) * [Installation Nginx on FreeBSD 11.3](doc/HELPERS.md#installation-nginx-on-freebsd-113) * [Installation Nginx on FreeBSD 11.3 (from ports)](doc/HELPERS.md#installation-nginx-on-freebsd-113-from-ports) * [Analyse configuration](doc/HELPERS.md#analyse-configuration) * [Monitoring](doc/HELPERS.md#monitoring) * [GoAccess](doc/HELPERS.md#goaccess) * [Build and install](doc/HELPERS.md#build-and-install) * [Analyse log file and enable all recorded statistics](doc/HELPERS.md#analyse-log-file-and-enable-all-recorded-statistics) * [Analyse compressed log file](doc/HELPERS.md#analyse-compressed-log-file) * [Analyse log file remotely](doc/HELPERS.md#analyse-log-file-remotely) * [Analyse log file and generate html report](doc/HELPERS.md#analyse-log-file-and-generate-html-report) * [Ngxtop](doc/HELPERS.md#ngxtop) * [Analyse log file](doc/HELPERS.md#analyse-log-file) * [Analyse log file and print requests with 4xx and 5xx](doc/HELPERS.md#analyse-log-file-and-print-requests-with-4xx-and-5xx) * [Analyse log file remotely](doc/HELPERS.md#analyse-log-file-remotely-1) * [Testing](doc/HELPERS.md#testing) * [Build OpenSSL 1.0.2-chacha version](doc/HELPERS.md#build-openssl-102-chacha-version) * [Send request and show response headers](doc/HELPERS.md#send-request-and-show-response-headers) * [Send request with http method, user-agent, follow redirects and show response headers](doc/HELPERS.md#send-request-with-http-method-user-agent-follow-redirects-and-show-response-headers) * [Send multiple requests](doc/HELPERS.md#send-multiple-requests) * [Testing SSL connection](doc/HELPERS.md#testing-ssl-connection) * [Testing SSL connection (debug mode)](doc/HELPERS.md#testing-ssl-connection-debug-mode) * [Testing SSL connection with SNI support](doc/HELPERS.md#testing-ssl-connection-with-sni-support) * [Testing SSL connection with specific SSL version](doc/HELPERS.md#testing-ssl-connection-with-specific-ssl-version) * [Testing SSL connection with specific cipher](doc/HELPERS.md#testing-ssl-connection-with-specific-cipher) * [Testing OCSP Stapling](doc/HELPERS.md#testing-ocsp-stapling) * [Verify 0-RTT](doc/HELPERS.md#verify-0-rtt) * [Testing SCSV](doc/HELPERS.md#testing-scsv) * [Load testing with ApacheBench (ab)](doc/HELPERS.md#load-testing-with-apachebench-ab) * [Standard test](doc/HELPERS.md#standard-test) * [Test with Keep-Alive header](doc/HELPERS.md#test-with-keep-alive-header) * [Load testing with wrk2](doc/HELPERS.md#load-testing-with-wrk2) * [Standard scenarios](doc/HELPERS.md#standard-scenarios) * [POST call (with Lua)](doc/HELPERS.md#post-call-with-lua) * [Random paths (with Lua)](doc/HELPERS.md#random-paths-with-lua) * [Multiple paths (with Lua)](doc/HELPERS.md#multiple-paths-with-lua) * [Random server address to each thread (with Lua)](doc/HELPERS.md#random-server-address-to-each-thread-with-lua) * [Multiple json requests (with Lua)](doc/HELPERS.md#multiple-json-requests-with-lua) * [Debug mode (with Lua)](doc/HELPERS.md#debug-mode-with-lua) * [Analyse data pass to and from the threads](doc/HELPERS.md#analyse-data-pass-to-and-from-the-threads) * [Parsing wrk result and generate report](doc/HELPERS.md#parsing-wrk-result-and-generate-report) * [Load testing with locust](doc/HELPERS.md#load-testing-with-locust) * [Multiple paths](doc/HELPERS.md#multiple-paths) * [Multiple paths with different user sessions](doc/HELPERS.md#multiple-paths-with-different-user-sessions) * [TCP SYN flood Denial of Service attack](doc/HELPERS.md#tcp-syn-flood-denial-of-service-attack) * [HTTP Denial of Service attack](doc/HELPERS.md#tcp-syn-flood-denial-of-service-attack) * [Debugging](doc/HELPERS.md#debugging) * [Show information about processes](doc/HELPERS.md#show-information-about-nginx-processes) * [Check memory usage](doc/HELPERS.md#check-memoryusage) * [Show open files](doc/HELPERS.md#show-open-files) * [Check segmentation fault messages](doc/HELPERS.md#check-segmentation-fault-messages) * [Dump configuration](doc/HELPERS.md#dump-configuration) * [Get the list of configure arguments](doc/HELPERS.md#get-the-list-of-configure-arguments) * [Check if the module has been compiled](doc/HELPERS.md#check-if-the-module-has-been-compiled) * [Show the most accessed IP addresses](doc/HELPERS.md#show-the-most-accessed-ip-addresses) * [Show the most accessed IP addresses (ip and url)](doc/HELPERS.md#show-the-most-accessed-ip-addresses-ip-and-url) * [Show the most accessed IP addresses (method, code, ip, and url)](doc/HELPERS.md#show-the-most-accessed-ip-addresses-method-code-ip-and-url) * [Show the top 5 visitors (IP addresses)](doc/HELPERS.md#show-the-top-5-visitors-ip-addresses) * [Show the most requested urls](doc/HELPERS.md#show-the-most-requested-urls) * [Show the most requested urls containing 'string'](doc/HELPERS.md#show-the-most-requested-urls-containing-string) * [Show the most requested urls with http methods](doc/HELPERS.md#show-the-most-requested-urls-with-http-methods) * [Show the most accessed response codes](doc/HELPERS.md#show-the-most-accessed-response-codes) * [Analyse web server log and show only 2xx http codes](doc/HELPERS.md#analyse-web-server-log-and-show-only-2xx-http-codes) * [Analyse web server log and show only 5xx http codes](doc/HELPERS.md#analyse-web-server-log-and-show-only-5xx-http-codes) * [Show requests which result 502 and sort them by number per requests by url](doc/HELPERS.md#show-requests-which-result-502-and-sort-them-by-number-per-requests-by-url) * [Show requests which result 404 for php files and sort them by number per requests by url](doc/HELPERS.md#show-requests-which-result-404-for-php-files-and-sort-them-by-number-per-requests-by-url) * [Calculating amount of http response codes](doc/HELPERS.md#calculating-amount-of-http-response-codes) * [Calculating requests per second](doc/HELPERS.md#calculating-requests-per-second) * [Calculating requests per second with IP addresses](doc/HELPERS.md#calculating-requests-per-second-with-ip-addresses) * [Calculating requests per second with IP addresses and urls](doc/HELPERS.md#calculating-requests-per-second-with-ip-addresses-and-urls) * [Get entries within last n hours](doc/HELPERS.md#get-entries-within-last-n-hours) * [Get entries between two timestamps (range of dates)](doc/HELPERS.md#get-entries-between-two-timestamps-range-of-dates) * [Get line rates from web server log](doc/HELPERS.md#get-line-rates-from-web-server-log) * [Trace network traffic for all processes](doc/HELPERS.md#trace-network-traffic-for-all-nginx-processes) * [List all files accessed by a NGINX](doc/HELPERS.md#list-all-files-accessed-by-a-nginx) * [Check that the gzip_static module is working](doc/HELPERS.md#check-that-the-gzip_static-module-is-working) * [Which worker processing current request](doc/HELPERS.md#which-worker-processing-current-request) * [Capture only http packets](doc/HELPERS.md#capture-only-http-packets) * [Extract User Agent from the http packets](doc/HELPERS.md#extract-user-agent-from-the-http-packets) * [Capture only http GET and POST packets](doc/HELPERS.md#capture-only-http-get-and-post-packets) * [Capture requests and filter by source ip and destination port](doc/HELPERS.md#capture-requests-and-filter-by-source-ip-and-destination-port) * [Capture HTTP requests/responses in real time, filter by GET, HEAD and save to a file](doc/HELPERS.md#capture-http-requests--responses-in-real-time-filter-by-get-head-and-save-to-a-file) * [Dump a process's memory](doc/HELPERS.md#dump-a-processs-memory) * [GNU Debugger (gdb)](doc/HELPERS.md#gnu-debugger-gdb) * [Dump configuration from a running process](doc/HELPERS.md#dump-configuration-from-a-running-process) * [Show debug log in memory](doc/HELPERS.md#show-debug-log-in-memory) * [Core dump backtrace](doc/HELPERS.md#core-dump-backtrace) * [Debugging socket leaks](doc/HELPERS.md#debugging-socket-leaks) * [Shell aliases](doc/HELPERS.md#shell-aliases) * [Configuration snippets](doc/HELPERS.md#configuration-snippets) * [Nginx server header removal](doc/HELPERS.md#nginx-server-header-removal) * [Custom log formats](doc/HELPERS.md#custom-log-formats) * [Log only 4xx/5xx](doc/HELPERS.md#log-only-4xx5xx) * [Restricting access with basic authentication](doc/HELPERS.md#restricting-access-with-basic-authentication) * [Restricting access with client certificate](doc/HELPERS.md#restricting-access-with-client-certificate) * [Restricting access by geographical location](doc/HELPERS.md#restricting-access-by-geographical-location) * [GeoIP 2 database](doc/HELPERS.md#geoip-2-database) * [Dynamic error pages with SSI](doc/HELPERS.md#dynamic-error-pages-with-ssi) * [Blocking/allowing IP addresses](doc/HELPERS.md#blockingallowing-ip-addresses) * [Blocking referrer spam](doc/HELPERS.md#blocking-referrer-spam) * [Limiting referrer spam](doc/HELPERS.md#limiting-referrer-spam) * [Blocking User-Agent](doc/HELPERS.md#blocking-user-agent) * [Limiting User-Agent](doc/HELPERS.md#limiting-user-agent) * [Limiting the rate of requests with burst mode](doc/HELPERS.md#limiting-the-rate-of-requests-with-burst-mode) * [Limiting the rate of requests with burst mode and nodelay](doc/HELPERS.md#limiting-the-rate-of-requests-with-burst-mode-and-nodelay) * [Limiting the rate of requests per IP with geo and map](doc/HELPERS.md#limiting-the-rate-of-requests-per-ip-with-geo-and-map) * [Limiting the number of connections](doc/HELPERS.md#limiting-the-number-of-connections) * [Using trailing slashes](doc/HELPERS.md#using-trailing-slashes) * [Properly redirect all HTTP requests to HTTPS](doc/HELPERS.md#properly-redirect-all-http-requests-to-https) * [Adding and removing the www prefix](doc/HELPERS.md#adding-and-removing-the-www-prefix) * [Proxy/rewrite and keep the original URL](doc/HELPERS.md#proxyrewrite-and-keep-the-original-url) * [Proxy/rewrite and keep the part of original URL](doc/HELPERS.md#proxyrewrite-and-keep-the-part-of-original-url) * [Proxy/rewrite without changing the original URL (in browser)](doc/HELPERS.md#proxyrewrite-without-changing-the-original-url-in-browser) * [Modify 301/302 response body](doc/HELPERS.md#modify-301302-response-body) * [Redirect POST request with payload to external endpoint](doc/HELPERS.md#redirect-post-request-with-payload-to-external-endpoint) * [Route to different backends based on HTTP method](doc/HELPERS.md#route-to-different-backends-based-on-HTTP-method) * [Allow multiple cross-domains using the CORS headers](doc/HELPERS.md#allow-multiple-cross-domains-using-the-cors-headers) * [Set correct scheme passed in X-Forwarded-Proto](doc/HELPERS.md#set-correct-scheme-passed-in-x-forwarded-proto) * [Other snippets](doc/HELPERS.md#other-snippets) * [Recreate base directory](doc/HELPERS.md#recreate-base-directory) * [Create a temporary static backend](doc/HELPERS.md#create-a-temporary-static-backend) * [Create a temporary static backend with SSL support](doc/HELPERS.md#create-a-temporary-static-backend-with-ssl-support) * [Generate password file with htpasswd command](doc/HELPERS.md#generate-password-file-with-htpasswd-command) * [Generate private key without passphrase](doc/HELPERS.md#generate-private-key-without-passphrase) * [Generate private key with passphrase](doc/HELPERS.md#generate-private-key-with-passphrase) * [Remove passphrase from private key](doc/HELPERS.md#remove-passphrase-from-private-key) * [Encrypt existing private key with a passphrase](doc/HELPERS.md#encrypt-existing-private-key-with-a-passphrase) * [Generate CSR](doc/HELPERS.md#generate-csr) * [Generate CSR (metadata from existing certificate)](doc/HELPERS.md#generate-csr-metadata-from-existing-certificate) * [Generate CSR with -config param](doc/HELPERS.md#generate-csr-with--config-param) * [Generate private key and CSR](doc/HELPERS.md#generate-private-key-and-csr) * [List available EC curves](doc/HELPERS.md#list-available-ec-curves) * [Print ECDSA private and public keys](doc/HELPERS.md#print-ecdsa-private-and-public-keys) * [Generate ECDSA private key](doc/HELPERS.md#generate-ecdsa-private-key) * [Generate private key and CSR (ECC)](doc/HELPERS.md#generate-private-key-and-csr-ecc) * [Generate self-signed certificate](doc/HELPERS.md#generate-self-signed-certificate) * [Generate self-signed certificate from existing private key](doc/HELPERS.md#generate-self-signed-certificate-from-existing-private-key) * [Generate self-signed certificate from existing private key and csr](doc/HELPERS.md#generate-self-signed-certificate-from-existing-private-key-and-csr) * [Generate multidomain certificate (Certbot)](doc/HELPERS.md#generate-multidomain-certificate-certbot) * [Generate wildcard certificate (Certbot)](doc/HELPERS.md#generate-wildcard-certificate-certbot) * [Generate certificate with 4096 bit private key (Certbot)](doc/HELPERS.md#generate-certificate-with-4096-bit-private-key-certbot) * [Generate DH public parameters](doc/HELPERS.md#generate-dh-public-parameters) * [Display DH public parameters](doc/HELPERS.md#display-dh-public-parameters) * [Extract private key from pfx](doc/HELPERS.md#extract-private-key-from-pfx) * [Extract private key and certs from pfx](doc/HELPERS.md#extract-private-key-and-certs-from-pfx) * [Extract certs from p7b](doc/HELPERS.md#extract-certs-from-p7b) * [Convert DER to PEM](doc/HELPERS.md#convert-der-to-pem) * [Convert PEM to DER](doc/HELPERS.md#convert-pem-to-der) * [Verification of the certificate's supported purposes](doc/HELPERS.md#verification-of-the-certificates-supported-purposes) * [Check private key](doc/HELPERS.md#check-private-key) * [Verification of the private key](doc/HELPERS.md#verification-of-the-private-key) * [Get public key from private key](doc/HELPERS.md#get-public-key-from-private-key) * [Verification of the public key](doc/HELPERS.md#verification-of-the-public-key) * [Verification of the certificate](doc/HELPERS.md#verification-of-the-certificate) * [Verification of the CSR](doc/HELPERS.md#verification-of-the-csr) * [Check the private key and the certificate are match](doc/HELPERS.md#check-the-private-key-and-the-certificate-are-match) * [Check the private key and the CSR are match](doc/HELPERS.md#check-the-private-key-and-the-csr-are-match) [TLSv1.3 and CCM ciphers](doc/HELPERS.md#tlsv13-and-ccm-ciphers) - **[Base Rules (16)](doc/RULES.md#base-rules)** * [Organising Nginx configuration](doc/RULES.md#beginner-organising-nginx-configuration) * [Format, prettify and indent your Nginx code](doc/RULES.md#beginner-format-prettify-and-indent-your-nginx-code) * [Use reload option to change configurations on the fly](doc/RULES.md#beginner-use-reload-option-to-change-configurations-on-the-fly) * [Separate listen directives for 80 and 443 ports](doc/RULES.md#beginner-separate-listen-directives-for-80-and-443-ports) * [Define the listen directives with address:port pair](doc/RULES.md#beginner-define-the-listen-directives-with-addressport-pair) * [Prevent processing requests with undefined server names](doc/RULES.md#beginner-prevent-processing-requests-with-undefined-server-names) * [Never use a hostname in a listen or upstream directives](doc/RULES.md#beginner-never-use-a-hostname-in-a-listen-or-upstream-directives) * [Set the HTTP headers with add_header and proxy_*_header directives properly](doc/RULES.md#beginner-set-the-http-headers-with-add_header-and-proxy__header-directives-properly) * [Use only one SSL config for the listen directive](doc/RULES.md#beginner-use-only-one-ssl-config-for-the-listen-directive) * [Use geo/map modules instead of allow/deny](doc/RULES.md#beginner-use-geomap-modules-instead-of-allowdeny) * [Map all the things...](doc/RULES.md#beginner-map-all-the-things) * [Set global root directory for unmatched locations](doc/RULES.md#beginner-set-global-root-directory-for-unmatched-locations) * [Use return directive for URL redirection (301, 302)](doc/RULES.md#beginner-use-return-directive-for-url-redirection-301-302) * [Configure log rotation policy](doc/RULES.md#beginner-configure-log-rotation-policy) * [Use simple custom error pages](doc/RULES.md#beginner-use-simple-custom-error-pages) * [Don't duplicate index directive, use it only in the http block](doc/RULES.md#beginner-dont-duplicate-index-directive-use-it-only-in-the-http-block) - **[Debugging (5)](doc/RULES.md#debugging)** * [Use custom log formats](doc/RULES.md#beginner-use-custom-log-formats) * [Use debug mode to track down unexpected behaviour](doc/RULES.md#beginner-use-debug-mode-to-track-down-unexpected-behaviour) * [Improve debugging by disable daemon, master process, and all workers except one](doc/RULES.md#beginner-improve-debugging-by-disable-daemon-master-process-and-all-workers-except-one) * [Use core dumps to figure out why NGINX keep crashing](doc/RULES.md#beginner-use-core-dumps-to-figure-out-why-nginx-keep-crashing) * [Use mirror module to copy requests to another backend](doc/RULES.md#beginner-use-mirror-module-to-copy-requests-to-another-backend) - **[Performance (13)](doc/RULES.md#performance)** * [Adjust worker processes](doc/RULES.md#beginner-adjust-worker-processes) * [Use HTTP/2](doc/RULES.md#beginner-use-http2) * [Maintaining SSL sessions](doc/RULES.md#beginner-maintaining-ssl-sessions) * [Enable OCSP Stapling](doc/RULES.md#beginner-enable-ocsp-stapling) * [Use exact names in a server_name directive if possible](doc/RULES.md#beginner-use-exact-names-in-a-server_name-directive-if-possible) * [Avoid checks server_name with if directive](doc/RULES.md#beginner-avoid-checks-server_name-with-if-directive) * [Use $request_uri to avoid using regular expressions](doc/RULES.md#beginner-use-request_uri-to-avoid-using-regular-expressions) * [Use try_files directive to ensure a file exists](doc/RULES.md#beginner-use-try_files-directive-to-ensure-a-file-exists) * [Use return directive instead of rewrite for redirects](doc/RULES.md#beginner-use-return-directive-instead-of-rewrite-for-redirects) * [Enable PCRE JIT to speed up processing of regular expressions](doc/RULES.md#beginner-enable-pcre-jit-to-speed-up-processing-of-regular-expressions) * [Activate the cache for connections to upstream servers](doc/RULES.md#beginner-activate-the-cache-for-connections-to-upstream-servers) * [Make an exact location match to speed up the selection process](doc/RULES.md#beginner-make-an-exact-location-match-to-speed-up-the-selection-process) * [Use limit_conn to improve limiting the download speed](doc/RULES.md#beginner-use-limit_conn-to-improve-limiting-the-download-speed) - **[Hardening (31)](doc/RULES.md#hardening)** * [Always keep NGINX up-to-date](doc/RULES.md#beginner-always-keep-nginx-up-to-date) * [Run as an unprivileged user](doc/RULES.md#beginner-run-as-an-unprivileged-user) * [Disable unnecessary modules](doc/RULES.md#beginner-disable-unnecessary-modules) * [Protect sensitive resources](doc/RULES.md#beginner-protect-sensitive-resources) * [Take care about your ACL rules](doc/RULES.md#beginner-take-care-about-your-acl-rules) * [Hide Nginx version number](doc/RULES.md#beginner-hide-nginx-version-number) * [Hide Nginx server signature](doc/RULES.md#beginner-hide-nginx-server-signature) * [Hide upstream proxy headers](doc/RULES.md#beginner-hide-upstream-proxy-headers) * [Remove support for legacy and risky HTTP request headers](doc/RULES.md#beginner-remove-support-for-legacy-and-risky-http-request-headers) * [Use only the latest supported OpenSSL version](doc/RULES.md#beginner-use-only-the-latest-supported-openssl-version) * [Force all connections over TLS](doc/RULES.md#beginner-force-all-connections-over-tls) * [Use min. 2048-bit for RSA and 256-bit for ECC](doc/RULES.md#beginner-use-min-2048-bit-for-rsa-and-256-bit-for-ecc) * [Keep only TLS 1.3 and TLS 1.2](doc/RULES.md#beginner-keep-only-tls-13-and-tls-12) * [Use only strong ciphers](doc/RULES.md#beginner-use-only-strong-ciphers) * [Use more secure ECDH Curve](doc/RULES.md#beginner-use-more-secure-ecdh-curve) * [Use strong Key Exchange with Perfect Forward Secrecy](doc/RULES.md#beginner-use-strong-key-exchange-with-perfect-forward-secrecy) * [Prevent Replay Attacks on Zero Round-Trip Time](doc/RULES.md#beginner-prevent-replay-attacks-on-zero-round-trip-time) * [Defend against the BEAST attack](doc/RULES.md#beginner-defend-against-the-beast-attack) * [Mitigation of CRIME/BREACH attacks](doc/RULES.md#beginner-mitigation-of-crimebreach-attacks) * [Enable HTTP Strict Transport Security](doc/RULES.md#beginner-enable-http-strict-transport-security) * [Reduce XSS risks (Content-Security-Policy)](doc/RULES.md#beginner-reduce-xss-risks-content-security-policy) * [Control the behaviour of the Referer header (Referrer-Policy)](doc/RULES.md#beginner-control-the-behaviour-of-the-referer-header-referrer-policy) * [Provide clickjacking protection (X-Frame-Options)](doc/RULES.md#beginner-provide-clickjacking-protection-x-frame-options) * [Prevent some categories of XSS attacks (X-XSS-Protection)](doc/RULES.md#beginner-prevent-some-categories-of-xss-attacks-x-xss-protection) * [Prevent Sniff Mimetype middleware (X-Content-Type-Options)](doc/RULES.md#beginner-prevent-sniff-mimetype-middleware-x-content-type-options) * [Deny the use of browser features (Feature-Policy)](doc/RULES.md#beginner-deny-the-use-of-browser-features-feature-policy) * [Reject unsafe HTTP methods](doc/RULES.md#beginner-reject-unsafe-http-methods) * [Prevent caching of sensitive data](doc/RULES.md#beginner-prevent-caching-of-sensitive-data) * [Limit concurrent connections](doc/RULES.md#beginner-limit-concurrent-connections) * [Control Buffer Overflow attacks](doc/RULES.md#beginner-control-buffer-overflow-attacks) * [Mitigating Slow HTTP DoS attacks (Closing Slow Connections)](doc/RULES.md#beginner-mitigating-slow-http-dos-attacks-closing-slow-connections) - **[Reverse Proxy (8)](doc/RULES.md#reverse-proxy)** * [Use pass directive compatible with backend protocol](doc/RULES.md#beginner-use-pass-directive-compatible-with-backend-protocol) * [Be careful with trailing slashes in proxy_pass directive](doc/RULES.md#beginner-be-careful-with-trailing-slashes-in-proxy_pass-directive) * [Set and pass Host header only with $host variable](doc/RULES.md#beginner-set-and-pass-host-header-only-with-host-variable) * [Set properly values of the X-Forwarded-For header](doc/RULES.md#beginner-set-properly-values-of-the-x-forwarded-for-header) * [Don't use X-Forwarded-Proto with $scheme behind reverse proxy](doc/RULES.md#beginner-dont-use-x-forwarded-proto-with-scheme-behind-reverse-proxy) * [Always pass Host, X-Real-IP, and X-Forwarded headers to the backend](doc/RULES.md#beginner-always-pass-host-x-real-ip-and-x-forwarded-headers-to-the-backend) * [Use custom headers without X- prefix](doc/RULES.md#beginner-use-custom-headers-without-x--prefix) * [Always use $request_uri instead of $uri in proxy_pass](doc/RULES.md#beginner-always-use-request_uri-instead-of-uri-in-proxy_pass) - **[Load Balancing (2)](doc/RULES.md#load-balancing)** * [Tweak passive health checks](doc/RULES.md#beginner-tweak-passive-health-checks) * [Don't disable backends by comments, use down parameter](doc/RULES.md#beginner-dont-disable-backends-by-comments-use-down-parameter) - **[Others (4)](doc/RULES.md#others)** * [Set the certificate chain correctly](doc/RULES.md#beginner-set-the-certificate-chain-correctly) * [Enable DNS CAA Policy](doc/RULES.md#beginner-enable-dns-caa-policy) * [Define security policies with security.txt](doc/RULES.md#beginner-define-security-policies-with-securitytxt) * [Use tcpdump to diagnose and troubleshoot the HTTP issues](doc/RULES.md#beginner-use-tcpdump-to-monitor-http-traffic) - **[Configuration Examples](doc/EXAMPLES.md#configuration-examples)** * [Reverse Proxy](doc/EXAMPLES.md#reverse-proxy) * [Installation](doc/EXAMPLES.md#installation) * [Configuration](doc/EXAMPLES.md#configuration) * [Import configuration](doc/EXAMPLES.md#import-configuration) * [Set bind IP address](doc/EXAMPLES.md#set-bind-ip-address) * [Set your domain name](doc/EXAMPLES.md#set-your-domain-name) * [Regenerate private keys and certs](doc/EXAMPLES.md#regenerate-private-keys-and-certs) * [Update modules list](doc/EXAMPLES.md#update-modules-list) * [Generating the necessary error pages](doc/EXAMPLES.md#generating-the-necessary-error-pages) * [Add new domain](doc/EXAMPLES.md#add-new-domain) * [Test your configuration](doc/EXAMPLES.md#test-your-configuration)

Introduction



Before you start playing with NGINX please read an official Beginner’s Guide. It's a great introduction for everyone.

Nginx (/ˌɛndʒɪnˈɛks/ EN-jin-EKS, stylized as NGINX or nginx) is an open source HTTP and reverse proxy server, a mail proxy server, and a generic TCP/UDP proxy server with a strong focus on high concurrency, performance and low memory usage. It is originally written by Igor Sysoev.

For a long time, it has been running on many heavily loaded Russian sites including Yandex, Mail.Ru, VK, and Rambler. At this moment some high-profile companies using NGINX include Cisco, DuckDuckGo, Facebook, GitLab, Google, Twitter, Apple, Intel, and many more. In the September 2019 it was the most commonly used HTTP server (see Netcraft survey).

NGINX is a fast, light-weight and powerful web server that can also be used as a:

So, to be brief, it provides the core of complete web stacks and is designed to help build scalable web applications. When it comes to performance, NGINX can easily handle a huge amount of traffic. The other main advantage of the NGINX is that allows you to do the same thing in different ways.

Unlike traditional HTTP servers, NGINX doesn't rely on threads to handle requests and it was written with a different architecture in mind - one which is much more suitable for nonlinear scalability in both the number of simultaneous connections and requests per second.

NGINX is also known as a Apache Killer (mainly because of its lightness and much less RAM consumption). It is event-based, so it does not follow Apache's style of spawning new processes or threads for each web page request. Generally, it was created to solve the C10K problem.

For me, it is a one of the best and most important service that I used in my SysAdmin career.


These essential documents should be the main source of knowledge for you:

In addition, I would like to recommend three great docs focuses on the concept of the HTTP protocol:

If you love security keep your eye on this one: Cryptology ePrint Archive. It provides access to recent research in cryptology and explores many subjects of security (e.g. Ciphers, Algorithms, SSL/TLS protocols). A great introduction that covers core concepts of cryptography is Practical Cryptography for Developers. I also recommend to read the Bulletproof SSL and TLS. Yep, it's definitely the most comprehensive book about deploying TLS for me.

An obligatory source of knowledge is also the OWASP Cheat Sheet Series. You should ought treat it as an excellent security guidance. Burp Scanner - Issue Definitions introduces you to the web apps and security vulnerabilities. Finally, The Web Security Academy is a free online training center for web application security with high-quality reading materials and interactive labs of varying levels of difficulty. All are really good source to start learning about web application security.

And, of course, always browse official Nginx Security Advisories and CVE databases like CVE Details or CVE - The MITRE Corporation - to stay Up-to-Date on NGINX vulnerabilities.

Prologue

When I was studying architecture of HTTP servers I became interested in NGINX. As I was going through research, I kept notes. I found a lot of information about it, e.g. forum posts on the web about every conceivable problem was great. However, I've never found one guide that covers the most important things in a suitable form. I was a little disappointed.

I was interested in everything: NGINX internals, functions, security best practices, performance optimisations, tips & tricks, hacks and rules, but for me some of the documents treated the subject lightly.

Of course, NGINX Official Documentation is the best place but I know that we also have other great resources:

These are definitely the best assets for us and in the first place you should seek help there. Moreover, in order to improve your knowledge, please see Books chapter - it contains top literature on NGINX.

Why I created this handbook

For me, however, there hasn't been a truly in-depth and reasonably simple cheatsheet which describe a variety of configurations and important cross-cutting topics for HTTP servers. Configuration of the NGINX can be tricky sometimes and you really need to get into the syntax and concepts to get an understanding tricks, loopholes, and mechanisms. The documentation isn't as pretty as other projects and should certainly include more robust examples.

This handbook is a set of rules and recommendations for the NGINX Open Source HTTP server. It also contains the best practices, notes, and helpers with countless examples. Many of them refer to external resources.

There are a lot of things you can do to improve in your NGINX instance and this guide will attempt to cover as many of them as possible. For the most part, it contains the most important things about NGINX for me. I think the configuration you provided should work without any talisman. That's why I created this repository.

With this handbook you will explore the many features and capabilities of the NGINX. You'll find out, for example, how to testing the performance or how to resolve debugging problems. You will learn configuration guidelines, security design patterns, ways to handle common issues and how to stay out of them. I explained here a few best tips to avoid pitfalls and configuration mistakes.

I added set of guidelines and examples has also been produced to help you administer of the NGINX. They give us insight into NGINX internals also.

Mostly, I apply the rules presented here on the NGINX working as a reverse proxy. However, does not to prevent them being implemented for NGINX as a standalone server.

Who this handbook is for

If you do not have the time to read hundreds of articles (just like me) this multipurpose handbook may be useful. I created it in the hope that it will be useful especially for System Administrators and Experts of Web-based applications.

This handbook does not get into all aspects of NGINX. What's more, some of the things described in this guide may be rather basic because most of us do not configure NGINX every day and it is easy to forget about basic/trivial things. On the other hand, also discusses heavyweight topics so there is something for advanced users. I tried to put external resources in many places in this handbook in order to dispel any suspicion that may exist.

I did my best to make this handbook a single and consistent (but now I know that is really hard). It's organized in an order that makes logical sense to me. I think it can also be a good complement to official documentation and other great documents. Many of the topics described here can certainly be done better or different. Of course, I still have a lot to improve and to do. I hope you enjoy and have fun with it.

Do not treat this handbook and notes written here as revealed knowledge. You should take a scientific approach when reading this document. If you have any doubts and disagree with me, please point out my mistakes. You should to discover cause and effect relationships by asking questions, carefully gathering and examining the evidence, and seeing if all the available information can be combined in to a logical answer.

I create this handbook for one more reason. Rather than starting from scratch in, I putting together a plan for answering your questions to help you find the best way to do things and ensure that you don't repeat my mistakes from the past.

So, what's most important:

Finally, you should know I'm not a NGINX expert but I love to know how stuff works and why work the way they do. I’m not a crypto expert... but I do know the term "elliptic curve" (I really like this quote!). Don't need to be an expert to figure out the reason just got to have used this and not this or why something works this way and not another. It feels good to understand the recommendations and nuances of a topic you’re passionate about.

Before you start

Remember about the following most important things:

Blindly deploying of the rules described here can damage your web application!

Do not follow guides just to get 100% of something. Think about what you actually do at your server!

Copy-and-paste is not the best way to learn. Think twice before adopting rules from this handbook.

There are no settings that are perfect for everyone.

Always think about what is better and more important for you: security vs usability/compatibility.

Security mainly refers to minimise the risk.

Change one thing may open a whole new set of problems.

Read about how things work and what values are considered secure enough (and for what purposes).

The only correct approach is to understand your exposure, measure and tune.

+ Security is important for ethical reasons. Compliance is important for legal reasons.
+ The key to workplace contentment is understanding they are unrelated to each other.
+ Both are important, but one does not lead to the other (compliance != security).
author: unknown

+ Security is always needed, no matter what type of website it is. It can be static HTML
+ or fully dynamic, an attacker can still inject hostile content into the page in transit
+ to attack the user.
author: Scott Helme

+ Don’t enable older deprecated protocols just because Karen in Florida is still using
+ a PC that she bought back in 2001.
author: thisinterestsmeblog

I think, in the age of phishing, cyber attacks, ransomware, etc., you should take care of security of your infrastructure as hard as possible but don't ever forget about this one...


Lastly, I would like to quote two very important comments found on the web about compliance with the standards and regulations, and essence of a human factor in security:

Regulations that make sense are often not descriptive - capturing the intent and scope of a rule often requires technical expertise. More than that, it's the type of expertise most organisations do not have. And instead of improving themselves, these companies, who may form the grand majority of the industry, petition the regulators to provide a safe checklist of technical mitigations that can be implemented to remain compliant. [...] Instead of doing the right thing and meeting the planned intent, companies are instead ticking nonsensical boxes that the regulators and their auditors demand. Blindly. Mindlessly. Divorced from reality. - by bostik

Whenever considering security, the human factor is nearly always as important or more important than just the technical aspects. Policy and procedures need to consider the human element and try to ensure that these policies and procedures are structured in such a way as to help enable staff to do the right thing, even when they may not fully understand why they need to do it. - by Tim X

Contributing & Support

A real community, however, exists only when its members interact in a meaningful way that deepens their understanding of each other and leads to learning.

If you find something which doesn't make sense, or something doesn't seem right, please make a pull request and please add valid and well-reasoned explanations about your changes or comments.

Before adding a pull request, please see the contributing guidelines.

Code Contributors

This project exists thanks to all the people who contribute.

ToDo

What needs to be done? Look at the following ToDo list:

New chapters:

Existing chapters:

Introduction
- [x] _Prologue_ - [x] _Why I created this handbook_ - [x] _Who this handbook is for_ - [x] _Before you start_ - [x] _Contributing & Support_ - [x] _RSS Feed & Updates - [x] _Checklist to rule them all_
Bonus Stuff
- [x] _Fully automatic installation_ - [x] _Static error pages generator_ - [x] _Server names parser_
Books
- [x] _ModSecurity 3.0 and NGINX: Quick Start Guide_ - [x] _Cisco ACE to NGINX: Migration Guide_
External Resources
- _Nginx official_ - [x] _Nginx Forum_ - [x] _Nginx Mailing List_ - [x] _NGINX-Demos_ - _Presentations & Videos_ - [x] _NGINX: Basics and Best Practices_ - [x] _NGINX Installation and Tuning_ - [x] _Nginx Internals (by Joshua Zhu)_ - [x] _Nginx internals (by Liqiang Xu)_ - [x] _How to secure your web applications with NGINX_ - [x] _Tuning TCP and NGINX on EC2_ - [x] _Extending functionality in nginx, with modules!_ - [x] _Nginx - Tips and Tricks._ - [x] _Nginx Scripting - Extending Nginx Functionalities with Lua_ - [x] _How to handle over 1,200,000 HTTPS Reqs/Min_ - [x] _Using ngx_lua / lua-nginx-module in pixiv_ - _Cheatsheets & References_ - [x] _Nginx configurations for most popular CMS/CMF/Frameworks based on PHP_ - _Performance & Hardening_ - [x] _Memorable site for testing clients against bad SSL configs_ - _Config parsers_ - [x] _Quick and reliable way to convert NGINX configurations into JSON and back_ - [x] _Parses nginx configuration with Pyparsing_ - _Config managers_ - [x] _Ansible role to install and manage nginx configuration_ - [x] _Ansible Role - Nginx_ - [x] _Ansible role for NGINX_ - [x] _Puppet Module to manage NGINX on various UNIXes_ - _Static analyzers_ - [x] _nginx-minify-conf_ - _Comparison reviews_ - [x] _NGINX vs. Apache (Pro/Con Review, Uses, & Hosting for Each)_ - [x] _Web cache server performance benchmark: nuster vs nginx vs varnish vs squid_ - _Builder tools_ - [x] _Nginx-builder_ - _Benchmarking tools_ - [x] _wrk2_ - [x] _httperf_ - [x] _slowloris_ - [x] _slowhttptest_ - [x] _GoldenEye_ - _Debugging tools_ - [x] _strace_ - [x] _GDB_ - [x] _SystemTap_ - [x] _stapxx_ - [x] _htrace.sh_ - _Security & Web testing tools_ - [x] _Burp Suite_ - [x] _w3af_ - [x] _nikto_ - [x] _ssllabs-scan_ - [x] _http-observatory_ - [x] _testssl.sh_ - [x] _sslyze_ - [x] _cipherscan_ - [x] _O-Saft_ - [x] _Nghttp2_ - [x] _h2spec_ - [x] _http2fuzz_ - [x] _Arjun_ - [x] _Corsy_ - [x] _XSStrike_ - _Online & Web tools_ - [x] _ssltools_ - _Other stuff_ - [x] _OWASP Cheat Sheet Series_ - [x] _Mozilla Web Security_ - [x] _Application Security Wiki_ - [x] _OWASP ASVS 4.0_ - [x] _The System Design Primer_ - [x] _awesome-scalability_ - [x] _Web Architecture 101_
HTTP Basics
- [x] _Features and architecture_ - [x] _HTTP/2_ - [x] _How to debug HTTP/2?_ - [x] _HTTP/3_ - [x] _URI vs URL_ - [x] _Connection vs request_ - [x] _HTTP Headers_ - [x] _Header compression_ - [x] _HTTP Methods_ - [x] _Request_ - [x] _Request line_ - [x] _Methods_ - [x] _Request URI_ - [x] _HTTP version_ - [x] _Request header fields_ - [x] _Message body_ - [x] _Generate requests_ - [x] _Response_ - [x] _Status line_ - [x] _HTTP version_ - [x] _Status codes and reason phrase_ - [x] _Response header fields_ - [x] _Message body_ - [x] _HTTP client_ - [x] _IP address shortcuts_ - [x] _Back-End web architecture_ - [x] _Useful video resources_
SSL/TLS Basics
- [x] _TLS versions_ - [x] _TLS handshake_ - [x] _In which layer is TLS situated within the TCP/IP stack?_ - [x] _RSA and ECC keys/certificates_ - [x] _Cipher suites_ - [x] _Authenticated encryption (AEAD) cipher suites_ - [x] _Why cipher suites are important?_ - [x] _NGINX and TLS 1.3 Cipher Suites_ - [x] _Diffie-Hellman key exchange_ - [x] _Certificates_ - [x] _Chain of Trust_ - [x] _What is the main purpose of the Intermediate CA?_ - [x] _Single-domain_ - [x] _Multi-domain_ - [x] _Wildcard_ - [x] _Wildcard SSL doesn't handle root domain?_ - [x] _TLS Server Name Indication_ - [x] _Verify your SSL, TLS & Ciphers implementation_ - [x] _Useful video resources_
NGINX Basics
- _Processes_ - [x] _CPU pinning_ - [x] _Shutdown of worker processes_ - _Configuration syntax_ - [x] _Comments_ - [x] _End of lines_ - [x] _Variables, Strings, and Quotes_ - [x] _Directives, Blocks, and Contexts_ - [x] _External files_ - [x] _Measurement units_ - [x] _Regular expressions with PCRE_ - [x] _Enable syntax highlighting_ - _Connection processing_ - [x] _Event-Driven architecture_ - [x] _Multiple processes_ - [x] _Simultaneous connections_ - [x] _HTTP Keep-Alive connections_ - [x] _sendfile, tcp_nodelay, and tcp_nopush_ - _Server blocks logic_ - [x] _Matching location_ - [ ] _if in location_ - [ ] _Nested locations_ - [x] _rewrite vs return_ - [x] _try_files directive_ - [x] _if, break and set_ - [x] _root vs alias_ - [x] _internal directive_ - [x] _External and internal redirects_ - [x] _allow and deny_ - [x] _uri vs request_uri_ - _Compression and decompression_ - [x] _What is the best NGINX compression gzip level?_ - _Hash tables_ - [x] _Server names hash table_ - _Log files_ - [x] _Conditional logging_ - [x] _Manually log rotation_ - [x] _NGINX upstream variables returns 2 values_ - _Reverse proxy_ - [x] _Passing requests_ - [x] _Trailing slashes_ - [ ] _Processing headers_ - [x] _Passing headers_ - [x] _Importance of the Host header_ - [x] _Redirects and X-Forwarded-Proto_ - [x] _A warning about the X-Forwarded-For_ - [x] _Improve extensibility with Forwarded_ - [x] _Response headers_ - _Load balancing algorithms_ - [x] _Backend parameters_ - [x] _Upstream servers with SSL_ - [x] _Round Robin_ - [x] _Weighted Round Robin_ - [x] _Least Connections_ - [x] _Weighted Least Connections_ - [x] _IP Hash_ - [x] _Generic Hash_ - [ ] _Fair module_ - [x] _Other methods_ - _Rate Limiting_ - [x] _Variables_ - [x] _Directives, keys, and zones_ - [x] _Burst and nodelay parameters_ - _NAXSI Web Application Firewall_ - _OWASP ModSecurity Core Rule Set (CRS)_ - _Other subjects_ - [ ] _Secure Distribution of SSL Private Keys with NGINX_ - _Core modules_ - [x] _ngx_http_geo_module_ - _3rd party modules_ - [x] _ngx_set_misc_ - [x] _ngx_http_geoip_module_
Helpers
- _Installing from source_ - [x] _Automatic installation on RHEL/Debian/BSD_ - [x] _Compiler and linker_ - [x] _Debugging Symbols_ - [x] _SystemTap_ - [x] _stapxx_ - [x] _Separation and improvement of installation methods_ - [x] _Installation Nginx on CentOS 7_ - [x] _Installation OpenResty on CentOS 7_ - [x] _Installation Tengine on Ubuntu 18.04_ - [x] _Installation Nginx on FreeBSD 11.3_ - [x] _Installation Nginx on FreeBSD 11.3 (from ports)_ - _Monitoring_ - [ ] _CollectD, Prometheus, and Grafana_ - [ ] _nginx-vts-exporter_ - [ ] _CollectD, InfluxDB, and Grafana_ - [ ] _Telegraf, InfluxDB, and Grafana_ - _Testing_ - [x] _Build OpenSSL 1.0.2-chacha version_ - [x] _Send request and show response headers_ - [x] _Send request with http method, user-agent, follow redirects and show response headers_ - [x] _Send multiple requests_ - [x] _Testing SSL connection_ - [x] _Testing SSL connection (debug mode)_ - [x] _Testing SSL connection with SNI support_ - [x] _Testing SSL connection with specific SSL version_ - [x] _Testing SSL connection with specific cipher_ - [x] _Verify 0-RTT_ - [x] _Testing SCSV_ - _Load testing with ApacheBench (ab)_ - [x] _Standard test_ - [x] _Test with Keep-Alive header_ - _Load testing with wrk2_ - [x] _Standard scenarios_ - [x] _POST call (with Lua)_ - [x] _Random paths (with Lua)_ - [x] _Multiple paths (with Lua)_ - [x] _Random server address to each thread (with Lua)_ - [x] _Multiple json requests (with Lua)_ - [x] _Debug mode (with Lua)_ - [x] _Analyse data pass to and from the threads_ - [x] _Parsing wrk result and generate report_ - _Load testing with locust_ - [x] _Multiple paths_ - [x] _Multiple paths with different user sessions_ - [x] _TCP SYN flood Denial of Service attack_ - [x] _HTTP Denial of Service attack_ - _Debugging_ - [x] _Show information about processes_ - [x] _Check memory usage_ - [x] _Show open files_ - [x] _Check segmentation fault messages_ - [x] _Dump configuration_ - [x] _Get the list of configure arguments_ - [x] _Check if the module has been compiled_ - [x] _Show the most accessed IP addresses (ip and url)_ - [x] _Show the most requested urls with http methods_ - [x] _Show the most accessed response codes_ - [x] _Calculating requests per second with IP addresses and urls_ - [x] _Check that the gzip_static module is working_ - [x] _Which worker processing current request_ - [x] _Capture only http packets_ - [x] _Extract User Agent from the http packets_ - [x] _Capture only http GET and POST packets_ - [x] _Capture requests and filter by source ip and destination port_ - [x] _Capture HTTP requests/responses in real time, filter by GET, HEAD and save to a file_ - [ ] _Server Side Include (SSI) debugging_ - [x] _Dump a process's memory_ - _GNU Debugger (gdb)_ - [x] _Dump configuration from a running process_ - [x] _Show debug log in memory_ - [x] _Core dump backtrace_ - [x] _Debugging socket leaks_ - _SystemTap cheatsheet_ - [x] _stapxx_ - _Errors & Issues_ - [ ] _Common errors_ - _Configuration snippets_ - [x] _Nginx server header removal_ - [x] _Custom log formats_ - [x] _Log only 4xx/5xx_ - [x] _Restricting access with client certificate_ - [x] _Restricting access by geographical location_ - [x] _GeoIP 2 database_ - [ ] _Custom error pages_ - [x] _Dynamic error pages with SSI_ - [x] _Limiting the rate of requests per IP with geo and map_ - [x] _Using trailing slashes_ - [x] _Properly redirect all HTTP requests to HTTPS_ - [x] _Adding and removing the www prefix_ - [x] _Proxy/rewrite and keep the original URL_ - [x] _Proxy/rewrite and keep the part of original URL_ - [x] _Proxy/rewrite without changing the original URL (in browser)_ - [x] _Modify 301/302 response body_ - [x] _Redirect POST request with payload to external endpoint_ - [x] _Route to different backends based on HTTP method_ - [ ] _Redirect users with certain IP to special location_ - [x] _Allow multiple cross-domains using the CORS headers_ - [x] _Set correct scheme passed in X-Forwarded-Proto_ - [ ] _Securing URLs with the Secure Link Module_ - [ ] _Tips and methods for high load traffic testing (cheatsheet)_ - [ ] _Location matching examples_ - [ ] _Passing requests to the backend_ - [ ] _The HTTP backend server_ - [ ] _The uWSGI backend server_ - [ ] _The FastCGI backend server_ - [ ] _The memcached backend server_ - [ ] _The Redis backend server_ - [ ] _HTTPS traffic to upstream servers_ - [ ] _TCP and UDP load balancing_ - [ ] _Lua snippets_ - [ ] _nginscripts snippets_ - _Other snippets_ - [x] _Recreate base directory_ - [x] _Create a temporary static backend_ - [x] _Create a temporary static backend with SSL support_ - [x] _Generate password file with htpasswd command_ - [x] _Generate private key without passphrase_ - [x] _Generate private key with passphrase_ - [x] _Remove passphrase from private key_ - [x] _Encrypt existing private key with a passphrase_ - [x] _Generate CSR_ - [x] _Generate CSR (metadata from existing certificate)_ - [x] _Generate CSR with -config param_ - [x] _Generate private key and CSR_ - [x] _List available EC curves_ - [x] _Generate ECDSA private key_ - [x] _Generate private key and CSR (ECC)_ - [x] _Generate self-signed certificate_ - [x] _Generate self-signed certificate from existing private key_ - [x] _Generate self-signed certificate from existing private key and csr_ - [x] _Generate multidomain certificate (Certbot)_ - [x] _Generate wildcard certificate (Certbot)_ - [x] _Generate certificate with 4096 bit private key (Certbot)_ - [x] _Generate DH public parameters_ - [x] _Display DH public parameters_ - [x] _Extract certs from p7b_ - [x] _Convert DER to PEM_ - [x] _Convert PEM to DER_ - [x] _Verification of the certificate's supported purposes_ - [x] _Verification of the private key_ - [x] _Check private key_ - [x] _Get public key from private key_ - [x] _Verification of the public key_ - [x] _Verification of the certificate_ - [x] _Verification of the CSR_ - [x] _Check the private key and the certificate are match_ - [x] _TLSv1.3 and CCM ciphers_
Base Rules
- [x] _Format, prettify and indent your Nginx code_ - [x] _Never use a hostname in a listen or upstream directives_ - [x] _Set the HTTP headers with add_header and proxy_*_header directives properly_ - [ ] _Making a rewrite absolute (with scheme)_ - [x] _Use return directive for URL redirection (301, 302)_ - [x] _Use simple custom error pages_ - [x] _Configure log rotation policy_ - [x] _Don't duplicate index directive, use it only in the http block_
Debugging
- [x] _Improve debugging by disable daemon, master process, and all workers except one_ - [x] _Use core dumps to figure out why NGINX keep crashing_ - [x] _Use mirror module to copy requests to another backend_ - [ ] _Dynamic debugging with echo module_ - [ ] _Dynamic debugging with SSI_
Performance
- [x] _Enable OCSP Stapling_ - [ ] _Avoid multiple index directives_ - [x] _Use $request_uri to avoid using regular expressions_ - [x] _Use try_files directive to ensure a file exists_ - [ ] _Don't pass all requests to the backend - use try_files_ - [x] _Use return directive instead of rewrite for redirects_ - [x] _Enable PCRE JIT to speed up processing of regular expressions_ - [ ] _Set proxy timeouts for normal load and under heavy load_ - [ ] _Configure kernel parameters for high load traffic_ - [x] _Activate the cache for connections to upstream servers_
Hardening
- [x] _Keep NGINX up-to-date_ - [x] _Take care about your ACL rules_ - [x] _Use only the latest supported OpenSSL version_ - [x] _Remove support for legacy and risky HTTP request headers_ - [x] _Prevent Replay Attacks on Zero Round-Trip Time_ - [x] _Prevent caching of sensitive data_ - [x] _Limit concurrent connections_ - [ ] _Set properly files and directories permissions (also with acls) on a paths_ - [ ] _Implement HTTPOnly and secure attributes on cookies_
Reverse Proxy
- [x] _Use pass directive compatible with backend protocol_ - [x] _Be careful with trailing slashes in proxy_pass directive_ - [x] _Set and pass Host header only with $host variable_ - [x] _Set properly values of the X-Forwarded-For header_ - [x] _Don't use X-Forwarded-Proto with $scheme behind reverse proxy_ - [x] _Always pass Host, X-Real-IP, and X-Forwarded headers to the backend_ - [x] _Use custom headers without X- prefix_ - [x] _Always use $request_uri instead of $uri in proxy_pass_ - [ ] _Set proxy buffers and timeouts_
Others
- [x] _Set the certificate chain correctly_ - [x] _Define security policies with security.txt_ - [x] _Use tcpdump to diagnose and troubleshoot the HTTP issues_

If you have any idea, send it back to me or add a pull request.

RSS Feed & Updates

GitHub exposes an RSS/Atom feed of the commits, which may also be useful if you want to be kept informed about all changes.

Checklist to rule them all

This checklist was the primary aim of the nginx-admins-handbook. It contains a set of best practices and recommendations on how to configure and maintain the NGINX properly.

This checklist contains all rules (79) from this handbook.

Generally, I think that each of these principles is important and should be considered. I separated them into four levels of priority to help guide your decision.

PRIORITY NAME AMOUNT DESCRIPTION
high critical 33 definitely use this rule, otherwise it will introduce high risks of your NGINX security, performance, and other
medium major 26 it's also very important but not critical, and should still be addressed at the earliest possible opportunity
low normal 12 there is no need to implement but it is worth considering because it can improve the NGINX working and functions
info minor 8 as an option to implement or use (not required)

Remember, these are only guidelines. My point of view may be different from yours so if you feel these priority levels do not reflect your configurations commitment to security, performance or whatever else, you should adjust them as you see fit.

RULE CHAPTER PRIORITY
Define the listen directives with address:port pair
Prevents soft mistakes which may be difficult to debug.
Base Rules high
Prevent processing requests with undefined server names
It protects against configuration errors, e.g. traffic forwarding to incorrect backends.
Base Rules high
Never use a hostname in a listen or upstream directives
While this may work, it will comes with a large number of issues.
Base Rules high
Set the HTTP headers with addheader and proxy*_header directives properly
Set the right security headers for all contexts.
Base Rules high
Configure log rotation policy
Save yourself trouble with your web server: configure appropriate logging policy.
Base Rules high
Use simple custom error pages
Default error pages reveals information which leads to information leakage vulnerability.
Base Rules high
Use HTTP/2
HTTP/2 will make our applications faster, simpler, and more robust.
Performance high
Always keep NGINX up-to-date
Use newest NGINX package to fix vulnerabilities, bugs, and to use new features.
Hardening high
Run as an unprivileged user
Use the principle of least privilege. This way only master process runs as root.
Hardening high
Protect sensitive resources
Hidden directories and files should never be web accessible.
Hardening high
Take care about your ACL rules
Test your access-control lists and to stay secure.
Hardening high
Hide upstream proxy headers
Don't expose what version of software is running on the server.
Hardening high
Remove support for legacy and risky HTTP request headers
Supports for the offending headers should be removed.
Hardening high
Force all connections over TLS
Protects your website for handle sensitive communications.
Hardening high
Use min. 2048-bit for RSA and 256-bit for ECC
2048 bit (RSA) or 256 bit (ECC) keys are sufficient for commercial use.
Hardening high
Keep only TLS 1.3 and TLS 1.2
Use TLS with modern cryptographic algorithms and without protocol weaknesses.
Hardening high
Use only strong ciphers
Use only strong and not vulnerable cipher suites.
Hardening high
Use more secure ECDH Curve
Use ECDH Curves with according to NIST recommendations.
Hardening high
Use strong Key Exchange with Perfect Forward Secrecy
Establishes a shared secret between two parties that can be used for secret communication.
Hardening high
Defend against the BEAST attack
The server ciphers should be preferred over the client ciphers.
Hardening high
Enable HTTP Strict Transport Security
Tells browsers that it should only be accessed using HTTPS, instead of using HTTP.
Hardening high
Reduce XSS risks (Content-Security-Policy)
CSP is best used as defence-in-depth. It reduces the harm that a malicious injection can cause.
Hardening high
Control the behaviour of the Referer header (Referrer-Policy)
The default behaviour of referrer leaking puts websites at risk of privacy and security breaches.
Hardening high
Provide clickjacking protection (X-Frame-Options)
Defends against clickjacking attack.
Hardening high
Prevent some categories of XSS attacks (X-XSS-Protection)
Prevents to render pages if a potential XSS reflection attack is detected.
Hardening high
Prevent Sniff Mimetype middleware (X-Content-Type-Options)
Tells browsers not to sniff MIME types.
Hardening high
Reject unsafe HTTP methods
Only allow the HTTP methods for which you, in fact, provide services.
Hardening high
Prevent caching of sensitive data
It helps to prevent critical data (e.g. credit card details, or username) leaked.
Hardening high
Limit concurrent connections
Limit concurrent connections to prevent a rogue guys from repeatedly connecting to and monopolizing NGINX.
Hardening high
Use pass directive compatible with backend protocol
Set pass directive only to working with compatible backend layer protocol.
Reverse Proxy high
Set properly values of the X-Forwarded-For header
Identify clients communicating with servers located behind the proxy.
Reverse Proxy high
Don't use X-Forwarded-Proto with $scheme behind reverse proxy
Prevent pass incorrect value of this header.
Reverse Proxy high
Always use $request_uri instead of $uri in proxy_pass
You should always pass unchanged URI to the backend layer.
Reverse Proxy high
Organising Nginx configuration
Well organised code is easier to understand and maintain.
Base Rules medium
Format, prettify and indent your Nginx code
Formatted code is easier to maintain, debug, and can be read and understood in a short amount of time.
Base Rules medium
Use reload option to change configurations on the fly
Graceful reload of the configuration without stopping the server and dropping any packets.
Base Rules medium
Use return directive for URL redirection (301, 302)
The by far simplest and fastest because there is no regexp that has to be evaluated.
Base Rules medium
Maintaining SSL sessions
Improves performance from the clients’ perspective.
Performance medium
Enable OCSP Stapling
Enable to reduce the cost of an OCSP validation.
Performance medium
Use exact names in a server_name directive if possible
Helps speed up searching using exact names.
Performance medium
Avoid checks server_name with if directive
It decreases NGINX processing requirements.
Performance medium
Use $request_uri to avoid using regular expressions
By default, the regex is costly and will slow down the performance.
Performance medium
Use try_files directive to ensure a file exists
Use it if you need to search for a file, it saving duplication of code also.
Performance medium
Use return directive instead of rewrite for redirects
Use return directive to more speedy response than rewrite.
Performance medium
Enable PCRE JIT to speed up processing of regular expressions
NGINX with PCRE JIT is much faster than without it.
Performance medium
Activate the cache for connections to upstream servers
Nginx can now reuse its existing connections (keepalive) per upstream.
Performance medium
Disable unnecessary modules
Limits vulnerabilities, improve performance and memory efficiency.
Hardening medium
Hide Nginx version number
Don't disclose sensitive information about NGINX.
Hardening medium
Hide Nginx server signature
Don't disclose sensitive information about NGINX.
Hardening medium
Use only the latest supported OpenSSL version
Stay protected from SSL security threats and don't miss out of new features.
Hardening medium
Prevent Replay Attacks on Zero Round-Trip Time
0-RTT is disabled by default but you should know that enabling this option creates a significant security risks.
Hardening medium
Mitigation of CRIME/BREACH attacks
Disable HTTP compression or compress only zero sensitive content.
Hardening medium
Deny the use of browser features (Feature-Policy)
A mechanism to allow and deny the use of browser features.
Hardening medium
Control Buffer Overflow attacks
Prevents errors are characterised by the overwriting of memory fragments of the NGINX process.
Hardening medium
Mitigating Slow HTTP DoS attacks (Closing Slow Connections)
Prevents attacks in which the attacker sends HTTP requests in pieces slowly.
Hardening medium
Set and pass Host header only with $host variable
Use of the $host is the only one guaranteed to have something sensible.
Reverse Proxy medium
Always pass Host, X-Real-IP, and X-Forwarded headers to the backend
It gives you more control of forwarded headers.
Reverse Proxy medium
Set the certificate chain correctly
Send the complete chain to the client.
Others medium
Enable DNS CAA Policy
Allows domain name holders to indicate to CA whether they are authorized to issue digital certificates.
Others medium
Separate listen directives for 80 and 443 ports
Help you maintain and modify your configuration.
Base Rules low
Use only one SSL config for the listen directive
Prevents multiple configurations on the same listening address.
Base Rules low
Use geo/map modules instead of allow/deny
Provides the perfect way to block invalid visitors.
Base Rules low
Set global root directory for unmatched locations
Specifies the root directory for an undefined locations.
Base Rules low
Don't duplicate index directive, use it only in the http block
Watch out for duplicating the same rules.
Base Rules low
Adjust worker processes
You can adjust this value to maximum throughput under high concurrency.
Performance low
Make an exact location match to speed up the selection process
Exact location matches are often used to speed up the selection process.
Performance low
Use limit_conn to improve limiting the download speed
Limits NGINX download speed per connection.
Performance low
Be careful with trailing slashes in proxy_pass directive
Incorrect setting could end up with some strange url.
Reverse Proxy low
Use custom headers without X- prefix
The use of custom headers with X- prefix is discouraged.
Reverse Proxy low
Tweak passive health checks
Improve behaviour of the passive health checks.
Load Balancing low
Define security policies with security.txt
Helps make things easier for companies and security researchers.
Others low
Map all the things...
Map module provides a more elegant solution for clearly parsing a big list of regexes.
Base Rules info
Use custom log formats
This is extremely helpful for debugging specific location directives.
Debugging info
Use debug mode to track down unexpected behaviour
There's probably more detail than you want, but that can sometimes be a lifesaver.
Debugging info
Improve debugging by disable daemon, master process, and all workers except one
This simplifies the debugging and lets test configurations rapidly.
Debugging info
Use core dumps to figure out why NGINX keep crashing
Enable core dumps when your NGINX instance receive an unexpected error or when it crashed.
Debugging info
Use mirror module to copy requests to another backend
Use mirroring for investigation and debugging of any original request.
Debugging info
Don't disable backends by comments, use down parameter
Is a good solution to marks the server as permanently unavailable.
Load Balancing info
Use tcpdump to diagnose and troubleshoot the HTTP issues
Use tcpdump to monitor HTTP.
Others info

Bonus Stuff

You can find here a few of the different things I've worked and included to this repository. I hope that these extras will be useful.

Configuration reports

Many of these recipes have been applied to the configuration of my old private website.

An example configuration is in the configuration examples chapter. It's also based on this version of printable high-res hardening cheatsheets.

SSL Labs

Read about SSL Labs grading here (SSL Labs Grading 2018).

Short SSL Labs grades explanation:

A+ is clearly the desired grade, both A and B grades are acceptable and result in adequate commercial security. The B grade, in particular, may be applied to configurations designed to support very wide audiences (for old clients).

I finally got A+ grade and following scores:

Look also at the following recommendations. I believe the right configuration of NGINX should give the following SSL Labs scores and provides the best security for the most cases:

blkcipher_ssllabs_preview

Something about SSL Labs grading mechanism (that's an interesting point of view):

The whole grading mechanism is more propaganda and public relations than actual security. If you want good security, then you must mind the details and understand how things work internally. If you want a good grade then you should do whatever it takes to have a good grade. An "A+" from SSL Labs is a very nifty thing to add at the end of a report, but it does not really equate with having rock solid security. Having an "A+" equates with being able to say "I have an A+". - from this answer by Tom Leek.

Mozilla Observatory

Read about Mozilla Observatory here and about Observatory Scoring Methodology.

I also got the highest summary note (A+) on the Observatory with a very high test score (120/100, max. 135/100):

blkcipher_mozilla_observatory_preview

Printable hardening cheatsheets

I created two versions of printable posters with hardening cheatsheets (High-Res 5000x8800) based on recipes from this handbook:

For xcf and pdf formats please see this directory.

nginx-hardening-cheatsheet-100p

nginx-hardening-cheatsheet-tls13

Fully automatic installation

I created a set of scripts for unattended installation of NGINX from the raw, uncompiled code. It allows you to easily install, create a setup for dependencies (like zlib or openssl), and customized with installation parameters.

For more information please see Installing from source - Automatic installation chapter which describes the installation of NGINX on systems/distros such as Ubuntu, Debian, CentOS, and FreeBSD.

Static error pages generator

I created a simple to use generator for static pages to replace the default error pages that comes with any web server like NGINX.

For more information please see HTTP Static Error Pages Generator.

Server names parser

I added scripts for fast multiple domain searching in the configuration. These tools get specific server_name matches and print them on the screen as a server { ... } blocks. Both are very helpful if you really have tons of domains or if you want to list specific vhosts from file or the active configuration.

You must follow one important rule to be able to use it. Your server block must have the following structure:

server {

  server_name example.com example.org;

  ... # other directives

}

Example of use:

./snippets/server-name-parser/check-server-name.sh example.com
Searching 'example.com' in '/usr/local/etc/nginx' (from disk)

/usr/local/etc/nginx/domains/example.com/servers.conf:79: return 301 https://example.com$request_uri;
/usr/local/etc/nginx/domains/example.com/servers.conf:252: return 301 https://example.com$request_uri;
/usr/local/etc/nginx/domains/example.com/servers.conf:3825: server_name example.com;

Searching 'example.com' in server contexts (from a running process)

>>>>>>>>>> BEG >>>>>>>>>>
server {

  include listen/192.168.252.10/https.example.com.conf;

  server_name example.com;

  location / {

    return 204 "RFC 792";

  }

  access_log /var/log/nginx/example.com/access.log standard;
  error_log /var/log/nginx/example.com/error.log warn;

}
<<<<<<<<<< END <<<<<<<<<<

For more information please see snippets/server-name-parser directory.

Books

Nginx Essentials

Authors: Valery Kholodkov

Excel in Nginx quickly by learning to use its most essential features in real-life applications.

This short review comes from this book or the store.

Nginx Cookbook

Authors: Derek DeJonghe

You’ll find recipes for:

This short review comes from this book or the store.

Nginx HTTP Server

Authors: Martin Fjordvald, Clement Nedelcu

Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever.

This short review comes from this book or the store.

Nginx High Performance

Authors: Rahul Sharma

Optimize NGINX for high-performance, scalable web applications.

This short review comes from this book or the store.

Mastering Nginx

Authors: Dimitri Aivaliotis

Written for experienced systems administrators and engineers, this book teaches you from scratch how to configure Nginx for any situation. Step-by-step instructions and real-world code snippets clarify even the most complex areas.

This short review comes from this book or the store.

ModSecurity 3.0 and NGINX: Quick Start Guide

Authors: Faisal Memon, Owen Garrett, Michael Pleshakov

Learn in this ebook how to get started with ModSecurity, the world’s most widely deployed web application firewall (WAF), now available for NGINX and NGINX Plus.

This short review comes from this book or the store.

Cisco ACE to NGINX: Migration Guide

Authors: Faisal Memon

This ebook provides step-by-step instructions on replacing Cisco ACE with NGINX and off-the-shelf servers. NGINX helps you cut costs and modernize.

In this ebook you will learn:

This short review comes from this book or the store.

External Resources

Nginx official

  :black_small_square: Nginx Project
  :black_small_square: Nginx Documentation
  :black_small_square: Nginx Wiki
  :black_small_square: Nginx Admin's Guide
  :black_small_square: Nginx Pitfalls and Common Mistakes
  :black_small_square: Development Guide
  :black_small_square: Nginx Forum
  :black_small_square: Nginx Security Advisories
  :black_small_square: Nginx Security Controls
  :black_small_square: Nginx Mailing List
  :black_small_square: Nginx Read-only Mirror
  :black_small_square: NGINX-Demos
  :black_small_square: Thread Pools in NGINX Boost Performance 9x!

Nginx distributions

  :black_small_square: OpenResty
  :black_small_square: The Tengine Web Server

Comparison reviews

  :black_small_square: NGINX vs. Apache (Pro/Con Review, Uses, & Hosting for Each)
  :black_small_square: Web cache server performance benchmark: nuster vs nginx vs varnish vs squid

Cheatsheets & References

  :black_small_square: agentzh's Nginx Tutorials
  :black_small_square: Introduction to nginx.conf scripting
  :black_small_square: Nginx discovery journey
  :black_small_square: Nginx Guts
  :black_small_square: Nginx Cheatsheet
  :black_small_square: Nginx Tutorials, Linux Sysadmin Configuration & Optimizing Tips and Tricks
  :black_small_square: Nginx boilerplate configs
  :black_small_square: Awesome Nginx configuration template
  :black_small_square: Nginx Quick Reference
  :black_small_square: A collection of resources covering Nginx and more
  :black_small_square: A collection of useful Nginx configuration snippets
  :black_small_square: Nginx configurations for most popular CMS/CMF/Frameworks based on PHP
  :black_small_square: Boilerplate configuration for nginx and certbot with docker-compose

Performance & Hardening

  :black_small_square: Nginx Tuning For Best Performance by Denji
  :black_small_square: Nginx Optimization: understanding sendfile, tcp_nodelay and tcp_nopush
  :black_small_square: How we scaled nginx and saved the world 54 years every day
  :black_small_square: TLS has exactly one performance problem: it is not used widely enough
  :black_small_square: SSL/TLS Deployment Best Practices
  :black_small_square: SSL Server Rating Guide
  :black_small_square: SSL Pulse
  :black_small_square: How to Build a Tough NGINX Server in 15 Steps
  :black_small_square: Top 25 Nginx Web Server Best Security Practices
  :black_small_square: Nginx Secure Web Server
  :black_small_square: Strong SSL Security on Nginx
  :black_small_square: Enable cross-origin resource sharing (CORS)
  :black_small_square: NAXSI - WAF for Nginx
  :black_small_square: ModSecurity for Nginx

Presentations & Videos

  :black_small_square: NGINX: Basics and Best Practices
  :black_small_square: NGINX Installation and Tuning
  :black_small_square: Nginx Internals (by Joshua Zhu)
  :black_small_square: Nginx internals (by Liqiang Xu)
  :black_small_square: How to secure your web applications with NGINX
  :black_small_square: Tuning TCP and NGINX on EC2
  :black_small_square: Extending functionality in nginx, with modules!
  :black_small_square: Nginx - Tips and Tricks.
  :black_small_square: Nginx Scripting - Extending Nginx Functionalities with Lua
  :black_small_square: How to handle over 1,200,000 HTTPS Reqs/Min
  :black_small_square: Using ngx_lua / lua-nginx-module in pixiv
  :black_small_square: Reading nginx CHANGES together
  :black_small_square: Dynamic modules:how it works
  :black_small_square: NGINX Conf 2014
  :black_small_square: NGINX Conf 2015
  :black_small_square: NGINX Conf 2016
  :black_small_square: NGINX Conf 2017
  :black_small_square: NGINX Conf 2018 | Deep Dive Track
  :black_small_square: NGINX Conf 2018 | Keynotes and Sessions
  :black_small_square: Making HTTPS Fast(er): Ilya Grigorik @ nginx.conf 2014

Playgrounds

  :black_small_square: NGINX Rate Limit, Burst and nodelay sandbox

Config generators

  :black_small_square: nginxconfig - Nginx config generator on steroids.
  :black_small_square: ssl-config-generator - Mozilla SSL Configuration Generator.
  :black_small_square: nginx-config-builder - is a python library for building nginx configuration files programatically.

Config parsers

  :black_small_square: crossplane - quick and reliable way to convert NGINX configurations into JSON and back.
  :black_small_square: nginxparser - parses nginx configuration with Pyparsing.

Config managers

  :black_small_square: ansible-role-nginx - asible role to install and manage nginx configuration.
  :black_small_square: ansible-role-nginx - installs and configures the latest version of Nginx.
  :black_small_square: ansible-role-nginx - installs NGINX, NGINX Plus, the NGINX Amplify agent, and more.
  :black_small_square: puppet-nginx - puppet module to manage NGINX on various UNIXes.

Static analyzers

  :black_small_square: gixy - is a tool to analyze Nginx configuration to prevent security misconfiguration and automate flaw detection.
  :black_small_square: nginx-config-formatter - Nginx config file formatter/beautifier written in Python.
  :black_small_square: nginxbeautifier - format and beautify Nginx config files.
  :black_small_square: nginx-minify-conf - creates a minified version of a Nginx configuration.

Log analyzers

  :black_small_square: GoAccess - is a fast, terminal-based log analyzer (quickly analyze and view web server statistics in real time).
  :black_small_square: Graylog - is a leading centralized log management for capturing, storing, and enabling real-time analysis.
  :black_small_square: Logstash - is an open source, server-side data processing pipeline.

Performance analyzers

  :black_small_square: ngxtop - parses your Nginx access log and outputs useful, top-like, metrics of your Nginx server.

Builder tools

  :black_small_square: Nginx-builder - is a tool for building deb or rpm package NGINX from the source code.

Benchmarking tools

  :black_small_square: ab - is a single-threaded command line tool for measuring the performance of HTTP web servers.
  :black_small_square: siege - is an http load testing and benchmarking utility.
  :black_small_square: wrk - is a modern HTTP benchmarking tool capable of generating significant load.
  :black_small_square: wrk2 - is a constant throughput, correct latency recording variant of wrk.
  :black_small_square: vegeta - HTTP load testing tool and library.
  :black_small_square: bombardier - is a HTTP(S) benchmarking tool.
  :black_small_square: gobench - is a HTTP/HTTPS load testing and benchmarking tool.
  :black_small_square: hey - is a HTTP load generator, ApacheBench (ab) replacement, formerly known as rakyll/boom.
  :black_small_square: boom - is a script you can use to quickly smoke-test your web app deployment.
  :black_small_square: httperf - the httperf HTTP load generator.
  :black_small_square: JMeter™ - is designed to load test functional behavior and measure performance.
  :black_small_square: Gatling - is a powerful open-source load and performance testing tool for web applications.
  :black_small_square: locust - is an easy-to-use, distributed, user load testing tool.
  :black_small_square: slowloris - low bandwidth DoS tool. Slowloris rewrite in Python.
  :black_small_square: slowhttptest - application layer DoS attack simulator.
  :black_small_square: GoldenEye - GoldenEye Layer 7 (KeepAlive+NoCache) DoS test tool.

Debugging tools

  :black_small_square: strace - is a diagnostic, debugging and instructional userspace utility (linux syscall tracer) for Linux.
  :black_small_square: GDB - allows you to see what is going on `inside' another program while it executes.
  :black_small_square: SystemTap - provides infrastructure to simplify the gathering of information about the running Linux system.
  :black_small_square: stapxx - simple macro language extensions to SystemTap.
  :black_small_square: htrace.sh - is a simple Swiss Army knife for http/https troubleshooting and profiling.

Security & Web testing tools

  :black_small_square: Burp Suite - is a graphical tool for testing Web application security.
  :black_small_square: w3af - is a Web Application Attack and Audit Framework.
  :black_small_square: nikto - web server scanner which performs comprehensive tests.
  :black_small_square: ssllabs-scan - client for SSL Labs APIs, designed for automated and/or bulk testing.
  :black_small_square: http-observatory - Mozilla HTTP Observatory.
  :black_small_square: testssl.sh - checks a server's service on any port for the support of TLS/SSL ciphers.
  :black_small_square: sslyze - is a fast and powerful SSL/TLS server scanning library.
  :black_small_square: cipherscan - is a very simple way to find out which SSL ciphersuites are supported by a target.
  :black_small_square: O-Saft - OWASP SSL advanced forensic tool.
  :black_small_square: Nghttp2 - is an implementation of HTTP/2 and its header compression algorithm HPACK in C.
  :black_small_square: h2spec - is a conformance testing tool for HTTP/2 implementation.
  :black_small_square: h2t - is a simple tool to help sysadmins to hardening their websites.
  :black_small_square: http2fuzz - HTTP/2 fuzzer written in Golang.
  :black_small_square: Arjun - HTTP parameter discovery suite.
  :black_small_square: Corsy - CORS misconfiguration scanner.
  :black_small_square: XSStrike - most advanced XSS scanner.

Development

  :black_small_square: Sample ebook generated from NGINX source code.
  :black_small_square: Programming in Lua (first edition)
  :black_small_square: Scripting Nginx with Lua
  :black_small_square: Emiller’s Guide To Nginx Module Development
  :black_small_square: Emiller’s Advanced Topics In Nginx Module Development
  :black_small_square: NGINX Tutorial: Developing Modules
  :black_small_square: An Introduction To OpenResty (nginx + lua) - Part 1
  :black_small_square: An Introduction To OpenResty - Part 2 - Concepts
  :black_small_square: An Introduction To OpenResty - Part 3
  :black_small_square: OpenResty (Nginx) with dynamically generated certificates
  :black_small_square: Programming OpenResty

Online & Web tools

  :black_small_square: SSL Server Test by SSL Labs
  :black_small_square: Test SSL/TLS (PCI DSS, HIPAA and NIST)
  :black_small_square: SSL analyzer and certificate checker
  :black_small_square: Tools for testing SSL configuration
  :black_small_square: Test your TLS server configuration (e.g. ciphers)
  :black_small_square: Scan your website for non-secure content
  :black_small_square: Analyze website security
  :black_small_square: TLS Cipher Suite Search
  :black_small_square: SSL/TLS Capabilities of Your Browser
  :black_small_square: SSL-Client Info's
  :black_small_square: Public Diffie-Hellman Parameter Service/Tool
  :black_small_square: Analyse the HTTP response headers by Security Headers
  :black_small_square: Analyze your website by Mozilla Observatory
  :black_small_square: CAA Record Helper
  :black_small_square: Linting tool that will help you with your site's accessibility, speed, security and more
  :black_small_square: Service to scan and analyse websites
  :black_small_square: Tool from above to either encode or decode a string of text
  :black_small_square: Online translator for search queries on log data
  :black_small_square: Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript
  :black_small_square: Online tool to learn, build, & test Regular Expressions
  :black_small_square: Online Regex Tester & Debugger
  :black_small_square: Tool for testing regular expressions directly within an NGINX configuration
  :black_small_square: A web app for encryption, encoding, compression and data analysis
  :black_small_square: Nginx location match tester
  :black_small_square: Nginx location match visible

Other stuff

  :black_small_square: Web technology for developers
  :black_small_square: Mozilla Web Security
  :black_small_square: Application Security Wiki
  :black_small_square: OWASP ASVS 3.0.1
  :black_small_square: OWASP ASVS 3.0.1 Web App
  :black_small_square: OWASP ASVS 4.0
  :black_small_square: OWASP Top 10 Proactive Controls 2018.
  :black_small_square: OWASP Testing Guide v4
  :black_small_square: OWASP Dev Guide
  :black_small_square: Transport Layer Protection Cheat Sheet by OWASP
  :black_small_square: OWASP WSTG
  :black_small_square: Security/Server Side TLS by Mozilla
  :black_small_square: Applied Crypto Hardening
  :black_small_square: Browser support tables for modern web technologies
  :black_small_square: Memorable site for testing clients against bad SSL configs
  :black_small_square: The HTTPS-Only Standard
  :black_small_square: The Web Security Academy
  :black_small_square: Burp Scanner - Issue Definitions
  :black_small_square: Web application security: what to do when...
  :black_small_square: Transport Layer Security (TLS) Parameters
  :black_small_square: TLS Redirection (and Virtual Host Confusion)
  :black_small_square: TLS Security 6: Examples of TLS Vulnerabilities and Attacks
  :black_small_square: Guidelines for Setting Security Headers
  :black_small_square: Mozilla Guidelines - Web Security
  :black_small_square: Secure your web application with these HTTP headers
  :black_small_square: Security HTTP Headers
  :black_small_square: Analysis of various reverse proxies, cache proxies, load balancers, etc.
  :black_small_square: How HTTPS works ...in a comic!
  :black_small_square: Regular-Expressions
  :black_small_square: Regexp Security Cheatsheet
  :black_small_square: HTTPS on Stack Overflow: The End of a Long Road
  :black_small_square: The Architecture of Open Source Applications - Nginx
  :black_small_square: BBC Digital Media Distribution: How we improved throughput by 4x
  :black_small_square: The C10K problem by Dan Kegel
  :black_small_square: The Secret To 10 Million Concurrent Connections
  :black_small_square: High Performance Browser Networking
  :black_small_square: The System Design Primer
  :black_small_square: awesome-scalability
  :black_small_square: Web Architecture 101
  :black_small_square: Learn where some of the network sysctl variables fit into the Linux/Kernel network flow
  :black_small_square: jemalloc vs tcmalloc vs dlmalloc
  :black_small_square: On the Impact of Memory Allocation on High-Performance Query Processing
  :black_small_square: GLB: GitHub’s open source load balancer

What's next?

Go back to the Table of Contents or read the next chapters:



Meme