nginx反向代理,虛擬主機(jī)

nginx反向代理,虛擬主機(jī)

一、配置虛擬主機(jī)
1.基于端口配置虛擬主機(jī)
1.1配置文件:

在五大連池等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站制作、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站策劃,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),營(yíng)銷型網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站建設(shè)公司,五大連池網(wǎng)站建設(shè)費(fèi)用合理。

[root@localhost conf.d]# vim vhost.conf 
server {
        listen       8080 ;
        root         /usr/share/nginx/html;
        location / {
        }
}
server {
        listen       8081;
        root         /usr/share/nginx/html;
        location / {
        }
}

1.2驗(yàn)證:
nginx反向代理,虛擬主機(jī)
nginx反向代理,虛擬主機(jī)
nginx反向代理,虛擬主機(jī)
2.基于IP地址配置虛擬主機(jī):
2.1配置文件:

[root@localhost conf.d]# vim vhost.conf 
server {
        listen      172.20.10.9:8080 ;
        root         /usr/share/nginx/html;
        location / {
        }
}
server {
        listen       172.20.10.11:8080;
        root         /usr/share/nginx/html;
        location / {
        }
}

2.2驗(yàn)證:
nginx反向代理,虛擬主機(jī)
nginx反向代理,虛擬主機(jī)nginx反向代理,虛擬主機(jī)

3.基于域名配置虛擬主機(jī)
3.1nginx配置文件:

[root@localhost conf.d]# vim vhost.conf 
server {
        listen      80;
        server_name  www.a.com;
        root      /data/a/html;
        location / {
        }
}
server {
        listen       80;
        server_name  www.b.com;
        root        /data/b/html;
        location / {
        }
}

3.2分別為虛擬主機(jī)配置root路徑

[root@localhost /]# mkdir -pv /data/a/html/
[root@localhost html]# vim  /data/a/html/index.html 
www.a.com
[root@localhost /]# mkdir -pv /data/b/html/
[root@localhost html]# vim  /data/b/html/index.html 
www.b.com

3.3client端配置hosts文件
配置windows主機(jī)在C:\Windows\System32\drivers\etc下的hosts文件:
nginx反向代理,虛擬主機(jī)
(4)驗(yàn)證:
nginx反向代理,虛擬主機(jī)

nginx反向代理,虛擬主機(jī)
二.nginx反向代理:
1.反向代理配置參數(shù):
1.1proxy_pass:
用來(lái)設(shè)置將客戶端請(qǐng)求轉(zhuǎn)發(fā)給的后端服務(wù)器主機(jī),可以是主機(jī)名,IP地址:端口的方式,也可以代理到預(yù)先設(shè)置的主機(jī)群組,需要模塊ngx_http_upstream_module支持

location /web {
            index index.html
            proxy_pass http://172.20.10.8:80;
             #不帶斜線將訪問(wèn)的/web,等于訪問(wèn)后端服務(wù)器http://172.20.10.8/web/index.html,即后端服務(wù)器配置的站點(diǎn)根目錄要有web目錄才可以訪問(wèn),這是一個(gè)追加/web到后端服務(wù)器http://servername:port/WEB/INDEX.HTML的操作
                        proxy_pass http://172.20.10.8:80/
                  #帶斜線,等于訪問(wèn)后端服務(wù)器的http://172.20.10.8:80/index.html內(nèi)容返回客戶端
                        }

1.2proxy_hide_header
#用于nginx作為反向代理的時(shí)候,在返回客戶端http響應(yīng)的時(shí)候,隱藏后端服務(wù)器版本相應(yīng)頭部的信息,可以設(shè)置在http/server或location塊;

location /web {
            index index.html;
            proxy_pass  http://172.20.10.8:80;
            proxy_hide_header ETag;
        }

