Nginx

字数 827 · 2019-07-01

1
2
3
4
# start nginx
nginx
# Use an alternative configuration file
nginx -c <config-file>
1
nginx -s <signal>

Where signal may be one of the following:

  • stop — fast shutdown
  • quit — graceful shutdown
  • reload — reloading the configuration file
  • reopen — reopening the log files

Configuration File’s Structure

nginx consists of modules which are controlled by directives specified in the configuration file. Directives are divided into simple directives and block directives.
A simple directive consists of the name and parameters separated by spaces and ends with a semicolon (;).
A block directive has the same structure as a simple directive, but instead of the semicolon it ends with a set of additional instructions surrounded by braces ({ and }).

If a block directive can have other directives inside braces, it is called a context (examples: events, http, server, and location).

Directives placed in the configuration file outside of any contexts are considered to be in the main context. The events and http directives reside in the main context, server in http, and location in server.

The rest of a line after the # sign is considered a comment.

1
2
3
4
5
6
7
8
9
10
11
12
events {
  # ...
}

http {
  server {
    location {
      # ...
    }
    # ...
  }
}

Serving Static Content

Generally, the configuration file may include several server blocks distinguished by ports on which they listen to and by server names.

Once nginx decides which server processes a request, it tests the URI specified in the request’s header against the parameters of the location directives defined inside the server block.

If there are several matching location blocks, nginx selects the one with the longest prefix.

1
2
3
4
5
data
├── images
│   └── image1.png
└── www
    └── index.html
1
2
3
4
5
6
7
8
9
10
http {
  server {
    location / {
      root /data/www;
    }
    location /images/ {
      root /data;
    }
  }
}

Setting Up a Simple Proxy Server

1
2
3
4
5
6
7
8
9
server {
  location / {
    proxy_pass http://localhost:8080;
  }

  location ~ \.(gif|jpg|png)$ {
    root /data/images;
  }
}

A regular expression should be preceded with ~.

http://nginx.org/en/docs/beginners_guide.html

Gzip

1
2
3
4
gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_types text/plain text/css application/json application/x-javascript application/javascript text/xml application/xml application/rss+xml text/javascript image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype;

root & alias

1
2
3
4
5
location ^~ /suffix/web {
    access_log off;
    alias /path/to/project/dist;
    try_files $uri $uri/ /index.html =404;
}

websocket

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
http {
  upstream websocket {
    server localhost:8998; 
  }
  map $http_upgrade $connection_upgrade { 
    default upgrade; 
    '' close; 
  } 
  server {
    // ...
    location ~ ^/ws/(.*)$ {
      proxy_pass http://websocket;
      proxy_send_timeout 300s;
      proxy_http_version 1.1; 
      proxy_redirect off; 
      proxy_set_header Host $host; 
      proxy_set_header X-Real-IP $remote_addr; 
      proxy_read_timeout 3600s; 
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
      proxy_set_header Upgrade $http_upgrade; 
      proxy_set_header Connection $connection_upgrade; 
    }
  }
}

rewrite

1
2
rewrite regex replacement [flag];
# flag: last | break | rediret | permanent

regex variable

1
2
3
4
5
location ~ ^/rss/(?<targer>.*)$ {
  proxy_pass http://$targer;
  proxy_hide_header Access-Control-Allow-Origin;
  add_header Access-Control-Allow-Origin *;
}