nginx反向代理

我就是我 2024-04-18 07:27 139阅读 0赞

拓扑

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70

IP规划:

nginx反向代理服务器:192.168.14.16

nginx-web1:192.168.14.12

nginx-web2:192.168.14.13

client:192.168.14.40

Nginx反向代理proxy与负载均衡upstream

配置反向代理proxy:proxy_pass url地址

配置负载均衡upstream:upstream

官方配置:http://nginx.org/en/docs

注意:反向代理之后获取客户端IP地址为nginx服务器地址,这里需要nginx进行forward,设置真实的IP地址:

proxy_set_header X-real-ip $remote_addr;

一、nginx反向代理

1、关闭防火墙和selinux

  1. [root@nginx ~]# systemctl stop firewalld
  2. [root@nginx ~]# systemctl disable firewalld
  3. [root@nginx ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2、配置nginx源;同等[root@nginx ~]#vim /etc/yum.repos.d/nginx.repo

  1. [root@nginx ~]# cat << EOF >>/etc/yum.repos.d/nginx.repo
  2. > [nginx]
  3. > name=aliyun epel
  4. > baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
  5. > gpgcheck=0
  6. > EOF

3、安装nginx

  1. [root@nginx ~]# yum install -y nginx

4、修改nginx配置文件:[root@nginx ~]# vim /etc/nginx/nginx.conf

nginx负载均衡的配置upstream是在http内,server外(是与sever平级关系)

web_up用户自定义

  1. upstream web_up {
  2. server 192.168.14.12:80 max_fails=3 fail_timeout=20s weight=2;
  3. server 192.168.14.13:80 max_fails=3 fail_timeout=20s weight=2;
  4. }

weight:权重,越大,访问的机会越多(其实还有其他的负载均衡方法,比如hash)
max_fails:最大失败次数
fail_timeout:最大连接时间(如果超过了就是一次fails,连续两次超时就达到了max_fails=2,则判断它挂掉了)

  1. location / {
  2. proxy_pass http://web_up; #对应上面upstream定义的web_up
  3. proxy_store off; #启用本地缓存功能
  4. proxy_redirect off; #指定修改被代理服务器返回的响应头中的location头域跟refresh头域数值
  5. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #定义或添加字段传递给代理服务器的请求头
  6. proxy_set_header X-Real-IP $remote_addr;
  7. proxy_set_header Host $host;
  8. }

watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3RsYWRhZ2lv_size_16_color_FFFFFF_t_70 1

5、检测配置文件

  1. [root@nginx ~]# /usr/sbin/nginx -t
  2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  3. nginx: configuration file /etc/nginx/nginx.conf test is successful

6、启动nginx

  1. [root@nginx ~]# systemctl start nginx

二、nginx-web1(nginx-web2一样配置)

1、关闭防火墙和selinux

  1. [root@web1 ~]# systemctl stop firewalld
  2. [root@web1 ~]# systemctl disable firewalld
  3. [root@web1 ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

2、配置nginx源,[root@web1 ~]#vim /etc/yum.repos.d/nginx.repo

  1. [nginx]
  2. name=aliyun epel
  3. baseurl=http://mirrors.aliyun.com/epel/7Server/x86_64/
  4. gpgcheck=0

3、安装nginx

  1. [root@web1 ~]# yum install -y nginx

4、修改页面,方便识别

  1. [root@web1 ~]# cp /usr/share/nginx/html/index.html /usr/share/nginx/html/index.html.bak
  2. [root@web1 ~]# vim /usr/share/nginx/html/index.html

查看

  1. [root@web1 ~]# cat /usr/share/nginx/html/index.html
  2. this is 12 server
  3. [root@web2 ~]# cat /usr/share/nginx/html/index.html
  4. this is 13 server

5、启动nginx

  1. [root@web1 ~]# systemctl start nginx

三、Client客户端

1、验证

20190903173126584.png

发表评论

表情:
评论列表 (有 0 条评论,139人围观)

还没有评论,来说两句吧...

相关阅读