五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

Nginx如何配置Http、Https、WS、WSS的方法步驟

發(fā)布日期:2021-12-21 03:55 | 文章來(lái)源:腳本之家

寫(xiě)在前面

當(dāng)今互聯(lián)網(wǎng)領(lǐng)域,Nginx是使用最多的代理服務(wù)器之一,很多大廠在自己的業(yè)務(wù)系統(tǒng)中都是用了Nginx作為代理服務(wù)器。所以,我們有必要了解下Nginx對(duì)于Http、Https、WS、WSS的各項(xiàng)配置。來(lái)來(lái)來(lái),跟冰河一些學(xué)習(xí)Nginx,一起進(jìn)階,一起頭禿~~

Nginx配置Http

首先,我們來(lái)聊聊Nginx如何配置Http,Nginx配置Http是Nginx最常用的功能之一。在nginx.conf中配置相應(yīng)的信息,如下所示。

upstream message {
  server localhost:8080 max_fails=3;
}
server {
 listen       80;
 server_name  localhost;
 location / {
  root   html;
  index  index.html index.htm;
  #允許cros跨域訪問(wèn) 
  add_header 'Access-Control-Allow-Origin' '*';
  #proxy_redirect default;
  #跟代理服務(wù)器連接的超時(shí)時(shí)間,必須留意這個(gè)time out時(shí)間不能超過(guò)75秒,當(dāng)一臺(tái)服務(wù)器當(dāng)?shù)魰r(shí),過(guò)10秒轉(zhuǎn)發(fā)到另外一臺(tái)服務(wù)器。
  proxy_connect_timeout 10;
 }
 
  location /message {
    proxy_pass                  http://message;
    proxy_set_header Host $host:$server_port;
 }
}

此時(shí),訪問(wèn) http://localhost/message,就會(huì)被轉(zhuǎn)發(fā)到 http://localhost:8080/message 上。

Nginx配置Https

如果業(yè)務(wù)對(duì)于網(wǎng)站的安全性要求比較高,此時(shí)可能就會(huì)在Nginx配置Https,具體配置信息可以參照如下方式進(jìn)行。

upstream message {
  server localhost:8080 max_fails=3;
}
server {
 listen       443 ssl;
 server_name localhost;
 ssl_certificate    /usr/local/nginx-1.17.8/conf/keys/binghe.pem;
 ssl_certificate_key /usr/local/nginx-1.17.8/conf/keys/binghe.key;
 ssl_session_timeout 20m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_verify_client off;
 location / {
  root   html;
  index  index.html index.htm;
  #允許cros跨域訪問(wèn) 
  add_header 'Access-Control-Allow-Origin' '*';
  #跟代理服務(wù)器連接的超時(shí)時(shí)間,必須留意這個(gè)time out時(shí)間不能超過(guò)75秒,當(dāng)一臺(tái)服務(wù)器當(dāng)?shù)魰r(shí),過(guò)10秒轉(zhuǎn)發(fā)到另外一臺(tái)服務(wù)器。
  proxy_connect_timeout 10;
 }
 
  location /message {
    proxy_pass                  http://message;
    proxy_set_header Host $host:$server_port;
 }
}

此時(shí)訪問(wèn)https://localhost/message 就會(huì)被轉(zhuǎn)發(fā)到 http://localhost:8080/message上。

Nginx配置WS

WS的全稱是WebSocket,Nginx配置WebSocket也比較簡(jiǎn)單,只需要在nginx.conf文件中進(jìn)行相應(yīng)的配置。這種方式很簡(jiǎn)單,但是很有效,能夠橫向擴(kuò)展WebSocket服務(wù)端的服務(wù)能力。

為了方便小伙伴們更好的理解,這里,我重點(diǎn)說(shuō)下Nginx配置WS。

先直接展示配置文件,如下所示(使用的話直接復(fù)制,然后改改ip和port即可)

map $http_upgrade $connection_upgrade { 
 default upgrade; 
 '' close; 
} 
upstream wsbackend{ 
 server ip1:port1; 
 server ip2:port2; 
 keepalive 1000;
} 
 
server { 
 listen 20038; 
 location /{ 
  proxy_http_version 1.1; 
  proxy_pass http://wsbackend; 
  proxy_redirect off; 
  proxy_set_header Host $host; 
  proxy_set_header X-Real-IP $remote_addr; 
  proxy_read_timeout 3600s; 
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  proxy_set_header Upgrade $http_upgrade; 
  proxy_set_header Connection $connection_upgrade; 
 } 
}

接下來(lái),我們就分別分析上述配置的具體含義。

首先:

map $http_upgrade $connection_upgrade { 
 default upgrade; 
 '' close; 
} 

表示的是:

  • 如果$http_upgrade 不為 '' (空), 則$connection_upgrade 為 upgrade 。
  • 如果$http_upgrade 為 '' (空), 則 $connection_upgrade 為 close。