1.3 proxy_pass_request_body on|off
#是否向后端服務(wù)器HTTP包體部分,可以設(shè)置在http/server或location塊,默認(rèn)即為開(kāi)啟
1.4 proxy_pass_request_headers on|off
#是否將客戶端的請(qǐng)求頭部轉(zhuǎn)發(fā)給后端服務(wù)器,可以設(shè)置在http/server或location塊,默認(rèn)即為開(kāi)啟
1.5proxy_set_header
#可以更改或添加客戶端的請(qǐng)求頭部信息內(nèi)容并轉(zhuǎn)發(fā)后端服務(wù)器,比如在后端服務(wù)器想要獲取客戶端的真是IP的時(shí)候,就要更改每一個(gè)報(bào)頭的頭部,如下:
proxy_set_header X-Forward-For $proxy_add_x_forward_for;
#proxy_set_header HOST $remote_addr;
#添加HOST到報(bào)文頭部,如果客戶端為NAT上網(wǎng)那么真實(shí)為客戶端的共用的公網(wǎng)IP地址
1.6proxy_hide_header field:
#用于隱藏后端服務(wù)器特定的響應(yīng)首部,默認(rèn)nginx在響應(yīng)報(bào)文中不傳遞后端服務(wù)器的首部字段Date,Server,X-Pad,X-Accel等
1.7proxy_connect_timeout 60;
#配置nginx服務(wù)器與后端服務(wù)器嘗試建立連接的超時(shí)時(shí)間,默認(rèn)為60s,
proxy_connect_timeout 60s
60s為自定義nginx與后端服務(wù)器建立連接的超時(shí)時(shí)間
1.8proxy_read_time time;
#配置nginx服務(wù)器向后端服務(wù)器或服務(wù)器組發(fā)起read發(fā)起read請(qǐng)求后,等待的超時(shí)時(shí)間,默認(rèn)為60s
proxy_send_time time;
#配置nginx后端服務(wù)器或服務(wù)器組發(fā)起write請(qǐng)求后,等待的超時(shí)時(shí)間,默認(rèn)為60s
1.9proxy_http_version 1.0:
#用于設(shè)置nginx提供代理服務(wù)的HTTP協(xié)議的版本,默認(rèn)http1.0
1.10proxy_ignore_client_abort off;
#當(dāng)客戶端網(wǎng)絡(luò)中斷時(shí),nginx服務(wù)器中斷其對(duì)后端服務(wù)器的請(qǐng)求。即如果此項(xiàng)設(shè)置為ON開(kāi)啟,則服務(wù)器忽略客戶端中斷并一直等著代理服務(wù)器執(zhí)行返回,如果設(shè)置為off,則客戶端中斷后nginx也會(huì)中斷客戶端并立即記錄日志,默認(rèn)為off
1.11proxy_headers_hash_bucket_size 64;
#當(dāng)配置proxy_hide_header和proxy_set_header的時(shí)候,用于設(shè)置nginx保存HTTP報(bào)文的hash表上限
proxy_headers_hash_max_size 512;
#設(shè)置proxy_headers_hash_max_size的最大可用空間
server_name_hash_bucket_size 512;
#設(shè)置server_name_hash表申請(qǐng)空間大小
server_names_hash_size 512;
#設(shè)置服務(wù)名稱hash表上限大小
2.反向代理示例:
2.1單臺(tái)web服務(wù)器

location / {
            proxy_pass  http://172.20.10.8;
        }

2.2指定location;

location /web {
            proxy_pass http://172.20.10.8:80/;
         }

注意后面的/
測(cè)試:

[root@node7 ~]# curl 172.20.10.9/web
172.20.10.8 index page

3緩存功能:
3.1proxy_cache zone | off; 默認(rèn)off
#指明調(diào)用的緩存,或關(guān)閉緩存機(jī)制;Context:http, server, location
3.2proxy_cache_key string;
#緩存中用于“鍵”的內(nèi)容,默認(rèn)值:proxy_cache_key $scheme$proxy_host$request_uri;
3.3proxy_cach#定義對(duì)特定響應(yīng)碼的響應(yīng)內(nèi)容的緩存時(shí)長(zhǎng),定義在http{...}中
示例:

proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;e_valid [code ...] time;

3.4proxy_cache_path;
定義可用于proxy功能的緩存;Context:http

proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size
[inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time]
[manager_threshold=time] [loader_files=number] [loader_sleep=time]
[loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time]
[purger_threshold=time];

示例:在http配置定義緩存信息

proxy_cache_path /var/cache/nginx/proxy_cache #定義緩存保存路徑,proxy_cache會(huì)自動(dòng)創(chuàng)
建
levels=1:2:2 #定義緩存目錄結(jié)構(gòu)層次,1:2:2可以生成2^4x2^8x2^8=1048576個(gè)目錄
keys_zone=proxycache:20m #指內(nèi)存中緩存的大小,主要用于存放key和metadata(如:使用次數(shù))
inactive=120s; #緩存有效時(shí)間
max_size=1g; #最大磁盤占用空間,磁盤存入文件內(nèi)容的緩存空間最大值

3.5#調(diào)用緩存功能,需要定義在相應(yīng)的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
3.6proxy_cache_use_stale;
#在被代理的后端服務(wù)器出現(xiàn)哪種情況下,可直接使用過(guò)期的緩存響應(yīng)客戶端,

proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |
http_502 | http_503 | http_504 | http_403 | http_404 | off ; #默認(rèn)是off
3.7proxy_cache_methods GET | HEAD | POST ...;

#對(duì)哪些客戶端請(qǐng)求方法對(duì)應(yīng)的響應(yīng)進(jìn)行緩存,GET和HEAD方法總是被緩存
3.8proxy_set_header field value;

#設(shè)定發(fā)往后端主機(jī)的請(qǐng)求報(bào)文的請(qǐng)求首部的值
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
請(qǐng)求報(bào)文的標(biāo)準(zhǔn)格式如下:
X-Forwarded-For: client1, proxy1, proxy2

3.9緩存配置:

proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
location /web {
            proxy_pass http://172.20.10.8:80;
            proxy_set_header clientip $remote_addr;
            proxy_cache proxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;
         }

4.添加頭部報(bào)文信息:
4.1nginx基于模塊ngx_http_headers_module可以實(shí)現(xiàn)對(duì)頭部報(bào)文添加指定的key與值#添加自定義首部,如下:

add_header name value [always];
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
add_trailer name value [always];
添加自定義響應(yīng)信息的尾部, 1.13.2版后支持

4.2nginx配置:

 location /web {
            proxy_pass http://172.20.10.8:80;
            proxy_set_header clientip $remote_addr;
            proxy_cache proxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;
            add_header X-Via $server_addr; 
            add_header X-Cache $upstream_cache_status;
            add_header X-Accel $server_name;
         }

4.3驗(yàn)證頭部信息:
nginx反向代理,虛擬主機(jī)
5.反向代理高級(jí)應(yīng)用:
Nginx可以基于ngx_http_upstream_module模塊提供服務(wù)器分組
轉(zhuǎn)發(fā)、權(quán)重分配、狀態(tài)監(jiān)測(cè)、調(diào)度算法等高級(jí)功能
5.1http_upstream_module配置參數(shù)

upstream name {
}
#自定義一組服務(wù)器,配置在http內(nèi)
server address [parameters];
#配置一個(gè)后端web服務(wù)器,配置在upstream內(nèi),至少要有一個(gè)server服務(wù)器配置。
#server支持的parameters如下:
weight=number #設(shè)置權(quán)重,默認(rèn)為1。
max_conns=number #給當(dāng)前server設(shè)置最大活動(dòng)鏈接數(shù),默認(rèn)為0表示沒(méi)有限制。
max_fails=number #對(duì)后端服務(wù)器連續(xù)監(jiān)測(cè)失敗多少次就標(biāo)記為不可用。
fail_timeout=time #對(duì)后端服務(wù)器的單次監(jiān)測(cè)超時(shí)時(shí)間,默認(rèn)為10秒。
backup #設(shè)置為備份服務(wù)器,當(dāng)所有服務(wù)器不可用時(shí)將重新啟用次服務(wù)器。
down #標(biāo)記為down狀態(tài)。
resolve #當(dāng)server定義的是主機(jī)名的時(shí)候,當(dāng)A記錄發(fā)生變化會(huì)自動(dòng)應(yīng)用新IP而不用重啟Nginx。
hash KEY consistent;
#基于指定key做hash計(jì)算,使用consistent參數(shù),將使用ketama一致性hash算法,適用于后端是Cache服務(wù)
器(如varnish)時(shí)使用,consistent定義使用一致性hash運(yùn)算,一致性hash基于取模運(yùn)算。
所謂取模運(yùn)算,就是計(jì)算兩個(gè)數(shù)相除之后的余數(shù),比如10%7=3, 7%4=3
hash $request_uri consistent; #基于用戶請(qǐng)求的uri做hash
ip_hash;
#源地址hash調(diào)度方法,基于的客戶端的remote_addr(源地址)做hash計(jì)算,以實(shí)現(xiàn)會(huì)話保持,
least_conn;
#最少連接調(diào)度算法,優(yōu)先將客戶端請(qǐng)求調(diào)度到當(dāng)前連接最少的后端服務(wù)器

5.2反向代理配置示例--多臺(tái)web服務(wù)器:

 upstream webserver {
               #  hash $request_uri consistent;  
                #   ip_hash;
                #  least_conn; 
        server 172.20.10.10:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.8:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.9:80 weight=1 fail_timeout=5s max_fails=3 backup;
    }
  location /web {
            index index.html;
            proxy_pass http://webserver/;
         }

5.3反向代理示例-客戶端IP透?jìng)?
(1)nginx配置文件:

upstream webserver {
        #  hash $request_uri consistent;  
        #   ip_hash;
        #  least_conn; 
     #   server 172.20.10.10:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.8:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.9:80 weight=1 fail_timeout=5s max_fails=3 backup;
    }   
location /web {
            index index.html;
            proxy_pass http://webserver/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }

(2)后端httpd服務(wù)器配置

[root@node6 conf]# vim httpd.conf
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

#重啟apache訪問(wèn)web界面并驗(yàn)證apache日志:

[root@node6 conf]# tail -2 /var/log/httpd/access_log 
- 172.20.10.2 - - [01/Dec/2019:15:45:21 +0800] "GET /web/ HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0"
- 172.20.10.2 - - [01/Dec/2019:15:45:21 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0"

網(wǎng)頁(yè)題目:nginx反向代理,虛擬主機(jī)
本文網(wǎng)址:http://bm7419.com/article14/jcijge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供營(yíng)銷型網(wǎng)站建設(shè)、Google微信公眾號(hào)、網(wǎng)站設(shè)計(jì)公司ChatGPT、云服務(wù)器

廣告

聲明:本網(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í)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司