人生难得几回搏,此时不搏待何时。这篇文章主要讲述Nginx反向代理+负载均衡相关的知识,希望能为你提供帮助。
nginx反向代理+负载均衡一、?实验需求:
1)访问A机器的80端口,反向代理到B机器的80端口;
访问http://192.168.43.200/app,反向代理到B机器http://192.168.43.201/app
2)访问A机器的88端口,反向代理到C机器的88端口;
访问http://192.168.43.200:88/app,反向代理到B机器http://192.168.43.201:88/app
3).访问A机器的80端口,负载均衡到后端的两台机器B和C的80端口?
二、实验环境
A: centos7.9--ip:192.168.43.200 | web: Nginx
192.168.43.200 |
B: centos7.9--ip:192.168.43.201 | web: Apache 192.168.43.201 |
C: centos7.2--ip:192.168.43.202 | web: Apache 192.168.43.202 |
2.nginx的负载均衡:upstream?
三、A机器安装Nginx
创建nginx用户
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]#
安装依赖包
[root@localhost ~]# yum -y install pcre-devel zlib-devel gcc gcc-c++ make
我已经提前把nginx安装包下载好并上传至服务器了
[root@localhost ~]# ll
total 1016
-rw-------. 1 root root1260 Oct8 08:16 anaconda-ks.cfg
-rw-r--r--1 root root 1032630 Oct 12 00:23 nginx-1.16.1.tar.gz
[root@localhost ~]# tar zxvf nginx-1.16.1.tar.gz
[root@localhost ~]# cd nginx-1.16.1
[root@localhost nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@localhost nginx-1.16.1]# make & & make install
启动服务
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.16.1]# ss -ant
StateRecv-Q Send-QLocal Address:PortPeer Address:Port
LISTEN0128*:80*:*
LISTEN0128*:22*:*
LISTEN0100127.0.0.1:25*:*
ESTAB052192.168.43.200:22192.168.43.147:65306
LISTEN0128[::]:22[::]:*
LISTEN0100[::1]:25[::]:*
nginx 停止、重新加载配置
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost nginx-1.16.1]# /usr/local/nginx/sbin/nginx -s reload
文章图片
至此,nginx服务安装成功!
四、B机器安装Apache
[root@python ~]# yum -y install httpd
[root@python ~]# mkdir /var/www/html/app
[root@python app]# echo $HOSTNAME `hostname -i` > > /var/www/html/app/index.html
[root@python www]# chown -R apache.apache /var/www/html/
[root@python app]# systemctl restart httpd
文章图片
B机器安装完成,
五、C机器安装Apache
[root@python ~]# yum -y install httpd
[root@python ~]# mkdir /var/www/html/app
[root@python app]# echo $HOSTNAME `hostname -i` > > /var/www/html/app/index.html
[root@python www]# chown -R apache.apache /var/www/html/
[root@python app]# systemctl restart httpd
文章图片
C机器安装完成,
六、配置Nginx
[root@localhost opt]# cd /usr/local/nginx/conf
[root@localhost conf]# [root@localhost conf]# vim nginx.conf
6.1Nginx安装后的配置规范,
[root@localhost conf]# cat nginx.conf
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events {
worker_connections1024;
}
http {
includemime.types;
default_typeapplication/octet-stream;
charset utf-8;
log_formatmain\'$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" \'
\'$status $body_bytes_sent "$http_referer" \'
\'"$http_user_agent" "$http_cookie" $host $request_time\';
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
client_header_timeout 600s;
client_body_timeout 600s;
client_max_body_size 100m;
client_body_buffer_size 256k;
gzipon;
gzip_min_length1k;
gzip_buffers4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_typestext/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on;
include vhosts/*.conf;
}
[root@localhost conf]#
6.2配置Nginx-server反向代理
[root@localhost vhosts]# mkdir -p /usr/local/nginx/conf/vhosts
[root@localhost conf]# cd /usr/local/nginx/conf/vhosts
[root@localhost conf]# vim 201.conf
配置反向代理:(本机端口88转发到B机器的WEB:192.168.43.201)
[root@localhost vhosts]# cat 201.conf
server {
listen 88; #监听本机端口号
server_name localhost;
index index.html index.php index.htm;
root /var/www/html;
access_log/usr/local/nginx/logs/80-access.log main;
error_log/usr/local/nginx/logs/80-error.log;
location / {
proxy_pass http://192.168.43.201/app/; #B服务器url
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300; #跟后端服务器连接超时时间,发起握手等候响应时间
proxy_send_timeout 300; #后端服务器回传时间,就是在规定时间内后端服务器必须传完所有数据
proxy_read_timeout 600; #连接成功后等待后端服务器的响应时间,已经进入后端的排队之中等候处理
proxy_buffer_size 256k; #代理请求缓冲区,会保存用户的头信息以供nginx进行处理
proxy_buffers 4 256k; #同上,告诉nginx保存单个用几个buffer最大用多少空间
proxy_busy_buffers_size 256k; #如果系统很忙时候可以申请最大的proxy_buffers
proxy_temp_file_write_size 256k; #proxy缓存临时文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
?listen 88; #监听端口
proxy_pass http://192.168.43.201/app/; #反向代理web地址?
重启Nginx服务并测试
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx
[root@localhost vhosts]# ss -ant
StateRecv-Q Send-QLocal Address:PortPeer Address:Port
LISTEN0128*:80*:*
LISTEN0128*:22*:*
LISTEN0100127.0.0.1:25*:*
ESTAB052192.168.43.200:22192.168.43.147:65306
LISTEN0128[::]:22[::]:*
LISTEN0100[::1]:25[::]:*
[root@localhost vhosts]#
文章图片
本机88端口转发正常!
配置反向代理(本机端口80转发到C机器的WEB:192.168.43.202)
[root@localhost vhosts]# cat 202.conf
server {
listen 80;
server_name localhost;
index index.html index.php index.htm;
root /var/www/html/app/;
access_log/usr/local/nginx/logs/80-access.log main;
error_log/usr/local/nginx/logs/80-error.log;
location / {
proxy_pass http://192.168.43.202/app/;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300; #跟后端服务器连接超时时间,发起握手等候响应时间
proxy_send_timeout 300; #后端服务器回传时间,就是在规定时间内后端服务器必须传完所有数据
proxy_read_timeout 600; #连接成功后等待后端服务器的响应时间,已经进入后端的排队之中等候处理
proxy_buffer_size 256k; #代理请求缓冲区,会保存用户的头信息以供nginx进行处理
proxy_buffers 4 256k; #同上,告诉nginx保存单个用几个buffer最大用多少空间
proxy_busy_buffers_size 256k; #如果系统很忙时候可以申请最大的proxy_buffers
proxy_temp_file_write_size 256k; #proxy缓存临时文件的大小
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
文章图片
本机80端口转发正常!
nginx反向代理完成!
6.3配置Nginx反向代理+负载均衡
[root@localhost conf]# cat nginx.conf
#usernobody;
worker_processes1;
#error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
#pidlogs/nginx.pid;
events {
worker_connections1024;
}
http {
includemime.types;
default_typeapplication/octet-stream;
charset utf-8;
log_formatmain\'$http_x_forwarded_for $remote_addr $remote_user [$time_local] "$request" \'
\'$status $body_bytes_sent "$http_referer" \'
\'"$http_user_agent" "$http_cookie" $host $request_time\';
sendfileon;
tcp_nopushon;
tcp_nodelayon;
keepalive_timeout65;
fastcgi_connect_timeout 3000;
fastcgi_send_timeout 3000;
fastcgi_read_timeout 3000;
fastcgi_buffer_size 256k;
fastcgi_buffers 8 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
fastcgi_intercept_errors on;
client_header_timeout 600s;
client_body_timeout 600s;
client_max_body_size 100m;
client_body_buffer_size 256k;
gzipon;
gzip_min_length1k;
gzip_buffers4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_typestext/plain application/x-Javascript text/css application/xml text/javascript application/x-httpd-php;
gzip_vary on;
include vhosts/*.conf;
}
[root@localhost conf]#
配置负载均衡为默认轮询模式
[root@localhost vhosts]# pwd
/usr/local/nginx/conf/vhosts
[root@localhost vhosts]# ls -l
total 4
-rw-r--r-- 1 nginx nginx 1208 Oct 12 03:43 LB.conf
[root@localhost vhosts]# cat LB.conf
upstream webserver {
server 192.168.43.201 max_fails=3 fail_timeout=30s; #max_fails = 3 为允许失败的次数,默认值为1
server 192.168.43.202 max_fails=3 fail_timeout=30s; #fail_timeout = 30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间
}
server {
listen 80;
server_name localhost;
index index.html index.php index.htm;
root /var/www/html;
access_log/usr/local/nginx/logs/80-access.log main;
error_log/usr/local/nginx/logs/80-error.log;
location / {
proxy_pass http://webserver ;
proxy_redirect off ;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 600;
proxy_buffer_size 256k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_max_temp_file_size 128m;
}
}
[root@localhost vhosts]#
?upstream webserver { #负载均衡的http upstream模块
server 192.168.43.201 max_fails=3 fail_timeout=30s; #max_fails = 3 为允许失败的次数,默认值为1
server 192.168.43.202 max_fails=3 fail_timeout=30s; #fail_timeout = 30s 当max_fails次失败后,暂停将请求分发到该后端服务器的时间
}?
proxy_pass http://webserver ; #集群名称, ?upstream webserver
重启Nginx服务
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx -s stop
[root@localhost vhosts]# /usr/local/nginx/sbin/nginx
[root@localhost vhosts]# ss -ant
StateRecv-Q Send-QLocal Address:PortPeer Address:Port
LISTEN0128*:80*:*
LISTEN0128*:22*:*
LISTEN0100127.0.0.1:25*:*
ESTAB052192.168.43.200:22192.168.43.147:65306
LISTEN0128[::]:22[::]:*
LISTEN0100[::1]:25[::]:*
[root@localhost vhosts]#
?检查WEB效果:因B和C的web需要加app,访问Nginx也需要加
文章图片
文章图片
不断刷新,依次轮询访问B和C的WEB服务器!
【Nginx反向代理+负载均衡】
推荐阅读
- Linux Centos7目录结构总结
- Linux运维基础交换机和路由器的区别是什么?
- MySQL一键安装入门教程
- 0基础linux运维教程 sersync介绍
- 北京软件开发公司排名-最大的软件开发公司有哪些呢
- “智慧工地“有哪些高科技(一起来看看)
- Linux 服务器断电查看关机时间
- 在Redux Framework中无法打开子菜单
- 基于类别的样式发布存档博客预览