- 前言
- 一、nginx
- 1.1 四层反向代理
- 1.1.1 源码编译nginx(调度器)
- 1.1.2 配置stream模块(调度器)
- 1.1.3 配置节点服务器
- 1.2 七层反向代理
- 1.2.1 源码编译nginx(调度器)
- 1.2.2 配置http模块(调度器)
- 1.2.3 配置节点服务器
- 二、结语
前言
本博客内容仅为记录博主思路,仅供参考,一切以自己实践结果为准。
一、nginx 1.1 四层反向代理
- 四层反向代理必须安装stream模块,若之前未配置,则需要重新配置开启
- 源码安装nginx
vim /usr/local/nginx/conf/nginx.conf #修改nginx主配置文件
stream {
server {
listen 8080; #监听端口为8080端口
proxy_pass httpd_server; #定义负载均衡服务名称为httpd
} #对应server块的大括号
upstream httpd_server { #配置负载均衡配置
server 192.168.13.20:8080 weight=1; #设置节点服务器IP/端口/权重等配置
server 192.168.13.30:8080 weight=1;
} #对应upstream块的大括号
log_format main '$remote_addr $upstream_addr-[$time_local] $status $upstream_bytes_sent';
#开启日志(内容自定义)
} #对应stream块的大括号
nginx -t #检查语法错误
systemctl restart nginx #重启nginx服务
1.1.3 配置节点服务器
setenforce 0 systemctl stop firewalld yum install httpd -y systemctl start httpd echo 'this is web1' >/var/www/html/index.html #在另外一台节点服务器重复此操作
1.2 七层反向代理 1.2.1 源码编译nginx(调度器)
源码安装nginx
1.2.2 配置http模块(调度器)vim /usr/local/nginx/conf/nginx.conf #修改nginx主配置文件
http{
upstream httpd{ #定义httpd负载调度服务(可自定义命名)
server 192.168.13.20:80 weight=1; #设置节点服务器IP/端口/权重等配置
server 192.168.13.30:80 weight=1;
}
keepalive_timeout 3; #缩小长链接时间,方便验证结果
server {
location / { #设置匹配规则(精确/正则/通用)
proxy_pass http://httpd/; #当触发该匹配规则时,调用自定义的httpd服务
} #对应location快大括号
} #对应server块大括号
} #对应http块大括号
nginx -t #检查语法错误
systemctl restart nginx #重启nginx服务
1.2.3 配置节点服务器
setenforce 0 systemctl stop firewalld yum install httpd -y systemctl start httpd echo 'this is web1' >/var/www/html/index.html #在另外一台节点服务器重复此操作
二、结语
- 易错点:大括号对应错误,这种错误通过nginx -t 可逐渐排查出来,为了防止此类错误,要养成良好写代码习惯,复制粘贴的时候也要注意大括号是否对应整齐
- 注意点:想使用四层反向代理,nginx必须安装stream模块



