Skip to content

Nginx

September 29, 2023
December 11, 2014

nginx news
nginx - ArchWiki

fcambus/nginx-resources: A collection of resources covering Nginx, Nginx + Lua, OpenResty and Tengine
schenkd/nginx-ui: Nginx UI allows you to access and modify the nginx configurations files without cli.

References

nginx documentation
Beginner’s Guide
Alphabetical index of directives
NGINX and NGINX Plus Admin Guide | NGINX

Nginx Community
GettingStarted - Nginx Community
Pitfalls - Nginx Community

Nginx 开发从入门到精通 — Nginx 开发从入门到精通
agentzh_新浪博客
agentzh's Nginx Tutorials download source
Nginx Guide - Envato Tuts+ Code Tutorials
nginx | 夢想家
The Architecture of Open Source Applications (Volume 2): nginx

agile6v/awesome-nginx: A curated list of awesome Nginx distributions、third modules、active developers and etc.

Nginx Tutorials | DigitalOcean
The NGINX Crash Course - YouTube

Tips and Tricks

Nginx Secure SSL Web Server @ Calomel.org
Tuning NGINX for Performance - NGINX
10 Tips to Improve Application Performance | NGINX
Optimizing HTTPS on Nginx
How To Create a Self-Signed SSL Certificate for Nginx in Ubuntu 16.04 | DigitalOcean
How To Set Up Nginx with HTTP/2 Support on Ubuntu 16.04 | DigitalOcean
DDos Mitigation - Using NGINX to Prevent DDoS Attacks | NGINX
Tuning NGINX - via @codeship | via @codeship

NGINX Docs | All-Active HA for NGINX Plus on the Google Cloud Platform
NGINX Docs | Load Balancing Apache Tomcat Servers with NGINX Open Source and NGINX Plus

Public IP Address API with two lines of Nginx config

Using NGINX to Serve .NET Core, Nodejs, or Static Contents - DZone Web Dev

OpenResty

OpenResty - Official Site Nginx + module bundles
OpenResty Reference

bungle/awesome-resty: A List of Quality OpenResty Libraries, and Resources.
OpenResty - Using LuaRocks

ficusio/openresty/
Load Balancing Search Traffic at Algolia with NGINX and OpenResty

agentzh's presentations

agentzh's Nginx Tutorials (version 2015.03.19)
OpenResty 最佳实践 - GitBook
Programming OpenResty · GitBook
乱入 OpenResty 手册! — chaos2openResty 2.1.130619 documentation
An Introduction To OpenResty (nginx + lua) - Part 1
An Introduction To OpenResty - Part 2 - Concepts
An Introduction To OpenResty - Part 3

Lapis - A web framework for Lua or MoonScript powered by OpenResty

Presentations

Application of Lua in Nginx

由 Lua 粘合的 Nginx 生态环境 -- agentzh tech-club.org 演讲听录 @ 2012-03-06 01:13 - Zoom.Quiet's PyBlosxom blogging slides

openresty/ngx_openresty: Turning Nginx into a Full-Fledged Scriptable Web Platform
openresty/lua-nginx-module - C
openresty/lua-resty-core: New FFI-based API for lua-nginx-module
openresty/lua-resty-upload: Streaming reader and parser for http file uploading based on ngx_lua cosocket

auth0/nginx-jwt: Lua script for Nginx that performs reverse proxy auth using JWT's

grimen/nginx-sandbox: Nginx/OpenResty sandbox/playground - for experiments with building Nginx apps using Lua.

Nginx Unit

Announcing NGINX Unit 1.0 | NGINX
About — NGINX Unit

Introducing NGINX Unit - YouTube
Introducing NGINX Controller - YouTube

Tengine

The Tengine Web Server Taobao's fork
在新版 Tengine 中开启 HTTP/2 协议支持 | 柳志超博客
Difference between openresty and tengine · Issue #54 · openresty/openresty

Amplify

nginxinc/nginx-amplify-doc: Public documentation for Amplify

vs Apache

NGINX vs. Apache: Our View of a Decade-Old Question
Nginx vs. Apache - Michael Lustfield
Apache vs Nginx: Practical Considerations | DigitalOcean
Apache vs Nginx

Configuration

https://github.com/h5bp/server-configs-nginx
https://github.com/Umkus/nginx-boilerplate
https://github.com/perusio/nginx_ensite

NGINXConfig | DigitalOcean

Configuration - Nginx Community

Core functionality
Module ngx_http_core_module

Serving Static Content | NGINX

Nginx Configuration Primer
Nginx Primer 2: From Apache to Nginx
Nginx Library
Debugging Nginx Errors
Understanding the Nginx Configuration Inheritance Model
How to Configure nginx
Hardening node.js for production part 2: using nginx to avoid node.js load | Arg! Team Blog

