nginx 配置二


1 代理服务让后端的上流服务器处理
在前端nginx 调度器配置中,修改/etc/nginx/nginx.conf:
添加 proxy_pass http://ip; 即可
在这里插入图片描述

2
当访问 http://192.168.76.142 时,为nginx本地web服务,
访问 http://192.168.76.131/admin 时,则代理到后端的服务器
在这里插入图片描述

二 ngx_http_headers_module模块
1 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
在这里插入图片描述

1 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];

2 proxy_cache zone | off;
指明要调用的缓存,或关闭缓存机制;
Context: http, server, location
3 proxy_cache_key string;
#指明缓存中用于“键”的指定内容;
4 默认值:proxy_cache_key $scheme $proxy_host $request_uri;
#若希望公用缓存,则只是用$request_uri
5 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;
在这里插入图片描述

6 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.
7 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;
四 ngx_http_headers_module模块
模块功能:向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;
1、add_header name value [always];
添加自定义首部
eg:
add_header X-Via $server_addr; $ 添加代理服务器地址
在这里插入图片描述

五ngx_http_upstream_module模块
模块功能: 用于实现后端服务器的负载均衡, 使用该模块来定义后端服务器组
定义以后,需要在使用的地方进行调用,即可实现负载均衡;
1、upstream name { … }
# 定义后端服务器组,会引入一个新的上下文;Context: http
eg:
upstream httpd_luo{
server 192.168.76.131;
server 192.168.76.143;
}
#注意: server 后面只需要添加地址即可
在这里插入图片描述

2、server address [parameters];
在upstream上下文中server成员,以及相关的参数;
Context: upstream
parameters:
weight=number
权重,默认为1;
max_fails=number
失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用;
fail_timeout=time
设置将服务器标记为不可用状态的超时时长;
max_conns
当前的服务器的允许的最大并发连接数;
backup
将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用;
down
标记为“不可用”;
3 least_conn;
Context: upstream
最少连接调度算法,当server拥有不同的权重时其为wlc;
4 ip_hash;
Context: upstream
源地址hash调度方法;
使用示例:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值