1. 程式人生 > >2018最新nginx對nodejs伺服器的http、https、ws、wss的配置

2018最新nginx對nodejs伺服器的http、https、ws、wss的配置

目錄

文章目錄

軟體版本

  1. Linux 的centos7系統
  2. nodejs:v8.11.1
  3. nginx: v1.12.1
  4. 伺服器:(其實跟配置nginx沒有什麼關係)
    短連結:使用express
    長連線:使用websocket

話不多說上乾貨

靜態資源配置

檔名*.conf
server{
    listen 80;
	listen 443 ssl;
	server_name xxx.xxxx.xxx; # 域名或者localhost或者ip
	client_max_body_size 200M;
	ssl_certificate /**.pem;
	ssl_certificate_key /**.key;
	location ~ ^/(css/|fonts/|images/|js/){ # 訪問靜態資源
		root /**/public;#靜態檔案路徑(http://xxx.xxxx.xxx/css==訪問/**/public/css下的檔案)
		access_log off;
		expires max;
	}
	location ~ .*\.(gif|jpg|jpeg|png)$ # 快取
	{
		expires 30d;
	}
	location / { # 訪問靜態網頁
   		root        /root/project/**; # 靜態網頁的路徑
    		index        index.php index.html index.html;
	}
}

注:
server_name:域名or localhost or IP

listen 443 ssl:設定https訪問模式

**ssl_certificate /****.pem *:https的安全證書的pem檔案

**ssl_certificate_key /****.key *:https的安全證書的key檔案(因為我用的是阿里雲伺服器,所以這兩個檔案是從阿里雲管理平臺申請的證書,申請時間挺快的)

反向代理配置

因為我主要用於一個小型的nodejs伺服器,所以登入用短連結,遊戲中用長連線實現
檔名*.conf

	upstream ws{#長連線伺服器 負載均衡
		server 127.0.0.1:6080;#遊戲伺服器1
		server 127.0.0.1:6081;#遊戲伺服器2
		server 127.0.0.1:6082;#遊戲伺服器3
		server 127.0.0.1:6083;#遊戲伺服器4
		server 127.0.0.1:6084;
		...
		keepalive 3000;
	}
	server{//短連線
	    listen 0.0.0.0:80;
		listen 443 ssl;
	    server_name xx.xxxxx.xxx; # 同上
		ssl_certificate /etc/nginx/conf.d/*.pem;
		ssl_certificate_key /etc/nginx/conf.d/*.key;
		location ~ ^/(css/|fonts/|images/|js/){
			root /root/project/***/public;
			access_log off;
			expires 10d;
		}
		location ~ .*\.(gif|jpg|jpeg|png)$
		{
			expires 30d;
		}
	    location /{
		proxy_pass_request_headers on;
	    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-Nginx-Proxy true;
		proxy_redirect off;
		client_max_body_size 10m;#傳輸資料的大小
	    proxy_pass http://127.0.0.1:6000;
	}
	server{#長連線
	    listen 80;
	    listen 443 ssl;
	    server_name xx.xxxx.xxx;
		ssl_certificate /etc/nginx/conf.d/*.pem;
		ssl_certificate_key /etc/nginx/conf.d/*.key;
		location ~ ^/(css/|fonts/|images/|js/){
			root /root/project/***/public;
			access_log off;
			expires 10d;
		}
	    location / {
			proxy_http_version 1.1;
		    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-Nginx-Proxy true;
			proxy_redirect off;
			client_max_body_size 10m;
		    proxy_pass http://ws;
			proxy_set_header Upgrade $http_upgrade;
		    proxy_set_header Connection "upgrade";
			proxy_connect_timeout 300s; #配置點1
		    proxy_read_timeout 300s; #配置點2,如果沒效,可以考慮這個時間配置長一點
		    proxy_send_timeout 300s; #配置點3
	   }
	}

注:
upstream ws :配置負載均衡,nginx會隨負載均衡演算法隨機的把長連線請求轉接到此區域中的某一個連線。(我這裡主要是用於:擴充使用者的長連線連線數。)

proxy_pass:代理請求路徑。自己伺服器端的路徑。

proxy_pass http://ws:長連線負載均衡的配置

proxy_connect_timeout 300s or proxy_read_timeout 300s or proxy_send_timeout 300s:主要是配置nginx對長連線的保持時間,如果此長連線一段時間不請求任何命令後,nginx會在此時間後斷開此連結。一般會在客戶端設定一個心跳,在小於此時間後發起一次請求,用以保持此長連線的連線(這個是我的解決辦法,不知道是否有更好的方法,歡迎提出來,學習一下)

大致到這裡,你就可以遠端訪問你的伺服器了。

nodejs簡單的遊戲伺服器請點選 github專案地址
nodejs實現第三方登入請點選 nodejs服務端實現微信小遊戲登入