Nginx反向代理+负载均衡

Posted 我是一只小小茑

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

1.nginx的反向代理:proxy_pass

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 root 1260 Oct 8 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
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 52 192.168.43.200:22 192.168.43.147:65306
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::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反向代理+负载均衡_Nginx反向代理+负载均衡\'

至此,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

\'Nginx反向代理+负载均衡_Nginx反向代理+负载均衡_02\'

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

\'Nginx反向代理+负载均衡_nginx安装_03\'

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

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
charset utf-8;

log_format main \'$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\';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;


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;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/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
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 52 192.168.43.200:22 192.168.43.147:65306
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@localhost vhosts]#

\'Nginx反向代理+负载均衡_nginx安装_04\'

本机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;
}
}

\'Nginx反向代理+负载均衡_nginx安装_05\'

本机80端口转发正常!

nginx反向代理完成!

6.3配置Nginx反向代理+负载均衡

[root@localhost conf]# cat nginx.conf

#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
charset utf-8;

log_format main \'$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\';
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;


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;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/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
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:80 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 52 192.168.43.200:22 192.168.43.147:65306
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@localhost vhosts]#


检查WEB效果:因B和C的web需要加app,访问Nginx也需要加

\'Nginx反向代理+负载均衡_Nginx反向代理+负载均衡_06\'

\'Nginx反向代理+负载均衡_nginx安装_07\'

不断刷新,依次轮询访问B和C的WEB服务器!


以上是关于Nginx反向代理+负载均衡的主要内容,如果未能解决你的问题,请参考以下文章

nginx--❤️图解及代码实现正向代理反向代理及负载均衡(非常实用,建议收藏❤️)

nginx做反向代理负载均衡 Java怎么获取后端服务器获取用户IP

nginx做反向代理负载均衡 Java怎么获取后端服务器获取用户IP

Nginx反向代理负载均衡, keepalived高可用

Nginx实现反向代理负载均衡功能

Nginx代理——正向反向代理,动静分离和负载均衡