Nginx反向代理
nginx代理服务器配置
upstream模块的负载均衡算法主要有三种,轮调(round-robin)、ip哈希(ip_hash)和最少连接(least_conn)三种。weight等于0或不写表示不参与负载均衡,upstream模块定义到server模块外
upstream websrv {
ip_hash
server 172.29.8.9 weight=1;
server 172.29.8.12 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://websrv/;
proxy_set_header X-Real-IP $remote_addr; 后端服务器看到来访的client IP日志
}
}
后端web服务器配置
1)web服务器为:apache调整后端服务的日志显示,可以查看统计client端口用户访问的真实IP,说明:Apache获取真实IP地址有2个模块:
mod_rpaf:Apache-2.2支持;Apache-2.4不支持;
mod_remoteip:Apache-2.4自带模块;Apache-2.2 支持
vim /etc/httpd/conf/httpd.conf
RemoteIPHeader X-Forwarded-For
RemoteIPInternalProxy 192.168.243.129
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
2)web服务器为: nginx
nginx编译安装要编译安装 --with-http_realip_module模块
/configure --with-http_realip_module && make
cp objs/nginx /usr/local/nginx/sbin/ && nginx -s reload
vim /usr/local/nginx/conf/nginx.conf
http { include mime.types; default_type application/octet-stream; set_real_ip_from 192.168.243.129; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';