一. ngx_http_proxy_module模塊:
模塊功能: 為后端httpd服務做反向代理, 并且與Httpd 之間使用http進行通信
1、proxy_pass URL;
Context: location, if in location, limit_except
當root 與proxy_pass 同時存在是,proxy 優先級更高
——————————————————————————————
A:注意:proxy_pass后面的路徑不帶uri時,其會將location的uri傳遞(添加到結尾) \
給后端主機;
server {
…
server_name HOSTNAME;
location /uri/ {
proxy_pass http://hos[:port]; #即此處結尾無"/"
}
…
}
訪問時候: http://HOSTNAME/uri –> http://host/uri #補充
B: proxy_pass后面的路徑是一個uri時,其會將location的uri替換為proxy_pass的uri,
即new_uri;
server {
…
server_name HOSTNAME;
location /uri/ {
proxy_pass http://host/new_uri/;
}
…
}
http://HOSTNAME/uri/ –> http://host/new_uri/ #替換
C: 如果location定義其uri時使用了正則表達式的模式,則proxy_pass之后必須不能使用
uri; 用戶請求時傳遞的uri將直接附加代理到的服務的之后;
server {
…
server_name HOSTNAME;
location ~|~* /uri/ {
proxy_pass http://host;
}
…
}
http://HOSTNAME/uri/ –> http://host/uri/;
————————————————————————————
=========================================================
使用示例:(備注:此處僅為單臺httpd 服務器代理)
1. 在前端nginx 調度器配置中,修改/etc/nginx/nginx.conf:
添加 proxy_pass http://ip; 即可
示例2.
當訪問 http://10.1.249.143 時,為nginx本地web服務,
訪問 http:// 10.1.249.143/admin 時,則代理到后端的服務器
=======================================================
2、proxy_set_header field value;
作用: 設定發往后端主機的請求報文的請求首部的值;
允許使用自定義的 首部信息;
Context: http, server, location
補充:
前端的nginx代理,可以捕獲客戶端發送來的請求報文首部,并
保存為$proxy_add_x_forwarded_for, 此值可以傳遞給后續的代理服務器
eg:
proxy_set_header X-Real-IP $remote_addr;
#將請求的客戶端遠程地址傳送給后端服務器
此時需要修改一下后端httpd 服務器的日志格式,以便可以直觀的看到效果:
修改/etc/httpd/conf/httpd.conf
LogFormat 中的combined項,在前面的%h 修改為%{X-Real-IP}i
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
================================================================
3、proxy_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];
4、proxy_cache zone | off;
指明要調用的緩存,或關閉緩存機制;
Context: http, server, location
5、 proxy_cache_key string;
#指明緩存中用于“鍵”的指定內容;
默認值:proxy_cache_key $scheme$proxy_host$request_uri;
#若希望公用緩存,則只是用$request_uri
6、proxy_cache_valid [code …] time;
#定義對特定響應碼的響應內容的緩存時長;
#若想全局生效,可以在server中定義,若希望局部uri生效,則在location中定義
定義在http{…}中;
eg:
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=pxycache:20m max_size=1g;
#注意,緩存的目錄需要事先創建
定義在需要調用緩存功能的配置段,例如server{…};
proxy_cache pxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
7、proxy_cache_use_stale
#指定緩存服務器與后端服務器無法通信時,
何種情況下依舊使用過期的緩存內容來響應客戶端
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |
http_502 | http_503 | http_504 | http_403 | http_404 | off …;
Determines in which cases a stale cached response can be used when an error
occurs during communication with the proxied server.
8、proxy_cache_methods GET | HEAD | POST …;
#定義允許使用緩存的請求方法
If the client request method is listed in this directive then the response will be cached. “GET” and “HEAD” methods are always added to the list, though it is recommended to specify them explicitly.
9、proxy_hide_header field;
#定義需要隱藏的響應報文首部
By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-…” from the response of a proxied server to a client. The proxy_hide_header directive sets additional fields that will not be passed.
一般nginx反向代理會配置很多站點,每個站點配置費時費力而且少有遺漏,主機信息還是會被泄露的。根據上面的說明,我們將
proxy_hide_header 配置在http區段
注意: 部分header 信息無法用此方法,如關閉server信息,需要用此方式:
Syntax:server_tokens on | off | string;
Default:server_tokens on;
Context:http, server, location;
10、proxy_connect_timeout time;
#代理服務器與后端服務器連接的超時時長
Defines a timeout for establishing a connection with a proxied server. It should be
noted that this timeout cannot usually exceed 75 seconds.
默認為60s;,最大不超過75s
———————————————————————————————二 . buffer相關的配置;
二. ngx_http_headers_module模塊
The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields, to a response header.
模塊功能:向由代理服務器響應給客戶端的響應報文添加自定義首部,或修改指定首部的值;
1、add_header name value [always];
添加自定義首部;
eg:
add_header X-Via $server_addr; $ 添加代理服務器地址
效果示例: 請求頁面后, 在瀏覽器調試控制臺中可看到代理服務器信息
add_header X-Accel $server_name;
2、expires [modified] time;
expires epoch | max | off;
給出的日期/時間后,被響應認為是過時。如Expires:Thu, 02 Apr 2009 05:14:08
GMT需和Last-Modified結合使用。用于控制請求文件的有效時間,當請求數據在有效期
內時客戶端瀏覽器從緩存請求數據而不是服務器端
.當緩存中數據失效或過期,才決定從服務器更新數據。
用于定義Expire或Cache-Control首部的值;
=========================================================
三. ngx_http_upstream_module模塊
The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass directives.
模塊功能: 用于實現后端服務器的負載均衡, 使用該模塊來定義后端服務器組
定義以后,需要在使用的地方進行調用,即可實現負載均衡;
1、upstream name { … }
# 定義后端服務器組,會引入一個新的上下文;Context: http
eg:
upstream httpd_srvs {
server 192.168.0.1
#注意: server 后面只需要添加地址即可!
server 192.168.0.2
…
}
2、server address [parameters];
在upstream上下文中server成員,以及相關的參數;
Context: upstream
address的表示格式:
unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]
parameters:
weight=number
權重,默認為1;
max_fails=number
失敗嘗試最大次數;超出此處指定的次數時,server將被標記為不可用;
fail_timeout=time
設置將服務器標記為不可用狀態的超時時長;
max_conns
當前的服務器的允許的最大并發連接數;
backup
將服務器標記為“備用”,即所有服務器均不可用時此服務器才啟用;
down
標記為“不可用”;
eg:
upstream httpd_srvs {
server 192.168.0.1 down ;
#定義服務器為下線狀態;
server 192.168.0.2 backup;
#定義為備用服務器
server 192.168.0.3 weight 2 max_conns 100;
#權重為2,最發并發為100
}
3、least_conn;
Context: upstream
最少連接調度算法,當server擁有不同的權重時其為wlc;
4、 ip_hash;
Context: upstream
源地址hash調度方法;
使用示例:
5、hash key [consistent];
Context: upstream
If the consistent parameter is specified the ketama consistent hashing
method will be used instead.
#[consistent]; 使用一致性哈希算法, 建議開啟此項
基于指定的key的hash表來實現對請求的調度,此處的key可以直接文本、
變量或二者的組合;
作用:將請求分類,同一類請求將發往同一個upstream server;
示例:
hash $request_uri consistent;
hash $remote_addr;
6、keepalive connections;
補充: 由于短連接消耗前端代理服務器的資源現象嚴重,因此會將一部分連接定義為
長連接以節省資源
#為每個worker進程保留的空閑的長連接數量;
#定義nginx與后端服務器的保持連接的數量
============================================================
四 . ngx_stream_core_module模塊
模擬反代基于tcp或udp的服務連接,即工作于傳輸層的反代或調度器;
#此模塊可以定義非http服務的反代功能
1、stream { … }
定義stream相關的服務;
Context:main
#用法與upstream 類似
eg: #反代ssh 服務
stream {
upstream sshsrvs {
server 192.168.22.2:22;
server 192.168.22.3:22;
least_conn;
}
#定義服務器組
server {
listen 10.1.0.6:22022;
proxy_pass sshsrvs;
#調用sshsrvs服務器組
}
# 前端監聽22022端口,并反代到后端服務器組的22端口
}
2、listen
listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
思考:
(1) 動態資源存儲一組服務器、圖片資源存在一組服務器、靜態的文本類資源存儲在一組服
務器;如何分別調度?
(2) 動態資源基于fastcgi或http協議(ap)?
lnamp
原創文章,作者:ldt195175108,如若轉載,請注明出處:http://www.www58058.com/55594