-
Notifications
You must be signed in to change notification settings - Fork 0
/
NginX
149 lines (124 loc) · 3.7 KB
/
NginX
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# nginx-config
Nginx is very easy to work with. There are a few configurations you may need to know.
## Simple proxy server
Suppose you're running the app in your VPS and listening on port 6000. ATM, you can access to the app at http://{vps-ip-address}:6000
But everyone love visit the web via domain name rather than IP address & port. So we need to add a Nginx config to resolve domain name & forward the request to your app.
For example: I created a domain name "tvux.me" and pointing it to my VPS server (A record).
In VPS, create a Nginx configuration file, using domain name for naming is recommended
`sudo vi /etc/nginx/sites-enabled/tvux.me`
**/etc/nginx/sites-enabled/tvux.me**
```
server {
server_name tvux.me;
listen 80;
location / {
proxy_pass http://localhost:6000;
}
}
```
Now I can access my web app at http://tvux.me
## Https server
To enable HTTPS for Nginx, install certbot & certbot Nginx plugin (tutorial [Web-server-for-dummies](https://github.com/ThinhVu/web-server-guide-for-dummies))
```
sudo certbot --nginx
```
then select config file you want to make a HTTPS. In this example it's **tvux.me**, then select option 2 to redirect all HTTP request to HTTPS.
Now I can access my web app at https://tvux.me
## IPv6 support
To support IPv6, the first thing is config the domain name to poiting to my server with AAAA record
Then in my server, modify the config file a little bit.
**/etc/nginx/sites-enabled/tvux.me**
```
server {
server_name tvux.me;
listen 80;
listent [::]:80;
location / {
proxy_pass http://localhost:6000;
}
}
```
## Load balancer
Your app might need to update sometimes in the future so to ensure the app uptime 100%, you may need to run at least 1 backup instance. For example, I run the same app in port 6001 for backup purpose.
**/etc/nginx/sites-enabled/tvux.me**
```
upstream backend {
server localhost:6000 fail_timeout=5s max_fails=3;
server localhost:6001 backup;
}
server {
server_name tvux.me;
listen 80;
listent [::]:80;
location / {
proxy_pass http://backend;
}
}
```
### Connection consistent
```
upstream backend {
hash $binary_remote_addr consistent;
server localhost:5000;
server localhost:5001;
server localhost:5002;
}
```
## Inspect request IP address
**/etc/nginx/sites-enabled/tvux.me**
```
server {
server_name tvux.me;
listen 80;
location / {
proxy_pass http://backend;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
```
Now you can get the request IP address from `request.headers['X-Real-IP']`
## CORS
**/etc/nginx/sites-enabled/tvux.me**
```
server {
server_name tvux.me;
listen 80;
location / {
proxy_pass http://backend;
proxy_http_version 1.1;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Headers *;
proxy_set_header Host $host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
```
## Worker connections
By default, Nginx uses 768 worker_connections. It'll fail to serve your app if there is a massive amount of requests at the same time. A simple solution is increase worker_connections to a larger number.
**/etc/nginx/nginx.conf**
```
events {
worker_connections 20000;
}
```
## Increase maximum request body size
By default, Nginx uses 10M for maximum body size (request's body).
**/etc/nginx/nginx.conf**
```
http {
client_max_body_size 300M;
}
```
## Read access log
```
cat /var/log/nginx/access.log
```
## Read error log
```
cat /var/log/nginx/error.log
```