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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章