Nginx for beginners 
Installation 
Windows 
- Download mainline version from official website
- Decompress the zip file at somewhere of your machine.
- cdto your decompressed folder, run- ./nginx.
- Open localhost:80in your browser to check ifnginxis successfully started.
Serving static content 
Let's start configuring nginx.conf from scratch. You can find this file in your decompressed folder(./conf/nginx.conf) if you're in windows.
- First, have a htmlfile on your machine.
<!DOCTYPE html>
<html>
<head>
 <meta charset="utf-8">
 <meta name="viewport" content="width=device-width, initial-scale=1">
 <title>Hello</title>
</head>
<body>
 <p>Hello from nginx</p>
</body>
</html>2
3
4
5
6
7
8
9
10
11
- Edit your nginx.confas. Double quote is optional.
http {
    server {
        listen 8080;
        root "path/to/your/parent/folder";
    }
}
events {}2
3
4
5
6
7
8
- Restart nginx, you should find out thatlocalhost:8080is the html we made.
./nginx -s reloadContent type 
All contents are text/plain by default, but this will lead to some issue like isolated css not working for html. So, to serve different types of content, add file extensions matching to content types.
http {
    types {
        text/css css;
        text/html html;
    }
    server {
        listen 8080;
        root "path/to/your/parent/folder";
    }
}
events {}2
3
4
5
6
7
8
9
10
11
12
However, nginx has predefined many type matching in mime.types. Simply adding include in nginx.conf is just fine.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
    }
}
events {}2
3
4
5
6
7
8
9
Location Context 
Location is similar to api route that works when we access localhost:port/<route>. However, should match to the folder structure.
Location 
To access index.html in a different folder, we can make a location in server. - Create a folder product with index.html.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
        location /product {
            root "path/to/your/parent/folder";
        }
    }
}
events {}2
3
4
5
6
7
8
9
10
11
12
Alias 
To make a alias for a location, since it's a alias, we don;t have to make a folder named 'item'.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
        location /product {
            root "path/to/your/parent/folder";
        }
        location /item {
            alias "path/to/your/parent/folder/product";
        }
    }
}
events {}2
3
4
5
6
7
8
9
10
11
12
13
14
15
Access any file 
nginx serve index.html by default. So, if index.html is not what we want to access, we should have a way to resolve it. The following config will try to access car/car.html if it exist, or it will fall back to /index.html. If all files are failed, it should show a 404 error page.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
        location /product {
            root "path/to/your/parent/folder";
        }
        location /item {
            alias "path/to/your/parent/folder/product";
        }
        location /car {
            root "path/to/your/parent/folder";
            try_files /car/car.html /index.html =404;
        }
    }
}
events {}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Access using regex 
nginx supports regex notate with ~* to match dynamic locations.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
        location /product {
            root "path/to/your/parent/folder";
        }
        location /item {
            alias "path/to/your/parent/folder/product";
        }
        location /car {
            root "path/to/your/parent/folder";
            try_files /car/car.html /index.html =404;
        }
        location ~* /id/[0-9] {
            root "path/to/your/parent/folder";
            try_files /index.html =404;
        }
    }
}
events {}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Redirect and Rewrite 
Redirect 
Redirecting is jumping from a location to another. The following will redirect to /product/index.html when we access localhost:8080/list.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
        location /product {
            root "path/to/your/parent/folder";
        }
        location /item {
            alias "path/to/your/parent/folder/product";
        }
        location /car {
            root "path/to/your/parent/folder";
            try_files /car/car.html /index.html =404;
        }
        location ~* /id/[0-9] {
            root "path/to/your/parent/folder";
            try_files /index.html =404;
        }
        location /list {
            return 307 /product
        }
    }
}
events {}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
Rewrite 
Rewriting is another way to alias a location but with regex. The following rewrite will mapping all matched location to /count/.
http {
    include mime.types;
    server {
        listen 8080;
        root "path/to/your/parent/folder";
        location /product {
            root "path/to/your/parent/folder";
        }
        location /item {
            alias "path/to/your/parent/folder/product";
        }
        location /car {
            root "path/to/your/parent/folder";
            try_files /car/car.html /index.html =404;
        }
        location ~* /id/[0-9] {
            root "path/to/your/parent/folder";
            try_files /index.html =404;
        }
        location /list {
            return 307 /product
        }
        rewrite ^/number/(w+) /count/$1;
    }
}
events {}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