Understanding Nginx Server and Location Block Selection Algorithms | DigitalOcean
How To Set Up Nginx Server Blocks (Virtual Hosts) on Ubuntu 16.04 | DigitalOcean
Understanding the Nginx Configuration File Structure and Configuration Contexts | DigitalOcean
How To Optimize Nginx Configuration | DigitalOcean

Main config filer (nginx.conf) sits in the Nginx config folder (e.g. /etc/nginx).

this config distro specific:
There are sites-available/ and sites-enabled/, default setting file includes sites-enabled/*.
Server files are usually put in sites-available/ and symlinked to sites-enabled/.

Options are called directives. They can live inside blocks (contexts) which define the scope they apply. Contexts are hierarchical: main, (http, mail, stream), server, location.

An array directive (e.g.: fastcgi_param) is basically any directive that can be used more than once in a single context. Each subsequent declaration (in the same scope) will append the new information to what Nginx knows from the previous declarations. Any declaration in sub-scope will clear the declarations in the parent scope.

Access Control

NGINX Docs | Restricting Access with HTTP Basic Authentication
Protecting Folders with Nginx » KBeezie

Dynamic Drive: .htaccess password generator

Snippets

h5bp/server-configs-nginx: Nginx HTTP server boilerplate configs
timgws/handy-nginx-includes: 'best practice' nginx configuration includes.
lebinh/nginx-conf: A collection of useful Nginx configuration snippets ❗!important
danielrichman/nginx-config-snippets: nginx config snippets: gzip, catchall, domain c14n, ssl, uwsgi, php, wordpress

Internals

Inside NGINX - NGINX
Inside NGINX: Designed for Performance & Scalability
Boosting NGINX Performance 9x with Thread Pools

NGINX Internal Architecture - Workers - YouTube

server-configs-nginx/how-nginx-works.md at master · h5bp/server-configs-nginx
How nginx processes a request

How we scaled nginx and saved the world 54 years every day

nginScript

Launching nginScript and Looking Ahead - NGINX
nginScript - why create our own JavaScript implementation?
nginScript - A New and Powerful Way to Configure NGINX - NGINX
nginScript Documentation | NGINX

System service

Debian/Ubuntu

https://github.com/JasonGiedymin/nginx-init-ubuntu > https://github.com/hulihanapplications/nginx-init-debian

$ sudo service nginx
configtest    force-reload  reload        restart       start         status        stop

systemd

sudo systemctl enable --now nginx.service

Modules

3rdPartyModules - Nginx Community
Emiller's Guide to Nginx Module Development

ModuleLink
Auth Digesthttps://github.com/samizdatco/nginx-http-auth-digest
nginx-jwthttps://github.com/auth0/nginx-jwt
Echohttps://github.com/agentzh/echo-nginx-module
Fancy Indexeshttps://github.com/aperezdc/ngx-fancyindex
Log Ifhttps://github.com/cfsego/ngx_log_if/
PageSpeeedhttp://ngxpagespeed.com/ngx_pagespeed_example/
MP4 streaminghttp://nginx.org/en/docs/http/ngx_http_mp4_module.html

Use Cases

Static file serving

user http;

server {
    listen   80;
    server_name  domain.com www.domain.com;
    access_log   /var/log/nginx/static_server.log combined;
    root   /home/www;
    location / {
        index  index.php index.html index.htm;
        # Directory listing
        autoindex on;
    }
    # Password protect a directory
    location ^~ /secret_folder/ {
        auth_basic            "Restricted Area";
        # relative to this config file
        auth_basic_user_file  conf/htpasswd;
    }
}
sudo htpasswd -c /etc/nginx/conf/htpasswd <USER>

Setup group and ownership of www folder

sudo -v
mkdir /home/www
usermod -a -G http ${USER}
chown -R http:http /home/www
chmod -R g+w /home/www

Linux ACLs - Servers for Hackers

mkdir /home/www
getfacl /home/www
setfacl -R -m u:${USER}:rwx /var/www

Proxy

Load balancing (computing) - Wikiwand
Using nginx as HTTP load balancer
NGINX Load Balancing - HTTP and TCP Load Balancer
Module ngx_http_proxy_module
Module ngx_http_upstream_module

Use Nginx as a Front-end Proxy and Software Load Balancer
How To Configure Nginx as a Web Server and Reverse Proxy for Apache on One Ubuntu 14.04 Droplet | DigitalOcean

SSL Termination

NGINX SSL Termination | NGINX
How To Configure Nginx with SSL as a Reverse Proxy for Jenkins | DigitalOcean
How To Set Up Nginx Load Balancing with SSL Termination | DigitalOcean

Docker

jwilder/nginx-proxy
Automated Nginx Reverse Proxy for Docker ·

jasonwyatt/docker-nginx-loadbalancer: Nginx Load Balancer for Docker. It reads links to containers which expose and generates a suitable Nginx config file at startup.

Using Nginx as a reverse proxy/load balancer with Weave and Docker - Weaveworks

CGI

Understanding and Implementing FastCGI Proxying in Nginx | DigitalOcean

Reverse Proxy/Load Balancing

Nginx Proxy Manager
Putting it All Together - Docker, Docker-Compose, NGinx Proxy Manager, and Domain Routing - How To. - YouTube
Nginx Proxy Manager - How-To Installation and Configuration - YouTube

SWAG setup - LinuxServer.io

Using nginx as HTTP load balancer
NGINX Load Balancing - HTTP and TCP Load Balancer
How To Set Up Nginx Load Balancing | DigitalOcean
Using NGINX on Heroku to Serve Single Page Apps and Avoid CORS — AlphaSights Engineering

Add this to hosts:

127.0.0.1   mynodeapp.dev

Uses upstream module for abstraction and load balancing

# node.conf

# using node app as an example backend
upstream node {
    server http://127.0.0.1:3000;
    server unix:/tmp/mynodeapp.socket;
}

server {
    listen 80;
    server_name mynodeapp.dev;
    location / {
            proxy_pass node;
            proxy_set_header Host $http_host;
    }
}

Reverse proxy with proxy_pass

Using NGINX to Serve .NET Core, Nodejs, or Static Contents - DZone Web Dev

Add this to hosts:

127.0.0.1   api.github.localhost

Add this to /etc/nginx/sites-enabled/api.github:

server {
    listen 80;
    server_name api.github.localhost;

    location / {
        proxy_set_header Host api.github.com;
        proxy_pass https://api.github.com/;
    }

    access_log      /var/log/nginx/github_access.log combined;
    error_log       /var/log/nginx/github_error.log error;
}

Reverse proxy with resolver

Nginx resolver explained - The Matrix has you...

This allows the service to resolve dynamically to solve the issue when service is not available at Nginx startup (which causes start failure).

http {
    resolver 172.16.0.23;
}

server {
    set $upstream_endpoint http://service-999999.eu-west-2.elb.amazonaws.com; # No trailing slash
    location /test/ {
      rewrite ^/test/(.*) /$1 break;
      proxy_pass $upstream_endpoint;
    }
}

We can generate resolver directive upon boot (or in ENTRYPOINT) with:

echo resolver $(awk 'BEGIN{ORS=" "} $1=="nameserver" {print $2}' /etc/resolv.conf) ";" > /etc/nginx/conf/resolvers.conf
http {
    include resolvers.conf;
}

Redirect

Redirect to https

server {
    server_name _;
    rewrite ^ https://example.com$request_uri permanent;
}

How To Redirect www to Non-www with Nginx on Ubuntu 14.04 | DigitalOcean

Status page

Module ngx_http_api_module

HTTP/2

spun from SPDY from Google

HTTP/2

HTTP 2.0 With Nginx - Servers for Hackers
How To Set Up Nginx with HTTP/2 Support on Ubuntu 16.04 | DigitalOcean

HTTP/3

spun from QUIC from Google

HTTP/3 - HTTP over QUIC is the next generation by Daniel Stenberg - YouTube
Understand HTTP3 in 5 minutes - Je suis un dev

CORS

enable cross-origin resource sharing
Example Nginx configuration for adding cross-origin resource sharing (CORS) support to reverse proxied APIs
Wide-open CORS config for nginx
How CORS (Cross-Origin Resource Sharing) Works? - The Startup - Medium
Cross Origin Resource Sharing (Explained by Example) - YouTube

Node/Nginx

these settings are not tested

How To Set Up a Node.js Application for Production on Ubuntu 14.04 | DigitalOcean
How To Deploy Node.js Applications Using Systemd and Nginx | DigitalOcean
How To Host Multiple Node.js Applications On a Single VPS with nginx, forever, and crontab | DigitalOcean
使用 Docker + Nginx 部署前端项目 - 掘金 SSG and Next.js SSR
How to run a Node.js server with Nginx - LogRocket Blog
rubenv/node-systemd: Support for running node.js as a socket-activated service under systemd
Deploying Node.js with systemd · Ruben Vermeersch (rubenv)

# node.conf

server {
    listen 80;
    server_name localhost
    location / {
        root   /home/www;
    }
    location /api/ {
        # forward API endpoint to internal API server
        proxy_pass http://192.168.1.10:3000;
        proxy_set_header Host $http_host;
    }
}

Nginx status page

location /nginx_status {
    # Turn on nginx stats
    stub_status on;
    # I do not need logs for stats
    access_log   off;
    # Security: Only allow access from 192.168.1.100 IP #
    allow 192.168.1.100;
    # Send rest of the world to /dev/null #
    deny all;
}