nginx反向代理簡介

簡介

服務器根據客戶端的請求,從其關聯的一組或多組後端服務器(如Web服務器)上獲取資源,然後再將這些資源返回給客戶端,客戶端只會得知反向代理的IP地址,而不知道在代理服務器後面的服務器的存在。

模塊

ngx_http_proxy_module

示例

location / {
    proxy_pass       http://localhost:8000;
    proxy_set_header Host      $host;
    proxy_set_header X-Real-IP $remote_addr;
}

1.設置緩衝區的大小爲size。

Syntax: proxy_buffer_size size;
Default:    
proxy_buffer_size 4k|8k;
Context:    http, server, location

2.設置響應被緩存的最小請求

Syntax: 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];
Default:    —
Context:    http
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;

3.當開啓緩衝響應的功能以後,在沒有讀到全部響應的情況下,寫緩衝到達一定大小時,nginx一定會向客戶端發送響應。

Syntax: proxy_cache zone | off;
Default:    
proxy_cache off;
Context:    http, server, location

4.定義對特定響應碼的響應內容的緩存時長

Syntax: proxy_cache_valid [code ...] time;
Default:    —
Context:    http, server, location
Sets caching time for different response codes. For example, the following directives
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404      1m;

5.設置與後端服務器建立連接的超時時間。應該注意這個超時一般不可能大於75秒。

Syntax: proxy_connect_timeout time;
Default:    
proxy_connect_timeout 60s;
Context:    http, server, location

6.定義從後端服務器讀取響應的超時

Syntax: proxy_read_timeout time;
Default:    
proxy_read_timeout 60s;
Context:    http, server, location

7.定義向後端服務器傳輸請求的超時

Syntax: proxy_send_timeout time;
Default:    
proxy_send_timeout 60s;
Context:    http, server, location

8.爲每個連接設置緩衝區的數量

Syntax: proxy_buffers number size;
Default:    
proxy_buffers 8 4k|8k;
Context:    http, server, location

9.當開啓緩衝響應的功能以後,在沒有讀到全部響應的情況下,寫緩衝到達一定大小時,nginx一定會向客戶端發送響應,直到緩衝小於此值

Syntax: proxy_busy_buffers_size size;
Default:    
proxy_busy_buffers_size 8k|16k;
Context:    http, server, location

10.在開啓緩衝後端服務器響應到臨時文件的功能後,設置nginx每次寫數據到臨時文件的大小

Syntax: proxy_temp_file_write_size size;
Default:    
proxy_temp_file_write_size 8k|16k;
Context:    http, server, location

11如果後端服務器出現狀況,nginx是可以使用過期的響應緩存

Syntax: proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429 | non_idempotent | off ...;
Default:    
proxy_next_upstream error timeout;
Context:    http, server, location
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章