1. 程式人生 > 實用技巧 >Nginx代理相關模組解析

Nginx代理相關模組解析

一 ngx_http_proxy_module模組

1.1 proxy_pass配置

proxy_pass URL; Context: location, if in location, limit_except 注意:proxy_pass後面的路徑不帶uri時,其會將location的uri傳遞給後端主機。
  1 server {
  2     …
  3     server_name HOSTNAME;
  4     location /uri/ {
  5        proxy http://hos[:port];
  6     }
  7        …
  8 }
http://HOSTNAME/uri –> http://host/uri
proxy_pass後面的路徑是一個uri時,其會將location的uri替換為proxy_pass的uri
  1 server {
  2     …
  3     server_name HOSTNAME;
  4 
  5     location /uri/ {
  6         proxy http://host/new_uri/;
  7     }
  8         …
  9  }
http://HOSTNAME/uri/ –> http://host/new_uri/ 如果location定義其uri時使用了正則表示式的模式,則proxy_pass之後必須不能使用uri; 使用者請求時傳遞的uri將直接附加代理到的服務的之後
  1 server {
  2     …
  3     server_name HOSTNAME;
  4         location ~|~* /uri/ {
  5         proxy http://host;
  6     }
  7         …
  8 }

1.2 proxy_set_header配置

proxy_set_header field value; #設定發往後端主機的請求報文的請求首部的值; Context: http, server, location proxy_set_header X-Real-IP $remote_addr; $remote_addr:記錄的是上一臺主機的IP,而上一臺主機有可能也是代理伺服器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; $proxy_add_x_forwarded_for:記錄的是源IP地址 在http客戶端還有修改/etc/httpd/conf/httpd.conf檔案 LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 通過上述方法則可以在後端主機上記錄真實的httpd資源請求者,而不再是隻記錄前端代理伺服器的IP地址

1.3 proxy_cache_path

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]; proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:1 keys_zone=gmtest:20M max_size=1G;

1.4 proxy_cache

proxy_cache zone | off; #指明要呼叫的快取,或關閉快取機制; Context: http, server, location proxy_cache gmtest;

1.5 proxy_cache_key

proxy_cache_key string; #快取中用於“鍵”的內容; 預設值:proxy_cache_key $scheme$proxy_host$request_uri; 建議定義成方法和url

1.6 proxy_cache_valid

proxy_cache_valid [code …] time; #定義對特定響應碼的響應內容的快取時長; 定義在http{…}中; proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=gmtest:20m max_size=1g; 定義在需要呼叫快取功能的配置段,例如server{…},或者location中; proxy_cache gmtest; proxy_cache_key $request_uri; proxy_cache_valid 200 302 301 1h; proxy_cache_valid any 1m;

1.7 proxy_cache_use_stale

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. 後端伺服器的故障在那種情況下,就使用快取的功能對客戶的進行返回

1.8 proxy_cache_methods

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. 預設方法就是GET HEAD方法

1.9 proxy_hide_header

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.

1.10 proxy_connect_timeout

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

1.11 buffer

buffer相關的配置 a:proxy_buffer_size size; Sets the size of the buffer used for reading the first part of the response received from the proxied server. This part usually contains a small response header. By default, the buffer size is equal to one memory page. 預設為4k|8k b:proxy_buffering on | off; Enables or disables buffering of responses from the proxied server. 預設為on c:proxy_buffers number size; Sets the number and size of the buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page. 預設為8 4k|8k d:proxy_busy_buffers_size size; When buffering of responses from the proxied server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read. 預設為8k|16k

二 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. 向由代理伺服器響應給客戶端的響應報文新增自定義首部,或修改指定首部的值;

2.1 add_header

add_header name value [always]; #新增自定義首部; add_header X-Via $server_addr; #經由的代理伺服器地址 add_header X-Accel $server_name;

2.2 expires

expires [modified] time; expires epoch | max | off; 用於定義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.

3.1 upstream

  1 upstream name { … }			#定義後端伺服器組,會引入一個新的上下文;
  2 Context: http
  3 upstream httpdsrvs {
  4             server …
  5             server…
  6             …
  7 }

3.2 server address

server address [parameters]; 在upstream上下文中server成員,以及相關的引數;Context: upstream address的表示格式: unix:/PATH/TO/SOME_SOCK_FILE IP[:PORT] HOSTNAME[:PORT] parameters: weight=number 權重,預設為1;預設演算法是wrr max_fails=number 失敗嘗試最大次數;超出此處指定的次數時,server將被標記為不可用 fail_timeout=time 設定將伺服器標記為不可用狀態的超時時長 max_conns 當前的伺服器的最大併發連線數 backup 將伺服器標記為“備用”,即所有伺服器均不可用時此伺服器才啟用 down 標記為“不可用” 先在nginx前端配置down,然後在下架後端伺服器,上架新的web程式,然後上架,在修改配置檔案立馬的down

3.3 least_conn

least_conn; 最少連線排程演算法,當server擁有不同的權重時其為wlc 要在後端伺服器是長連線時,效果才好,比如mysql

3.4 ip_hash

ip_hash; 源地址hash排程方法

3.5 hash

hash key [consistent]; 基於指定的key的hash表來實現對請求的排程,此處的key可以直接文字、變數或二者的組合 作用:將請求分類,同一類請求將發往同一個upstream server If the consistent parameter is specified the ketama consistent hashing method will be used instead. 示例: hash $request_uri consistent; hash $remote_addr; hash $cookie_name; 對同一瀏覽器的請求,發往同一個upstream server

3.6 keepalive

keepalive connections; 為每個worker程序保留的空閒的長連線數量

四 ngx_stream_core_module模組

模擬反代基於tcp或udp的服務連線,即工作於傳輸層的反代或排程器

4.1 stream

stream { … } 定義stream相關的服務;Context: main
  1 stream {
  2 upstream sshsrvs {
  3   server 192.168.22.2:22;
  4   server 192.168.22.3:22;
  5   least_conn;
  6 }
  7 server {
  8    listen 10.1.0.6:22022;
  9    proxy_pass sshsrvs;
 10   }
 11 }
stream模組中管的upstream模組的用法同上

4.2 listen

listen listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport] [so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]]; 提示:本文摘錄自:https://cloud.tencent.com/developer/article/1027563