其次:

upstream wsbackend{ 
 server ip1:port1; 
 server ip2:port2; 
 keepalive 1000; 
} 

表示的是 nginx負(fù)載均衡:

兩臺(tái)服務(wù)器 (ip1:port1)和(ip2:port2) 。

keepalive 1000 表示的是每個(gè)nginx進(jìn)程中上游服務(wù)器保持的空閑連接,當(dāng)空閑連接過(guò)多時(shí),會(huì)關(guān)閉最少使用的空閑連接.當(dāng)然,這不是限制連接總數(shù)的,可以想象成空閑連接池的大小,設(shè)置的值應(yīng)該是上游服務(wù)器能夠承受的。

最后:

server { 
 listen 20038; 
 location /{ 
  proxy_http_version 1.1; 
  proxy_pass http://wsbackend; 
  proxy_redirect off;
  proxy_set_header Host $host; 
  proxy_set_header X-Real-IP $remote_addr; 
  proxy_read_timeout 3600s; 
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
  proxy_set_header Upgrade $http_upgrade; 
  proxy_set_header Connection $connection_upgrade; 
 } 
} 

表示的是監(jiān)聽(tīng)的服務(wù)器的配置

  • listen 20038 表示 nginx 監(jiān)聽(tīng)的端口
  • locations / 表示監(jiān)聽(tīng)的路徑(/表示所有路徑,通用匹配,相當(dāng)于default)
  • proxt_http_version 1.1 表示反向代理發(fā)送的HTTP協(xié)議的版本是1.1,HTTP1.1支持長(zhǎng)連接
  • proxy_pass http://wsbackend; 表示反向代理的uri,這里可以使用負(fù)載均衡變量
  • proxy_redirect off; 表示不要替換路徑,其實(shí)這里如果是/則有沒(méi)有都沒(méi)關(guān)系,因?yàn)閐efault也是將路徑替換到proxy_pass的后邊
  • proxy_set_header Host $host; 表示傳遞時(shí)請(qǐng)求頭不變, $host是nginx內(nèi)置變量,表示的是當(dāng)前的請(qǐng)求頭,proxy_set_header表示設(shè)置請(qǐng)求頭
  • proxy_set_header X-Real-IP $remote_addr; 表示傳遞時(shí)來(lái)源的ip還是現(xiàn)在的客戶端的ip
  • proxy_read_timeout 3600s; 表的兩次請(qǐng)求之間的間隔超過(guò) 3600s 后才關(guān)閉這個(gè)連接,默認(rèn)的60s,自動(dòng)關(guān)閉的元兇
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 表示X-Forwarded-For頭不發(fā)生改變
  • proxy_set_header Upgrade $http_upgrade; 表示設(shè)置Upgrade不變
  • proxy_set_header Connection $connection_upgrade; 表示如果 $http_upgrade為upgrade,則請(qǐng)求為upgrade(websocket),如果不是,就關(guān)閉連接

此時(shí),訪問(wèn) ws://localhost:20038 就會(huì)被轉(zhuǎn)發(fā)到 ip1:port1 和 ip2:port2 上。

Nginx配置WSS

WSS表示W(wǎng)ebSocket + Https,通俗點(diǎn)說(shuō),就是安全的WebSocket,接下來(lái),我們來(lái)看看如何配置WSS。在配置WS時(shí),詳細(xì)描述了配置的細(xì)節(jié)信息,這里,我就不詳細(xì)介紹了。

map $http_upgrade $connection_upgrade { 
 default upgrade; 
 '' close; 
} 
upstream wsbackend{ 
 server ip1:port1; 
 server ip2:port2; 
 keepalive 1000; 
} 
server{
 listen 20038 ssl;
 server_name localhost;
 ssl_certificate    /usr/local/nginx-1.17.8/conf/keys/binghe.com.pem;
 ssl_certificate_key /usr/local/nginx-1.17.8/conf/keys/binghe.com.key;
 ssl_session_timeout 20m;
 ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_verify_client off;
 location /{
   proxy_http_version 1.1;
   proxy_pass http://wsbackend;
   proxy_redirect off; 
   proxy_set_header Host $host; 
   proxy_set_header X-Real-IP $remote_addr; 
   proxy_read_timeout 3600s; 
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
   proxy_set_header Upgrade $http_upgrade; 
   proxy_set_header Connection $connection_upgrade; 
 }
}

此時(shí),訪問(wèn) wss://localhost:20038 就會(huì)被轉(zhuǎn)發(fā)到 ip1:port1 和 ip2:port2 上。

到此這篇關(guān)于Nginx如何配置Http、Https、WS、WSS的方法步驟的文章就介紹到這了,更多相關(guān)Nginx配置Http、Https、WS、WSS內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶服務(wù)

7x24全年不間斷在線

專屬顧問(wèn)服務(wù)

1對(duì)1客戶咨詢顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部