升級 Nginx 來支持 WebSocket


來記錄一下新的 Nginx 支持 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
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
# 先來安裝 1.3.14
wget http://nginx.org/download/nginx-1.3.14.tar.gz
tar zxvf nginx-1.3.14.tar.gz
cd nginx-1.3.14
./configure --prefix=/usr/local/nginx --user=www --group=www --with-select_module --with-poll_module --with-file-aio --with-http_ssl_module --with-http_realip_module --with-http_geoip_module --with-http_gzip_static_module --with-http_secure_link_module --with-http_sub_module --with-http_stub_status_module --with-http_perl_module
make && make install

# 殺掉 Nginx 和重啟
killall nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

# 配置使用 Websocket 的域名
vim /usr/local/nginx/conf/[some directory]/sub.domain.com.conf

server {
listen 80;
charset utf-8;
server_name sub.domain.com;
root /home/user/sub.domain.com;

location / {
proxy_pass http://127.0.0.1:3330/;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;

proxy_redirect off;
}

location /status {
stub_status on;
access_log off;
}

location ~ /\.ht {
deny all;
}
}

# 重新加載域名設置檔
/usr/local/nginx/nginx.sh reload

# 執行應用
nodemon app.js