nginx對nodejs服務器的http、https、ws、wss的配置


最新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

  • 域名:用戶可以直接在瀏覽器地址訪問http://域名/(默認80端口)or https://域名/(默認443)
  • localhost and IP:用戶可以直接在瀏覽器地址訪問http://公網IP/(默認80端口)or https://公網IP/(默認443)

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服務端實現微信小游戲登錄

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM