首頁 > 運維 > Nginx > 主體

nginx proxy_pass反向代理程式設定實例分析

王林
發布: 2023-05-13 23:19:11
轉載
1145 人瀏覽過

下面舉個小實例說明下:

centos7系統函式庫中預設是沒有nginx的rpm包的,所以我們自己需要先更新下rpm依賴函式庫

1)使用yum安裝nginx需要包含nginx的函式庫,安裝nginx的函式庫

[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)啟動nginx

[root@localhost ~]# service nginx start //或者使用 systemctl start nginx.service
登入後複製

5)測試存取(103.110.186.23是192.168.1.23機器的外網)

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

看看以下幾種情況:分別用http://192.168.1.23/proxy/index.html來存取測試

為了方便測試,先在另一台機器192.168.1.5上部署一個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
登入後複製

測試存取(103.110.186.5是192.168.1.5的外網路ip):

[root@bastion-idc ~]# curl http://192.168.1.5:8090
this is 192.168.1.5
登入後複製

nginx proxy_pass反向代理配置实例分析

#192.168.1.23作為nginx反向代理機器,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/;
}
}
登入後複製

這樣,訪問http://192.168.1.23/proxy/就會被代理到http://192.168.1.5:8090/。 p匹配的proxy目錄不需要存在根目錄/var/www/html裡面


注意,終端機如果訪問http://192.168.1.23/proxy(即後面不帶"/") ,則會存取失敗!因為proxy_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>
登入後複製

頁面訪問http://103.110.186.23/proxy的時候,會自動加上"/”(同理是由於proxy_pass配置的url後面加了"/"),並反代到http://103.110.186.5:8090的結果

nginx proxy_pass反向代理配置实例分析

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
登入後複製

那麼訪問http://192.168.1.23/proxy或http://192.168.1.23/proxy/,都會失敗!


這樣設定後,訪問http://192.168.1.23/proxy/就會被反向代理到http://192.168.1.5:8090/proxy/

nginx proxy_pass反向代理配置实例分析

#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
登入後複製

這樣設定的話,訪問http://103.110.186.23/proxy代理到http://192.168.1.5:8090/ haha/

nginx proxy_pass反向代理配置实例分析

4)第四種情況:相對於第三種配置的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
登入後複製

上面配置後,訪問http ://192.168.1.23/proxy/index.html就會被代理到http://192.168.1.5:8090/hahaindex.html

同理,訪問http://192.168.1.23/proxy/test.html就會被代理到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
登入後複製

注意,這種情況下,不能直接存取http://192.168.1.23/proxy/,後面就算是預設的index.html檔案也要跟上,否則存取失敗!

nginx proxy_pass反向代理配置实例分析

-------------------------------- -------------------------------------------------- ---

上面四種方式都是符合的path路徑後面加上"/",下面說下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反向代理配置实例分析

nginx proxy_pass反向代理配置实例分析

#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]#
登入後複製

這樣設定的話,訪問http://103.110.186.23/proxy會自動加上"/」(即變成http://103.110.186.23/proxy/),代理到192.168.1.5: 8090/proxy/

nginx proxy_pass反向代理配置实例分析

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
登入後複製

這樣配置的話,訪問http://103.110.186.23/proxy會自動加上"/」(即變成http://103.110.186.23/proxy/),代理程式到http://192.168.1.5:8090/haha/

nginx proxy_pass反向代理配置实例分析

#4)第四種情況:相對於第三種配置的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反向代理配置实例分析

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

以上是nginx proxy_pass反向代理程式設定實例分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!