Nginx实现反向代理
openresty-nginx增强版(现在使用的)
localhost 反向代理到 s102--s104
1.windows下
安装openresty,并修改nginx.conf配置文件
![](https://img-blog.csdnimg.cn/2022010616113919701.png)
nginx.conf配置文件
#使用指定用户
#user root;
#使用的处理器进程数
worker_processes 4;
#错误日志文件
#error_log logs/error.log;
#日志文件与日志级别
#error_log logs/error.log notice;
#error_log logs/error.log info;
#存放进程pid
#pid /tmp/nginx.pid;
events {
#每个worker最大连接数
worker_connections 1024;
}
http {
include mime.types;
#default_type application/json;
#default_type application/octet-stream;
#文件类型:纯文本
default_type text/plain;
#重点:日志格式
log_format main escape=json $msec#$clientRealIp#$http_client_time#$status#$request_body;
#log_format main escape=json $clientRealIp#$http_client_time#$time_local#$status#$request_body;
#自定义变量,取得ip串中的第一个ip,也就是用户ip
map $http_x_forwarded_for $clientRealIp {
~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr;
}
#反向代理服务器不需要存储日志
access_log off;
sendfile on;
#tcp_nopush on;
underscores_in_headers on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#反向代理服务器:静态服务器
upstream nginx_servers{
server s102:8089 max_fails=2 fail_timeout=2 weight=4;
server s103:8089 max_fails=2 fail_timeout=2 weight=4;
server s104:8089 max_fails=2 fail_timeout=2 weight=4;
}
#server域配置
server {
listen 8089;
server_name localhost;
charset utf-8;
access_log off;
location ~* \.(png|html|js|css)$ {
proxy_pass http://nginx_servers;
}
location / {
proxy_pass http://nginx_servers;
}
proxy_set_header Host $host;
proxy_set_header remove_user_ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2. linux下
.下载安装linux下的openresty 在s102-s104安装openresty
你可以在你的 CentOS 系统中添加 openresty 仓库,这样就可以便于未来安装或更新我们的软件包(通过 yum update 命令)。运行下面的命令就可以添加我们的仓库:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo
https://openresty.org/package/centos/openresty.repo
然后就可以像下面这样安装软件包,比如 openresty:
sudo yum install -y openresty
nginx.conf配置文件
s102-s104
user root; worker_processes 4; #pid /tmp/nginx.pid; events { worker_connections 1024; } http { include mime.types; #default_type application/json; #default_type application/octet-stream; default_type text/plain; log_format main escape=json $msec#$clientRealIp#$http_client_time#$status#$request_body; #log_format main escape=json $clientRealIp#$http_client_time#$time_local#$status#$request_body; map $http_x_forwarded_for $clientRealIp { ~^(?P<firstAddr>[0-9\.]+),?.*$ $firstAddr; } access_log off; sendfile on; #tcp_nopush on; underscores_in_headers on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8089; server_name s102; charset utf-8; access_log /usr/local/openresty/nginx/logs/access.log main; location / { root html; index index.html index.htm; error_page 405 =200 $1; lua_need_request_body on; content_by_lua 'local s = ngx.var.request_body'; } } }
3、安装完之后,文件位置在/usr/local/openresty
4、启动openresty
windows启动:nginx
windows启动:nginx
Linux启动:openresty
5.查看日志
![](https://img-blog.csdnimg.cn/2022010616114096544.png)
6.清空日志中数据
[root@s101 /home/centos]# xcall.sh "echo -n > /usr/local/openresty/nginx/logs/access.log