CentOS7 nginx反向代理
nginx设置反向代理后,客户端只需要将请求发送到反向代理服务器lb,由反向代理服务器lb去选择目标服务器获取数据后,再返回给客户端,此时反向代理服务器和目标服务器对外就是一个逻辑服务器,仅仅对外暴露的是代理服务器地址,从而隐藏了真实服务器IP地址。这里用两台web服务器使用apache提供服务来模拟环境
拓扑
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
本站所有文章均可随意转载,转载时请保留原文链接及作者。