日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

nginx的反向代理proxy_pass指令

發布時間:2024/1/8 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 nginx的反向代理proxy_pass指令 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

nginx的反向代理proxy_pass指令

1. 首先什么是代理服務器?
客戶機發送請求時,不會直接發送到目的主機,而是先被代理服務器收到,代理服務器收到客服機的請求后,再向目的機發出,目的機就會返回數據給客戶機,在返回給客戶機之前,會被代理服務器先收到,會存放在代理服務器的硬盤中。然后代理服務器會再向客戶機發出,最后客戶機就會收到目的機返回的數據。

2. 代理服務器的作用有哪些?

1) 可以提高訪問速度
因為目標主機返回的數據會存放在代理服務器的硬盤中,因此下一次客戶機再次訪問相同的站點數據的時候,會直接從代理服務器的硬盤中讀取,因此響應速度會更快。

2)防火墻的作用
由于所有的客戶機請求都必須通過代理服務器訪問遠程站點,因此可在代理服務器上設限,過濾一些不安全的信息。

3. 理解什么是反向代理?

反向代理是指以代理服務器接收Internet上的鏈接請求,然后將請求轉發給內部網絡上的服務器,并將從服務器上得到的結果返回給Internet上請求連接的客戶端。

什么意思呢?上面的解釋可能有點不好理解,那么下面我們先來打個比方,比如我的內部服務器是放在212環境上,那么開發的接口如下這樣的:
http://192.168.1.212:8136/xxxx 然后端口號是8136,然后直接訪問該接口會返回對應的數據,但是接口一般都是域名訪問的,因此我們需要在nginx上配置一個域名,假如為 xy.xxx.com, 然后當我們在聯調接口的時候,我們使用 http://xy.xxx.com/xxxx 這樣的接口時,它會反向代理到 http://192.168.1.212:8136/xxxx 上來,這樣它會返回內部服務器的數據給客戶端,客戶端就拿到對應的數據顯示出來了。

3.1 理解proxy_pass指令
該指令是用來設置代理服務器的地址,可以是主機名稱,IP地址加端口號等形式。基本語法如下所示:

proxy_pass: URL;

因此我們經常會看到如下nginx上的配置:如下代碼:

server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location / {proxy_pass http://192.168.1.212:8136;include nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

如上代碼的含義是:監聽80端口號,然后我們定義的接口的域名為 xy.xxx.com, 然后當我們訪問 http://xy.xxx.com/xxxx這樣的接口的時候,它會通過 location / {} 這樣的反向代理到 http://192.168.1.212:8136上來,當然對于我們的host也需要綁定下 192.168.1.212 xy.xxx.com 就可以了。

當然如果代理服務器是一組服務器的話,我們可以使用upstream指令配置后端服務器組。如下代碼:

upstream proxy_xxx {server http://192.168.1.211:8136/xxx;server http://192.168.1.212:8136/xxx;server http://192.168.1.213:8136/xxx; }server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location / {proxy_pass proxy_xxx; // 對應上面的upstream 后面的 proxy_xxxinclude nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

但是在上面配置各個服務器中都指明了傳輸協議為 http://, 但是如果上面的接口沒有指明協議的話,那么我們需要在 proxy_pass上加上了,proxy_pass http://proxy_xxx 這樣的,如下配置代碼:

upstream proxy_xxx {server 192.168.1.211:8136/xxx;server 192.168.1.212:8136/xxx;server 192.168.1.213:8136/xxx; }server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location / {proxy_pass http://proxy_xxx; // 對應上面的upstream 后面的 proxy_xxxinclude nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

注意點:

1. 當我們配置是如下配置:

server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location / {proxy_pass http://192.168.1.212:8136;include nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

當用戶使用接口 http://xy.xxx.com/xxx 的時候,nginx會自動指向內部服務器 http://192.168.1.212:8136/xxx的。這個我們能理解的。

2. 當我們的nginx的配置是如下的:

server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location / {proxy_pass http://192.168.1.212:8136/yyy;include nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

注意上面的 proxy_pass http://192.168.1.212:8136/yyy; 如果客戶端還是以 http://xy.xxx.com/xxx 訪問接口的時候,那么nginx服務器就會將請求地址指向與 http://192.168.1.212:8136/yyy了,而不是http://192.168.1.212:8136/xxx了。

因此如果我們在正常配置中,我們可以選擇第一種配置,直接指向域名,然后反向代理到 ip地址+端口號即可。

3. 理解 proxy_pass http://192.168.1.212 和 proxy_pass http://192.168.1.212/的區別;

上面的兩者的區別是 proxy_pass 指令的URL變量末尾添加了斜杠 '/', 下面我們再來看下nginx的配置,如下代碼:

server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location / {#配置1 proxy_pass http://192.168.1.212:8136;#配置2 proxy_pass http://192.168.1.212:8136/;include nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

在如上配置中,location塊使用了 '/' 作為uri變量的值來匹配的,因此上面的配置1和配置2效果是相同的,比如客戶端的接口請求是:
http://xy.xxx.com/xxx的時候,不管使用配置1還是配置2,都會指向內部服務器 http://192.168.1.212:8936/xxx.

但是現在我們把代碼改成如下配置,那就不一樣了:

server {listen 80;server_name xy.xxx.com; // 接口的域名access_log /data/www/logs/nginx/access.log main;add_header Access-Control-Allow-Origin http://xy.xxx.com; // 允許的域名跨域add_header Access-Control-Allow-Credentials true;include nginx_xxx.conf;location /bus/ {#配置1 proxy_pass http://192.168.1.212:8136;#配置2 proxy_pass http://192.168.1.212:8136/;include nginx_proxy.conf;}error_page 500 502 503 504 /502.html;location = /50x.html {root html;} }

注意:上面的 location /bus/ , location塊使用了 /bus/ 作為uri變量的值來匹配,

比如我們現在客戶端請求 http://xy.xxx.com/bus/xxx的時候,如果使用配置1的話,那么它會指向內部服務器的地址為:http://192.168.1.212:8136/bus/xxx,

那如果我們使用配置2的話,那么它會指向內部服務器的地址為:http://192.168.1.212:8136/xxx, 這樣的。

?

因此這就是加上 反斜杠 /? ?的區別了。

總結

以上是生活随笔為你收集整理的nginx的反向代理proxy_pass指令的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。