Nginx Cheat Sheet
(from Understanding Nginx Server and Location Block Selection Algorithms - https://goo.gl/YyzshP)
server {
location {
}
}
Priority
The listen directive can be set to:
When "incomplete" listen directives
server {
listen 80 default_server;
server_name example.net www.example.net;
...
}
Nginx evaluates these by using the following formula:
server {
listen 80;
server_name example.com;
...
}
server {
listen 80;
server_name ~^(www|host1).*\.example\.com$;
...
}
server {
listen 80;
server_name ~^(subdomain|set|www|host1).*\.example\.com$;
...
}
server {
listen 80;
server_name ~^(?<user>.+)\.example\.net$;
...
}
location optional_modifier location_match {
...
}
index index.$geo.html index.0.html /index.html;
autoindex on | off;
root /var/www/main;
try_files $uri $uri.html $uri/ /fallback/index.html;
If a request is made for /blahblah, the first location will initially get the request. It will try to find a file called blahblah in /var/www/main directory. If it cannot find one, it will follow up by searching for a file called blahblah.html.
rewrite ^/rewriteme/(.*)$ /$1 last;
A request for /rewriteme/hello will be handled initially by the first location block. It will be rewritten to /hello and a location will be searched.
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
server {
listen 80;
server_name example.org www.example.org;
root /data/www;
location / {
index index.html index.php;
}
location ~* \.(gif|jpg|png)$ {
expires 30d;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
server {
listen 80;
server_name 107.170.165.117 myproject.com www.myproject.com;
root /srv/redmine/public;
passenger_enabled on;
client_max_body_size 10m;
}
upstream app_server {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80;
listen [::]:80 default ipv6only=on;
server_name ci.yourcompany.com;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}