Nginx多層代理如何配置-創(chuàng)新互聯(lián)

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

成都創(chuàng)新互聯(lián)公司長(zhǎng)期為成百上千家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為左云企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè),左云網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

此篇只說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)把它分享出去讓更多的人看到。

網(wǎng)站名稱:Nginx多層代理如何配置-創(chuàng)新互聯(lián)
文章源于:http://muchs.cn/article14/cecsge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管全網(wǎng)營(yíng)銷推廣、關(guān)鍵詞優(yōu)化網(wǎng)站維護(hù)、電子商務(wù)軟件開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化