溫馨提示×

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

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

Nginx多層代理如何配置

發(fā)布時(shí)間:2021-08-20 13:57:38 來源:億速云 閱讀:200 作者:小新 欄目:服務(wù)器

這篇文章將為大家詳細(xì)講解有關(guān)Nginx多層代理如何配置,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

此篇只說nginx的多級(jí)代理配置,不扯其他的.

需求:hba.changyoufun.com-121.201.125.239(gd1)--hk1--co(alphaclash.ggdev.co)  廣東代理--->香港--->加拿大

由于idc機(jī)房在加拿大,所以經(jīng)常會(huì)配些nginx多級(jí)反向代理到國(guó)內(nèi).(不做代理client就得翻墻,或者說是丟包很嚴(yán)重.)

下面的Nginx配置我只寫80的,443的忽略,簡(jiǎn)化nginx的配置,參數(shù)也不一一列舉不然太多了.

gd1的反向代理配置:(就是一個(gè)反向代理)

cat hba.changyoufun.com.conf
server{
 listen 121.201.125.239:80;
 
 server_name hba.changyoufun.com;
 access_log /data/weblogs/hba.changyoufun.com.access.log main;
 index index.html index.php index.htm;
 location / {
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_read_timeout 600;
    proxy_connect_timeout 600;
    proxy_pass http://hba80;
   }
}
Upstream配置(代理的是hk1的內(nèi)網(wǎng)ip):
 upstream hba80 {
  server 10.105.3.222:80;   
 }
 upstream hba443 {
  server 10.105.3.222:443;   
 }

hk1的反向代理配置:(也是一個(gè)nginx反向代理)

cat hba.changyoufun.com.conf
server{
 listen 10.105.3.222:80;
 server_name hba.changyoufun.com;
 access_log /data/weblogs/hba.changyoufun.com.access.log main;
 index index.html index.php index.htm;
 location / {
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_read_timeout 600;
    proxy_connect_timeout 600;
    proxy_pass http://hba80;
   }
}

Upstream配置(代理的是alphaclash.ggdev.co域名的ip地址):

upstream hba80 {
  server 216.66.17.34:80;   
 }
 upstream hba443 {
  server 216.66.17.34:443;   
 }

co機(jī)房原先已經(jīng)存在alphaclash.ggdev.co域名的配置,只需要copy一份alphaclash.ggdev.co的nginx配置,將域名替換為hba.changyoufun.com即可.

co的兩份nginx配置:

nginx和php的web版:

cat hba.changyoufun.com.conf
server{
  listen 216.66.17.34:80; 
 server_name hba.changyoufun.com ;
 access_log /data/weblogs/hba.changyoufun.com.access.log main;
 index index.html index.php index.htm;
root /product/clash/alpha/web/htdocs;
location ~ ^/.*(do|php)$ {
  fastcgi_pass php_proxy;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_script_name;
  include /etc/nginx/fastcgi_params;
 }
}
cat alphaclash.ggdev.co.conf
server{
  listen 216.66.17.34:80; 
 server_name alphaclash.ggdev.co;
 access_log /data/weblogs/alphaclash.ggdev.co.access.log main;
 index index.html index.php index.htm;
root /product/clash/alpha/web/htdocs;
location ~ ^/.*(do|php)$ {
  fastcgi_pass php_proxy;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  fastcgi_param PATH_INFO $fastcgi_script_name;
  include /etc/nginx/fastcgi_params;
 }
}

注釋:其實(shí)nginx的代理很簡(jiǎn)單,很多時(shí)候是沒想明白,既然代理alphaclash.ggdev.co,前面兩級(jí)代理配置了這個(gè)域名不就ok了?仔細(xì)想發(fā)現(xiàn)nginx代理根據(jù)ip:端口找域名,然后在location匹配到的location段再找upstream段,再根據(jù)upstream段的ip+port找下級(jí)域名,最后發(fā)現(xiàn)最后一級(jí)沒有相匹配的域名就報(bào)404了,所以最后一層也要配上相匹配的域名配置.

關(guān)于“Nginx多層代理如何配置”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(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