nginx.conf

One server{} for a site service.

Use location to specify router of requests.

proxy_pass target to delegate inverse .

root dir to specify static files directory.

server_name to set domains whose requests will be handled.

1
2
3
4
5
6
7
8
9
10
11
12
// a node server listening to 3000
// handle requests from example.com

server {
listen: 80;
server_name: example.com;
location / {
index index.html;
root: /public/;
proxy_pass http://location:3000
}
}

Gzip

1
2
3
4
5
gzip on;
gzip_proxied any;
gzip_min_length 1024;
gzip_buffers 4 8k;
gzip_types text/css application/javascript application/atom+xml application/rss+xml text/plain image/svg+xml application/json text/javascript;

CORS

1
2
3
4
5
6
7
8
9
10
11
12
server {
listen: 80;
server_name: example.com;

location / {
add_header Access-Control-Allow-Origin: *;
add_header Access-Control_Allow_Credentials true;
add_header Access-Control-AllowMethods GET,POST,OPTIONS;
index index.html;
root: /public/
}
}

API delegate

1
2
3
4
5
6
7
8
server {
listen: 80;
server_name: example.com;

location /api {
proxy_pass: https://localhost:3000;
}
}

HTTP to HTTPS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen: 80;
server_name: example.com, *.example.com;

location / {
return 301 https://$host$request_uri;
}
}

server {
listen: 443 ssl;
server_name example.com;
ssl_certificate /crts/crt.pem; // add the certificate
ssl_certificate_key /crts/key.pem
location / {
index index.html;
root: /public/
}
}