nginx反向代理与负载均衡、以及动静分离

什么是反向代理
在这里插入图片描述
使用nginx实现反向代理,Nginx只做请求的转发,后台有多个http服务器提供服务,nginx的功能就是把请求转发给后面的服务器,决定把请求转发给谁。
nginx通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。

nginx实现动静分离,其实就是在反向代理的时候,如果是静态资源,就直接从nginx发布的路径去读取,而不需要从后台服务器获取了。

但是要注意,这种情况下需要保证后端跟前端的程序保持一致,可以使用Rsync做服务端自动同步或者使用NFS、MFS分布式共享存储。

Http Proxy模块,功能很多,最常用的是proxy_pass和proxy_cache

如果要使用proxy_cache,需要集成第三方的ngx_cache_purge模块,用来清除指定的URL缓存。这个集成需要在安装nginx的时候去做,如:
./configure --add-module=…/ngx_cache_purge-1.0 …

nginx通过upstream模块来实现简单的负载均衡,upstream需要定义在http段内

在upstream段内,定义一个服务器列表,默认的方式是轮询,如果要确定同一个访问者发出的请求总是由同一个后端服务器来处理,可以设置ip_hash,如:

upstream idfsoft.com {
  ip_hash;
  server 127.0.0.1:9080 weight=5;
  server 127.0.0.1:8080 weight=5;
  server 127.0.0.1:1111;
}

定义好upstream后,需要在server段内添加如下内容:

server {
  location / {
    proxy_pass http://idfsoft.com;
  }
}

反向代理和负载均衡
环境说明

名称IP安装应用
DR调度器192.168.175.100nginx
yangcan1192.168.175.150apache
yangcan2192.168.175.151Apache

yangcan1和yangcan2安装Apache网站

yangcan1配置
[root@yangcan1 ~]# yum -y install httpd
[root@yangcan1 ~]# cd /var/www/html/
[root@yangcan1 html]# echo 'web1' >index.html
[root@yangcan1 html]# systemctl start httpd

yangcan2配置
[root@yangcan2 ~]# yum -y install httpd
[root@yangcan2 ~]# cd /var/www/html/
[root@yangcan2 html]# echo 'web2' >index.html
[root@yangcan2 html]# ls
index.html
[root@yangcan2 html]# systemctl start httpd

页面访问
在这里插入图片描述
在这里插入图片描述
调度器修改配置文件

[root@DR ~]# cd /usr/local/nginx/conf/
[root@DR conf]# ls
fastcgi.conf            koi-utf             nginx.conf           uwsgi_params
fastcgi.conf.default    koi-win             nginx.conf.default   uwsgi_params.default
fastcgi_params          mime.types          scgi_params          win-utf
fastcgi_params.default  mime.types.default  scgi_params.default
[root@DR conf]# vim nginx.conf
修改内容
#gzip  on;
    upstream yangcan.com {                //添加内容
        server 192.168.175.150 weight=2;      //添加IP,及权重
        server 192.168.175.151 weight=1;     //添加IP,及权重
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location / {
            proxy_pass http://yangcan.com;     //做反向代理
            }
[root@DR conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@DR conf]# nginx -s reload

本机测试(可以看到如上面配置一样,web1页面两次,web2页面一次)

C:\Users\Administrator>curl http://192.168.175.100
web1
C:\Users\Administrator>curl http://192.168.175.100
web1
C:\Users\Administrator>curl http://192.168.175.100
web2

页面访问调度器IP
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
添加ip_hash查看效果

[root@DR conf]# vim nginx.conf
添加ip_hash
#gzip  on;
    upstream yangcan.com {
        ip_hash;      //t添加内容
        server 192.168.175.150 weight=2;
        server 192.168.175.151 weight=1;
    }
[root@DR conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@DR conf]# nginx -s reload

本机测试(为了看到效果此时用本机测试而不是页面)
此时发现都是web1的页面,因为ip_hash的意思是如果访问的是第一个网站,那么后面一直访问的都是第一个网站

C:\Users\Administrator>curl http://192.168.175.100
web1
C:\Users\Administrator>curl http://192.168.175.100
web1
C:\Users\Administrator>curl http://192.168.175.100
web1
C:\Users\Administrator>curl http://192.168.175.100
web1

添加端口测试

[root@DR conf]# vim nginx.conf

#gzip  on;
    upstream yangcan.com {
        ip_hash;
        server 192.168.175.150:8080 weight=2;
        server 192.168.175.151:8081 weight=1;
    }
[root@DR conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@DR conf]# nginx -s reload

此时页面访问不了,去修改网站文件里面的端口号
[root@yangcan1 ~]# vim /etc/httpd/conf/httpd.conf 
#Listen 12.34.56.78:80
Listen 8080           //改为8080
[root@yangcan1 ~]# systemctl restart httpd

[root@yangcan2 ~]# vim /etc/httpd/conf/httpd.conf 
#Listen 12.34.56.78:80
Listen 8081
[root@yangcan2 ~]# systemctl restart httpd

此时就可以访问
在这里插入图片描述
动静分离
环境说明

主机名IP安装的应用
DRyangcan1nginx
yangcan1192.168.175.150lnmp
yangcan2192.168.175.151Apache
[root@DR conf]# vim nginx.conf

 #gzip  on;
    upstream static {
        server 192.168.175.151 weight=1;    //写静态网站的ip
    }
    upstream danamic {
        server 192.168.175.150 weight=2;   动态网站的ip
    }
    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

         location / {
            proxy_pass http://static;   //静态地址
            }
         location ~ \.php$ {
            proxy_pass http://danamic;    //动态地址
            }
[root@DR conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@DR conf]# nginx -s reload

页面访问
在这里插入图片描述

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值