nginx-usage-sample

nginx

nginx can be used as light web server(serve request directly), reverse proxy(http) and proxy server(stream) for Pop3 and IMAP.

nginx as a light static web server

put your website at /var/www/html

1
2
3
4
5
6
7
8
9
http {
server {
root /var/www/html;
index index.html;
location / {
index index.htm index.html;
}
}
}

nginx as static content server(file server)

1
2
3
location /images/ {
autoindex on;
}
  • root directive
    The root directive specifies the root directory that will be used to search for a file. To obtain the path of a requested file, NGINX appends the request URI to the path specified by the root directive. The directive can be placed on any level within the http {}, server {}, or location {} contexts. In the example below, the root directive is defined for a virtual server. It applies to all location {} blocks where the root directive is not included to explicitly redefine the root:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    http {
    server {
    listen 1.1.1.1;
    # only for this server
    root /www/data;

    location / {
    }

    location /images/ {
    }

    location ~ \.(mp3|mp4) {
    root /www/media;
    }
    }

    Here, NGINX searches for a URI that starts with /images/ in the /www/data/images/ directory in the file system. But if the URI ends with the .mp3 or .mp4 extension, NGINX instead searches for the file in the /www/media/ directory because it is defined in the matching location block.

    If a request ends with a slash, NGINX treats it as a request for a directory and tries to find an index file in the directory.

  • index directive
    The index directive defines the index file’s name (the default value is index.html). To continue with the example, if the request URI is /images/some/path/, NGINX delivers the file /www/data/images/some/path/index.html if it exists. If it does not, NGINX returns HTTP code 404 (Not Found) by default. To configure NGINX to return an automatically generated directory listing instead, include the on parameter to the autoindex directive

    1
    2
    3
    4
    5
    6
    7
    # you can also put these at server level or http level
    # so that it serve as a whole file server not the /images part only
    location /images/ {
    autoindex on;
    autoindex_exact_size on;
    autoindex_localtime on;
    }

    You can list more than one filename in the index directive. NGINX searches for files in the specified order and returns the first one it finds.

    1
    2
    3
    location / {
    index index.$geo.html index.htm index.html;
    }
  • try directive
    The try_files directive can be used to check whether the specified file or directory exists; NGINX makes an internal redirect if it does, or returns a specified status code if it doesn’t. For example, to check the existence of a file corresponding to the request URI, use the try_files directive and the $uri variable as follows:

    1
    2
    3
    4
    5
    6
    7
    server {
    root /www/data;

    location /images/ {
    try_files $uri /images/default.gif;
    }
    }

    The file is specified in the form of the URI, which is processed using the root or alias directives set in the context of the current location or virtual server. In this case, if the file corresponding to the original URI doesn’t exist, NGINX makes an internal redirect to the URI specified by the last parameter, returning /www/data/images/default.gif.

    The last parameter can also be a status code (directly preceded by the equals sign) or the name of a location. In the following example, a 404 error is returned if none of the parameters to the try_files directive resolve to an existing file or directory.

    1
    2
    3
    location / {
    try_files $uri $uri/ $uri.html =404;
    }

    In the next example, if neither the original URI nor the URI with the appended trailing slash resolve into an existing file or directory, the request is redirected to the named location which passes it to a backend server.

    1
    2
    3
    4
    5
    6
    location / {
    try_files $uri $uri/ @backend;
    }
    location @backend {
    proxy_pass http://backend.example.com;
    }

Optimizing Performance for Serving Content

1
2
3
4
5
6
7
location /mp3 {
sendfile on;
sendfile_max_chunk 1m;
tcp_nopush on;
tcp_nodelay on;
#...
}

setup static content server

nginx as a proxy(for same app)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http {
# one backend server
upstream zp_server1{
server 127.0.0.1:8089;
}

server {
listen 80;
# everything(original uri, args)will be passed to backend server
location / {
proxy_pass http://zp_server1;
}
}
}

nginx as a load balancer(for same app)

load balancer means for the same application, there are several deployed backend servers!, choose one for serving

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
http {
#two backend servers
upstream zp_server1 {
server 192.168.1.1:80 weight=5;
server 192.168.1.2:80 weight=8;
}

server {
listen 80;

location / {
proxy_pass http://zp_server1;
}
}
}

nginx as https server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
server {
# ssl is needed
listen 443 ssl;
server_name www.helloworld.com;

# cert is a must
ssl_certificate cert.pem;
ssl_certificate_key cert.key;

ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;

ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;

location / {
root /var/www;
# try index.html, if no, try index.htm
index index.html index.htm;
}
}

nginx as proxy for different apps

different websites uri

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
http {
upstream product_server{
# product website server
server www.helloworld.com:8081;
}
upstream admin_server{
server www.helloworld.com:8082;
}
upstream finance_server{
server www.helloworld.com:8083;
}
server {

listen 80;
index index.html;
root /var/www/html;

location / {
proxy_pass http://product_server;
}

#================================================
# matching is search location in url
# if location is /production/
# url /product, doesn't match
#================================================

# if location is /product
# url is /product match, /product/ also match!!

location /product/ {
proxy_pass http://product_server;
}
location /admin/ {
proxy_pass http://admin_server;
}
location /finance/ {
proxy_pass http://finance_server;
}
}
}

proxy_pass/fastcgi/scgi/uwsgi

proxy_pass is for http(s) between backend, nginx also supports other transport(high level above layer 4) protocol with backend like fastcgi, scgi, uwsgi.

1
2
3
4
5
6
7
8
9
10
11
server {
location / {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
}

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