当前位置:首页 > 系统 > CentOS7 nginx反向代理

CentOS7 nginx反向代理

系统2年前 (2021-01-11)

CentOS7 nginx反向代理

nginx设置反向代理后,客户端只需要将请求发送到反向代理服务器lb,由反向代理服务器lb去选择目标服务器获取数据后,再返回给客户端,此时反向代理服务器和目标服务器对外就是一个逻辑服务器,仅仅对外暴露的是代理服务器地址,从而隐藏了真实服务器IP地址。这里用两台web服务器使用apache提供服务来模拟环境


拓扑

CentOS7 nginx反向代理


web1

hostnamectl --static set-hostname web1 && su
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld.service && systemctl disable firewalld.service && systemctl status firewalld.service
yum install -y httpd
echo `hostname` >/var/www/html/index.html
systemctl start httpd & systemctl enable httpd


web2

hostnamectl --static set-hostname web2 && su
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld.service && systemctl disable firewalld.service && systemctl status firewalld.service
yum install -y httpd
echo `hostname` >/var/www/html/index.html
systemctl start httpd & systemctl enable httpd


负载均衡器lb

客户端通过访问不同的端口来访问不同的服务器

hostnamectl --static set-hostname lb && su
setenforce 0 && sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
systemctl stop firewalld.service && systemctl disable firewalld.service && systemctl status firewalld.service
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
yum install nginx -y 
systemctl restart nginx && systemctl enable nginx
touch /var/log/nginx/10.log
touch /var/log/nginx/20.log
sed -i 's/remote_addr/http_x_real_ip/' /etc/nginx/nginx.conf

cat <<'EOF'  >/etc/nginx/conf.d/ReverseProxy.conf
server {
    listen 8080;
    server_name web1;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://172.16.1.10;
    }
    access_log /var/log/nginx/10.log;
}

server {
    listen 8090;
    server_name web2;
    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://172.16.1.20;
    }
    access_log /var/log/nginx/20.log;
}
EOF

systemctl restart nginx && systemctl enable nginx


客户端通过访问lb默认端口,访问不同的服务器

cat <<'EOF'  >/etc/nginx/conf.d/HA.conf
upstream HA-WEB {  
        server 172.16.1.10 weight=1 max_fails=1 fail_timeout=30;
        server 172.16.1.20 weight=1 max_fails=2 fail_timeout=30;
    }

server {
    listen    80;
    server_name  localhost;

    location / {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://HA-WEB;
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }
}
EOF


本站所有文章均可随意转载,转载时请保留原文链接及作者。

本文链接:https://www.vos.cn/os/478.html

相关文章

修复升级EVE-NG后无法正常打开问题

修复升级EVE-NG后无法正常打开问题

升级流程Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.9.40-eve-ng-ukms+ x86_64) * Documentation:  ...

利用LVM特性对EVE-NG硬盘分区扩容

利用LVM特性对EVE-NG硬盘分区扩容

下载的EVE-NG镜像中,存放模拟器镜像的分区太小了,上传几个cisco和juniper的模拟器就快满了,所以需要使用lvm特性对ubuntu server的硬盘分区进行扩容,首先先将eve-ng虚拟...

CentOS7 软RAID配置

CentOS7 软RAID配置

虚拟机添加4块新硬盘,形成5盘环境,做Linux软RAID磁盘阵列准备环境搭建添加硬件选择硬盘默认选择SCSI创建新虚拟磁盘容量默认20G,存储为单个文件加默认安装系统的,一共5个fdisk -l查看...

CentOS7 Samba配置

CentOS7 Samba配置

在RAID的基础上搭建samba服务提供文件共享参数介绍[global] workgroup = 工作组名称 server string = 服务...

CentOS7 安装Oracle12cR2

CentOS7 安装Oracle12cR2

在CentOS7.8 Minimal版本下安装Oracle Database 12c Release 2一键安装oracle-single-install.zip系统环境准备sed -i&n...

Red Hat Enterprise Linux 6.5安装和分区

Red Hat Enterprise Linux 6.5安装和分区

系统镜像名称为rhel-server-6.5-x86_64-dvd.iso安装Install or upgrade an existing system跳过安装源测试检查Next语言English键盘...