Nginx syntax

From wikinotes

Documentation

builtin variables https://nginx.org/en/docs/varindex.html

Variables

# builtin variables
$host            # (requester) hostname, Host header, servername
$http_host       # Host header value
$https           # "on","" if SSL used
$request_method  # the HTTP method (GET,POST,...)
$request_uri     # original URI (w/ args)
$scheme          # "http"/"https"
$server_name     # server that accepted request
$server_port     # port that accepted request
set $variable "value";

Server Blocks

Server blocks are defined within a protocol.
You may define multiple server blocks, and even proxy between them.

http {
  server {
    # if desired, you can also specify the exact ip-address to respond to
    listen  80;       # ipv4
    listen  [::]:80;  # ipv6

    location / {
      index  index.html;
      root   /var/www/mywebsite.com;
    }

    # proxy to other webservers
    location /api/ {
      proxy_pass http://localhost:81/;
    }
  }

  server {
    listen 81;

    location / {
      include proxy_params;
      proxy_pass http://unix:/usr/local/www/website/serv.sock;
    }
  }
}

Map/Geo

Map allows you to define a variable, based on the value of a different variable.
See https://nginx.org/en/docs/http/ngx_http_map_module.html

# map [match] [set_var]
map $http_host $name {
  example.com   0;
  *.example.com 1;
  example.org   2;
  default       3;  # if does not match
}

Geo lets you define a variable based on the client ip address.
See https://nginx.org/en/docs/http/ngx_http_geo_module.html

# geo [set_var]
geo $var {
  127.0.0.1       0;
  192.168.1.0/24  1;
  default         2; # if does not match
}