nginx反向代理与负载均衡
目录
nginx是一款高性能的 Web服务器和反向代理服务器。可以很方便的实现动静分离以及负载均衡。它可以将客户端的请求转发给多个后端服务器,并实现负载均衡,以提高系统的性能和可靠性。
1. 反向代理配置:
nginx
实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx
发布的路径去读取,而不需要从后台服务器获取了。
Http Proxy`模块,功能很多,最常用的是`proxy_pass`和`proxy_cache
如果要使用proxy_cache
,需要集成第三方的ngx_cache_purge
模块,用来清除指定的URL缓存。这个集成需要在安装nginx
的时候去做,如:
./configure --add-module=../ngx_cache_purge-1.0 ......
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://www.example.com;
}
}
2.负载均衡配置:
nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内在upstream段内,定义一个服务器列表servers.com,其中包含了多个后端服务器的地址,Nginx会根据负载均衡算法将请求分发给这些后端服务器。
服务器列表默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:
upstream www.servers.com {
ip_hash;
#weight=5表示这个服务器被分配到的概率是下面的5倍
server 127.0.0.1:9080 weight=5;
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:1111;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://www.servers.com;
}
}
注意:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。
3.HTTP代理IP
3.1 正向代理
正向代理是最常见的代理类型之一,它代表客户端进行网络请求,并将请求转发给目标服务器。在这种代理类型中,客户端并不直接与目标服务器进行通信,而是通过正向代理服务器进行通信。正向代理服务器能够帮助用户访问受限网站,隐藏用户真实IP地址,提高安全性。
3.2 反向代理
反向代理是另一种常见的代理类型,它代表目标服务器进行网络请求,并将响应返回给客户端。在这种代理类型中,客户端并不知道自己正在与反向代理服务器进行通信,而是认为自己正在直接与目标服务器进行通信。反向代理服务器可以根据请求的特征和目标服务器的负载情况,将请求转发到不同的目标服务器上,实现负载均衡和容错能力。
3.3 透明代理
透明代理是一种不需要客户端配置的代理类型,客户端无需知道自己正在使用代理服务器。透明代理通常是在网络中的路由器或防火墙上实现的,可以对所有流经该设备的网络流量进行监控。
4. Nginx负载均衡与动静分离
环境说明
主机 | x | 安装服务 | ip |
---|---|---|---|
NGINX | redhat8 | nginx | 192.168.200.42 |
LNMP | redhat8 | nginx,mysql,php | 192.168.200.46 |
nginx | redhat8 | nginx | 192.168.200.47 |
4.1NGINX主机安装nginx
#关闭防火墙 SElinux
[root@NGINX ~]# systemctl disable --now firewalld
[root@NGINX ~]# sed -i 's/^SELINUX=*/SELINUX=disabled/g' /etc/selinux/config
[root@NGINX ~]# setenforce 0
#配置yum源
[root@NGINX ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@NGINX ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@NGINX ~]# yum makecache
#配置epel源
[root@NGINX ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@NGINX ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@NGINX ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
#创建系统用户nginx
[root@NGINX ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@NGINX ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
#预安装Development Tools 包组
[root@NGINX ~]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@NGINX ~]# mkdir -p /var/log/nginx
[root@NGINX ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@NGINX ~]# cd /usr/src/
[root@NGINX src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@NGINX src]# tar xf nginx-1.24.0.tar.gz
#编译安装
[root@NGINX ~]# cd /usr/src/nginx-1.24.0
[root@NGINX nginx-1.24.0]#
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@NGINX nginx-1.24.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@NGINX ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@NGINX ~]# . /etc/profile.d/nginx.sh
#修改配置文件
# 修改配置文件
[root@NGINX ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes 4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 1000;
error_log /var/log/nginx/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
# 动态负载均衡
upstream dynamic {
server 192.168.200.46 weight=1;
}
# 静态负载均衡
upstream static {
server 192.168.226.47 weight=1;
}
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/host.access.log main;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 动静分离
location / {
proxy_pass http://static;
}
location ~ \.php$ {
proxy_pass http://dynamic;
}
}
}
#重启nginx
[root@NGINX ~]# systemctl restart nginx
4.2LNMP主机安装lnmp服务
4.3 nginx主机安装nginx
#关闭防火墙 SElinux
[root@nginx ~]# systemctl disable --now firewalld
[root@nginx ~]# sed -i 's/^SELINUX=*/SELINUX=disabled/g' /etc/selinux/config
[root@nginx ~]# setenforce 0
#配置yum源
[root@nginx ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
[root@nginx ~]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@nginx ~]# yum makecache
#配置epel源
[root@nginx ~]# yum install -y https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
[root@nginx ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@nginx ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
#创建系统用户nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@nginx ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make wget vim
#预安装Development Tools 包组
[root@nginx ~]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget http://nginx.org/download/nginx-1.24.0.tar.gz
[root@nginx src]# tar xf nginx-1.24.0.tar.gz
#编译安装
[root@nginx ~]# cd /usr/src/nginx-1.24.0
[root@nginx nginx-1.24.0]#
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log
[root@nginx nginx-1.24.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install
#配置环境变量
[root@nginx ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@nginx ~]# . /etc/profile.d/nginx.sh
#配置nginx配置文件
[root@nginx ~]# cd /usr/local/nginx/conf
[root@nginx conf]# vim nginx.conf
worker_processes 4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 1000;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
server_name localhost;
access_log logs/host.access.log main;
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location / {
root html;
index index.html index.htm;
}
location = /50x.html {
root html;
}
}
}
#配置服务启动脚本
[root@nginx ~]# vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=OpenSSH server daemon
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecStop=/usr/local/nginx/sbin/nginx -s stop
ExecReload=/usr/local/nginx/sbin/nginx -s reload
[Install]
WantedBy=multi-user.target
[root@nginx ~]# systemctl daemon-reload //重新加载
[root@nginx ~]# systemctl start nginx
[root@nginx ~]# systemctl enable nginx
[root@nginx ~]# systemctl status nginx
● nginx.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2023-10-26 14:53:17 CST; 12s ago
Main PID: 31724 (nginx)
Tasks: 5 (limit: 11301)
Memory: 5.7M
CGroup: /system.slice/nginx.service
├─31724 nginx: master process /usr/local/nginx/sbin/nginx
├─31725 nginx: worker process
├─31726 nginx: worker process
├─31727 nginx: worker process
└─31728 nginx: worker process
Oct 26 14:53:17 nginx systemd[1]: Starting OpenSSH server daemon...
Oct 26 14:53:17 nginx systemd[1]: Started OpenSSH server daemon.
Oct 26 14:53:17 nginx systemd[1]: Started OpenSSH server daemon.
[root@nginx ~]# cd /usr/local/nginx/html/
[root@nginx html]# ll
total 8
-rw-r--r-- 1 root root 497 Oct 26 14:51 50x.html
-rw-r--r-- 1 root root 615 Oct 26 14:51 index.html
[root@nginx html]# vim abc.html
[root@nginx html]# cat abc.html
hello world
#测试
[root@nginx ~]# curl http://192.168.200.47/abc.html
hello world