Nginx中ngx_http_proxy_module模块

该模块允许将请求传递给另⼀一台服务器器
指令:
1 ,proxy_pass
设置代理理服务器器的协议和地址以及应映射位置的可
选 URI 。作为协议,可以指定“ http 或 https 。可
以将地址指定为域名或IP地址,以及可选端⼝口
Syntax: proxy_pass URL;
Default: —
Context: location, if in location,
limit_except
如果 proxy_pass 后⾯面指定了了 uri ,则其会
将 location 指定的 uri 给替换掉
location /bbs/ {
proxy_pass http://192.168.0.1/fo
rum/
}
客户端访问:http://www.a.com/bbs/
代理理服务器器:http://192.168.0.1/forum/
如果 proxy_pass 后⾯面没有指定 uri ,则会
将 location 指定的 uri 附加 proxy_pass 后⾯面
location /bbs/ {
proxy_pass http://192.168.0.1 #
如果是附加,地址最后⾯面不不要写"/"符号
}
客户端访问:http://www.a.com/bbs/
代理理服务器器:http://192.168.0.1/bbs/
动静分离
server {
listen 80 ;
server_name www.a.com ;
root /data/web1/ ;
location / {
proxy_pass http://192.168.0.
1 ;
}
location ~*.php$ {
proxy_pass http://192.168.0.
1 ;
}
}
2, proxy_set_header
设定发往后端主机的请求报⽂文的请求⾸首部的值
Syntax: proxy_set_header field
value;
Default:
proxy_set_header Host $proxy_host;
proxy_set_header Connection close;
Context: http, server, location
设置后端服务器器得到是真实的客服端IP,如果要在
后端服务器器⽇日志中看到客户端IP,还需要调整后端
服务器器的⽇日志格式
location / {
proxy_pass http://192.168.0.1 ;
proxy_set_header X-Real-IP $remo
te_addr ; # "X-Real-IP"是⼀一个⾃自定义名

}
3, proxy_cache_path
设置缓存的路路径和其他参数。缓存数据存储在⽂文件
中,缓存中的⽂文件名是将MD5功能应⽤用于缓存键的
结果。该 levels 参数定义⾼高速缓存的层次结构级
别:从1到3,每个级别接受值1或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 l
evels = 1:2 keys_zone = one:10m;
缓存中的⽂文件名如下所示:
/data/nginx/cache/c/29/b7f54b2df7773
722d382f4809d650 29c
4, proxy_cache
指明调⽤用的缓存,或关闭缓存机制
Syntax: proxy_cache zone | off;
Default: proxy_cache off;
Context: http, server, location
5, proxy_cache_key
定义缓存的键,也就对谁做MD5哈希来得到缓存⽂文
件的名
Syntax: proxy_cache_key string;
Default: proxy_cache_key
$scheme$proxy_host$request_uri;
Context: http, server, location
6, proxy_cache_valid
对特定响应码的响应内容的缓存时⻓长定义
在 http{...} 中
Syntax: proxy_cache_valid [code ...]
time;
Default: —
Context: http, server, location
7, proxy_cache_use_stale
在被代理理的后端服务器器出现哪种情况下,可以真接
使⽤用过期的缓存响应客户端
Syntax: proxy_cache_use_stale error |
timeout | invalid_header | updating |
http_500 | http_502 | http_503 |
http_504 | http_403 | http_404 |
http_429 | off ...;
Default: proxy_cache_use_stale off;
Context: http, server, location
8, proxy_cache_methods
对哪些客户端请求⽅方法对应的响应进⾏行行缓存, GET
和 HEAD ⽅方法总是被缓存
Syntax: proxy_cache_methods GET |
HEAD | POST ...;
Default: proxy_cache_methods GET
HEAD;
Context: http, server, location
9, proxy_hide_header
默认nginx响应报⽂文时,不不传递后端服务器器的⾸首部字
段 Date , Server , X-Pad , X-Accel-等 ,⽤用于
隐藏后端服务器器特定的响应⾸首部
Syntax: proxy_hide_header field;
Default: —
Context: http, server, location
10, proxy_connect_timeout
定义与后端服务器器建⽴立连接的超时时⻓长,如超时会
出现 502 错误,默认为60s,⼀一般不不建议超出75s
Syntax: proxy_connect_timeout time;
Default: proxy_connect_timeout 60s;
Context: http, server, location
11, proxy_send_timeout
将请求发送给后端服务器器的超时时⻓长;默认为60s
Syntax: proxy_send_timeout time;
Default: proxy_send_timeout 60s;
Context: http, server, location
12, proxy_read_timeout
等待后端服务器器发送响应报⽂文的超时时⻓长, 默认为
60s
Syntax: proxy_read_timeout time;
Default: proxy_read_timeout 60s;
Context: http, server, location
定义缓存:
http{
proxy_cache_path /var/cache/ngin
x/proxy_cache levels=1:1:1 keys_zone
=proxycache:20m inactive=120s max_si
ze=1g;
}
调⽤用缓存功能:
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;

原文地址:https://www.cnblogs.com/momenglin/p/11107953.html