一、Nginx反向代理的概述
- 反向代理:reverse proxy,指的是代理外网用户的请求到内部的指定的服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。
- Nginx 除了可以在企业提供高性能的web服务之外,另外还可以将 nginx 本身不具备的请求通过某种预定义的协议转发至其它服务器处理,不同的协议就是Nginx服务器与其他服务器进行通信的一种规范,主要在不同的场景使用以下模块实现不同的功能。
ngx_http_proxy_module: #将客户端的请求以http协议转发至指定服务器进行处理
ngx_http_upstream_module #用于定义为proxy_pass,fastcgi_pass,uwsgi_pass等指令引用的后
端服务器分组
ngx_stream_proxy_module:#将客户端的请求以tcp协议转发至指定服务器处理
ngx_http_fastcgi_module:#将客户端对php的请求以fastcgi协议转发至指定服务器助理
ngx_http_uwsgi_module: #将客户端对Python的请求以uwsgi协议转发至指定服务器处理
二、Nginx反向代理的配置参数
proxy_pass;
#用来设置将客户端请求转发给的后端服务器的主机,可以是主机名(将转发至后端服务做为主机头首部)、IP
地址:端口的方式
#也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持
#示例:
location /web {
index index.html;
proxy_pass http://10.0.0.18:8080; #8080后面无uri,即无 / 符号,需要将location后面url 附加到proxy_pass指定的url后面,此行为类似于root
#proxy_pass指定的uri不带斜线将访问的/web,等于访问后端服务器
http://10.0.0.18:8080/web/index.html,即后端服务器配置的站点根目录要有web目录才可以被访问
# http://nginx/web/index.html ==> http://10.0.0.18:8080/web/index.html
proxy_pass http://10.0.0.18:8080/; #8080后面有uri,即有 / 符号,相当于置换,即访问/web时实际返回proxy_pass后面uri内容.此行为类似于alias
#proxy_pass指定的uri带斜线,等于访问后端服务器的http://10.0.0.18:8080/index.html 内容返回给客户端
} # http://nginx/web/index.html ==> http://10.0.0.18:8080
三、编译安装Nginx
1.安装准备
2.安装依赖包
yum -y install gcc pcre-devel openssl-devel zlib-devel openssl openssl-devel
3.新建nginx用户便于管理
useradd -M -s /sbin/nologin nginx
4.切换到nginx-1.18.0,编译安装nginx
./configure --prefix=/apps/nginx \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module
5.修改文件权限
chown -R nginx.nginx /apps/nginx
conf:保存nginx所有的配置文件,其中nginx.conf是nginx服务器的最核心最主要的配置文件,其他的.conf则是用来配置nginx相关的功能的,例如fastcgi功能使用的是fastcgi.conf和fastcgi_params两个文件,配置文件一般都有个样板配置文件,是文件名.default结尾,使用的使用将其复制为并将default去掉即可。
html目录中保存了nginx服务器的web文件,但是可以更改为其他目录保存web文件,另外还有一个50x的web文件是默认的错误页面提示页面。
logs:用来保存nginx服务器的访问日志错误日志等日志,logs目录可以放在其他路径,比如/var/logs/nginx里面。
sbin:保存nginx二进制启动脚本,可以接受不同的参数以实现不同的功能。
6.将nginx的操作指令放入环境变量PATH的目录下
将nginx的可执行文件做个软链接,放入环境变量PATH的目录下,让系统识别nginx的操作指令。
ln -s /apps/nginx/sbin/nginx /usr/sbin/
7.检查、启动、重启、停止nginx服务
检查、启动nginx服务
多种方式查看nginx的PID号
停止nginx服务
重载nginx服务
8.创建Nginx自启动文件
#复制同一版本的nginx的yum安装生成的service文件
vim /usr/lib/systemd/system/nginx.service
#建立文件
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/apps/nginx/logs/nginx.pid
#注意文件位置,如果不对 启动不了
ExecStart=/apps/nginx/sbin/nginx -c /apps/nginx/conf/nginx.conf
#注意启动文件位置
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
LimitNOFILE=100000
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
#重新加载配置
systemctl start nginx
#启动nginx服务
四、Nginx的反向代理部署
单台web服务器反向代理的配置
1.服务端的配置
7-3 服务端
yum install httpd -y
#安装服务
cd /var/www/html
echo "Hello World" > index.html
#主页内容
systemctl start httpd
#开启服务
2.代理服务器的配置
7-2 代理服务器
编译安装nginx
vim /apps/nginx/conf/nginx.conf
在http模块中添加
include /apps/nginx/conf.d/*.conf
新建pc.conf文件
mkdir conf.d
vim /apps/nginx/conf.d/pc.conf
#添加服务端IP地址
server {
listen 80;
server_name www.pc.com;
root /data/html;
location / {
proxy_pass http://192.168.100.30;
}
}
nginx -t
nginx -s reload
3.客户端的配置
7-1 客户端
vim /etc/hosts
192.168.100.20 www.lhey.com
4.客户端测试
客户端测试
curl 192.168.100.20
五、Nginx反向代理实现动静分离部署
注意:因为nginx无法处理动态资源,所以要动静分离。 指定location实现反向代理和动静分离。
1.代理服务器的配置
7-2 代理服务器
编译安装nginx
vim /apps/nginx/conf/nginx.conf
在http模块中添加
include /apps/nginx/conf.d/*.conf
新建pc.conf文件
mkdir conf.d
vim /apps/nginx/conf.d/pc.conf
#添加动态资源跳转和静态资源跳转
server {
listen 80;
server_name www.pc.com;
root /data/html;
location /api {
proxy_pass http://192.168.100.30; #动态资源跳转
}
location /static {
proxy_pass http://192.168.100.40; #静态资源跳转
}
}
nginx -t
nginx -s reload
2.动态资源服务器的配置
7-3 动态资源服务器
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
#安装nginx服务
yum install epel-release -y #安装epel源
yum install nginx -y
#创建动态资源目录
cd /usr/share/nginx/html
mkdir api
echo api > ./api/index.html
#重启nginx
systemctl restart nginx
3.静态资源服务器的配置
7-4 静态资源服务器
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
#安装Nginx服务
yum install epel-release -y #安装epel源
yum install nginx -y
#创建动态资源目录
cd /usr/share/nginx/html
mkdir static
echo static > ./static/index.html
systemctl restart nginx
4.客户端测试
7-1 客户端
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
#动态资源
curl 192.168.100.20/api -L
#静态资源
curl 192.168.100.20/static -L
六、反向代理实现负载均衡概述
Nginx 可以基于ngx_http_upstream_module模块提供服务器分组转发、权重分配、状态监测、调度算法等高级功能。
要启用负载均衡,需要在Nginx主配置文件中添加一个upstream块来定义后端服务器的列表。
然后,在相应的location块中使用proxy_pass指令指定负载均衡的上游服务器。
官方文档
https://nginx.org/en/docs/http/ngx_http_up
1.负载均衡的工作原理
Nginx负载均衡通过将传入的请求分发到多个后端服务器来实现负载均衡。
它可以根据不同的调度算法(如轮询、IP哈希、最小连接数等)将请求分发到后端服务器。
2.负载均衡的部分配置参数
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1,实现类似于LVS中的WRR,WLC等
max_conns=number #给当前后端server设置最大活动链接数,默认为0表示没有限制
max_fails=number #后端服务器的下线条件,当客户端访问时,对本次调度选中的后端服务器连续进行检测多少次,如果都失败就标记为不可用,默认为1次,当客户端访问时,才会利用TCP触发对探测后端服务器健康性检查,而非周期性的探测
fail_timeout=time #后端服务器的上线条件,对已经检测到处于不可用的后端服务器,每隔此时间间隔再次进行检测是否恢复可用,如果发现可用,则将后端服务器参与调度,默认为10秒
sorry server 自己不能转自己
down #标记为down状态
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx
backup #设置为备份服务器,当所有后端服务器不可用时,才会启用此备用服务器
upstream backend {
server backend1.example.com;
server backend2.example.com backup;
server backend3.example.com;
}
3.调度算法
1)轮询(Round Robin):这是默认的调度算法,Nginx依次分配每个请求给后端服务器,实现简单且公平的请求分发。
http {
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
}
2)加权轮询(Weighted Round Robin):通过为每个后端服务器指定一个权重,根据权重比例来分配请求。具有更高权重的服务器将获得更多的请求。
http {
upstream backend {
server backend1.example.com weight=3;
server backend2.example.com weight=1;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
}
3)IP Hash(IP Hash):根据客户端的IP地址进行散列,将同一IP的请求分配给同一台后端服务器,实现会话保持。
http {
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
}
4)URL哈希(URL Hash):基于请求的URL进行散列计算,将相同URL的请求分发到同一台后端服务器,用于缓存或会话的需要。
http {
upstream backend {
hash $request_uri;
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
}
5)参数哈希(Parameter Hash):基于请求的特定参数进行散列计算,将具有相同参数值的请求分发到同一台后端服务器。
6)最小连接数(Least Connections):将请求分发给连接数最少的服务器,以确保服务器的负载尽可能均衡。
http {
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
server {
listen 80;
location / {
proxy_pass http://backend;
}
}
}
7)最少响应时间(Least Time):根据服务器的响应时间进行选择,将请求分配给响应时间最短的服务器。
七、负载均衡部署
使用轮询算法
1.代理服务器的配置
7-2 代理服务器
编译安装nginx
vim /apps/nginx/conf/nginx.conf
在http模块中添加
include /apps/nginx/conf.d/*.conf
新建pc.conf文件
mkdir conf.d
vim /apps/nginx/conf.d/pc.conf
#在子配置文件中添加,除此也可以在主配置文件的http模块中添加
#在location模块中定义web组
upstream web {
server 192.168.100.30;
server 192.168.100.40;
}
server {
location / {
proxy_pass http://web;
}
}
nginx -t
nginx -s reload
2.后端服务器1的配置
7-3 后端服务器1
yum install httpd -y
#安装httpd服务
cd /var/www/html
echo "7-3" > index.html
#主页内容
systemctl restart httpd
#开启服务
3.后端服务器2的配置
7-4 后端服务器2
yum install httpd -y
#安装httpd服务
cd /var/www/html
echo "7-4" > index.html
#主页内容
systemctl restart httpd
#开启服务
4.客户端测试
7-1 客户端
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
客户端测试
curl 192.168.100.20
使用加权轮询算法
1.代理服务器的配置
7-2 代理服务器
编译安装nginx
vim /apps/nginx/conf/nginx.conf
在http模块中添加
include /apps/nginx/conf.d/*.conf
新建pc.conf文件
mkdir conf.d
vim /apps/nginx/conf.d/pc.conf
#在子配置文件中添加,除此也可以在主配置文件的http模块中添加
#在location模块中定义web组
upstream web {
server 192.168.100.30 weight=3;
server 192.168.100.40 weight=1;
}
server {
location / {
proxy_pass http://web;
}
}
nginx -t
nginx -s reload
2.后端服务器1的配置
7-3 后端服务器1
yum install httpd -y
#安装httpd服务
cd /var/www/html
echo "7-3" > index.html
#主页内容
systemctl restart httpd
#开启服务
3.后端服务器2的配置
7-4 后端服务器2
yum install httpd -y
#安装httpd服务
cd /var/www/html
echo "7-4" > index.html
#主页内容
systemctl restart httpd
#开启服务
4.客户端测试
7-1 客户端
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
客户端测试
curl 192.168.100.20
备份-----使用backup参数
当所有后端服务器不可用时,才会启用此备用服务器。
1.代理服务器的配置
7-2 代理服务器
编译安装nginx
vim /apps/nginx/conf/nginx.conf
在http模块中添加
include /apps/nginx/conf.d/*.conf
新建pc.conf文件
mkdir conf.d
vim /apps/nginx/conf.d/pc.conf
#在子配置文件中添加,除此也可以在主配置文件的http模块中添加
#在location模块中定义web组
upstream web {
server 192.168.100.30;
server 192.168.100.40 backup;
}
server {
location / {
proxy_pass http://web;
}
}
nginx -t
nginx -s reload
2.后端服务器1的配置
7-3 后端服务器1
yum install httpd -y
#安装httpd服务
cd /var/www/html
echo "7-3" > index.html
#主页内容
systemctl restart httpd
#开启服务
3.后端服务器2的配置
7-4 后端服务器2
yum install httpd -y
#安装httpd服务
cd /var/www/html
echo "7-4" > index.html
#主页内容
systemctl restart httpd
#开启服务
4.客户端测试
7-1 客户端
#关闭防火墙和selinux
systemctl stop firewalld
setenforce 0
客户端测试
curl 192.168.100.20
测试显示后端服务器1一直运行中。
关闭后端服务器1,测试显示后端服务器2作为备用服务器开始运行。