溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

nginx proxy_pass反向代理配置實(shí)例分析

發(fā)布時(shí)間:2022-04-28 13:52:57 來(lái)源:億速云 閱讀:349 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“nginx proxy_pass反向代理配置實(shí)例分析”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“nginx proxy_pass反向代理配置實(shí)例分析”吧!

下面舉個(gè)小實(shí)例說(shuō)明下:

centos7系統(tǒng)庫(kù)中默認(rèn)是沒有nginx的rpm包的,所以我們自己需要先更新下rpm依賴庫(kù)

1)使用yum安裝nginx需要包括nginx的庫(kù),安裝nginx的庫(kù)

[root@localhost ~]# rpm -uvh http://nginx.org/packages/centos/7/noarch/rpms/nginx-release-centos-7-0.el7.ngx.noarch.rpm

2)使用下面命令安裝nginx

[root@localhost ~]# yum install nginx

3)nginx配置

[root@localhost ~]# cd /etc/nginx/conf.d/
[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
 
[root@localhost conf.d]# cat /var/www/html/index.html
this is page of test!!!!

4)啟動(dòng)nginx

[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service

5)測(cè)試訪問(wèn)(103.110.186.23是192.168.1.23機(jī)器的外網(wǎng)ip)

[root@localhost conf.d]# curl http://192.168.1.23
this is page of test!!!!

看看下面幾種情況:分別用http://192.168.1.23/proxy/index.html進(jìn)行訪問(wèn)測(cè)試

為了方便測(cè)試,先在另一臺(tái)機(jī)器192.168.1.5上部署一個(gè)8090端口的nginx,配置如下:

[root@bastion-idc ~]# cat /usr/local/nginx/conf/vhosts/haha.conf
server {
listen 8090;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
}
[root@bastion-idc ~]# cat /var/www/html/index.html
this is 192.168.1.5
[root@bastion-idc ~]# /usr/local/nginx/sbin/nginx -s reload

測(cè)試訪問(wèn)(103.110.186.5是192.168.1.5的外網(wǎng)ip):

[root@bastion-idc ~]# curl http://192.168.1.5:8090
this is 192.168.1.5

nginx proxy_pass反向代理配置實(shí)例分析

192.168.1.23作為nginx反向代理機(jī)器,nginx配置如下:

1)第一種情況:

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090/;
}
}

這樣,訪問(wèn)http://192.168.1.23/proxy/就會(huì)被代理到http://192.168.1.5:8090/。p匹配的proxy目錄不需要存在根目錄/var/www/html里面

注意,終端里如果訪問(wèn)http://192.168.1.23/proxy(即后面不帶"/"),則會(huì)訪問(wèn)失??!因?yàn)閜roxy_pass配置的url后面加了"/"

[root@localhost conf.d]# curl http://192.168.1.23/proxy/
this is 192.168.1.5
[root@localhost conf.d]# curl http://192.168.1.23/proxy
<html>
<head><title>301 moved permanently</title></head>
<body bgcolor="white">
<center><h1>301 moved permanently</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

頁(yè)面訪問(wèn)http://103.110.186.23/proxy的時(shí)候,會(huì)自動(dòng)加上"/”(同理是由于proxy_pass配置的url后面加了"/"),并反代到http://103.110.186.5:8090的結(jié)果

nginx proxy_pass反向代理配置實(shí)例分析

2)第二種情況,proxy_pass配置的url后面不加"/"

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service

那么訪問(wèn)http://192.168.1.23/proxy或http://192.168.1.23/proxy/,都會(huì)失??!

這樣配置后,訪問(wèn)http://192.168.1.23/proxy/就會(huì)被反向代理到http://192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置實(shí)例分析

3)第三種情況

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090/haha/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/
192.168.1.5 haha-index.html

這樣配置的話,訪問(wèn)http://103.110.186.23/proxy代理到http://192.168.1.5:8090/haha/

nginx proxy_pass反向代理配置實(shí)例分析

4)第四種情況:相對(duì)于第三種配置的url不加"/"

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy/ {
 proxy_pass http://192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html

上面配置后,訪問(wèn)http://192.168.1.23/proxy/index.html就會(huì)被代理到http://192.168.1.5:8090/hahaindex.html
同理,訪問(wèn)http://192.168.1.23/proxy/test.html就會(huì)被代理到http://192.168.1.5:8090/hahatest.html

[root@localhost conf.d]# curl http://192.168.1.23/proxy/index.html
192.168.1.5 hahaindex.html

注意,這種情況下,不能直接訪問(wèn)http://192.168.1.23/proxy/,后面就算是默認(rèn)的index.html文件也要跟上,否則訪問(wèn)失?。?/p>

nginx proxy_pass反向代理配置實(shí)例分析

-------------------------------------------------------------------------------------
上面四種方式都是匹配的path路徑后面加"/",下面說(shuō)下path路徑后面不帶"/"的情況:

1)第一種情況,proxy_pass后面url帶"/":

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service

nginx proxy_pass反向代理配置實(shí)例分析

nginx proxy_pass反向代理配置實(shí)例分析

2)第二種情況,proxy_pass后面url不帶"/"

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service
[root@localhost conf.d]#

這樣配置的話,訪問(wèn)http://103.110.186.23/proxy會(huì)自動(dòng)加上"/”(即變成http://103.110.186.23/proxy/),代理到192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置實(shí)例分析

3)第三種情況

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090/haha/;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service

這樣配置的話,訪問(wèn)http://103.110.186.23/proxy會(huì)自動(dòng)加上"/”(即變成http://103.110.186.23/proxy/),代理到http://192.168.1.5:8090/haha/

nginx proxy_pass反向代理配置實(shí)例分析

4)第四種情況:相對(duì)于第三種配置的url不加"/"

[root@localhost conf.d]# cat test.conf
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
index index.html;
}
 
location /proxy {
 proxy_pass http://192.168.1.5:8090/haha;
}
}
[root@localhost conf.d]# service nginx restart
redirecting to /bin/systemctl restart nginx.service

nginx proxy_pass反向代理配置實(shí)例分析

這樣配置的話,訪問(wèn)http://103.110.186.23/proxy,和第三種結(jié)果一樣,同樣被代理到http://192.168.1.5:8090/haha/

到此,相信大家對(duì)“nginx proxy_pass反向代理配置實(shí)例分析”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI