反向代理与负载均衡

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 {							// 此字段要写在server字段的前面
  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 webservers{
        server 192.168.23.161 weight=3;    // weight表示访问三次134后访问一次147
        server 192.168.23.180;

    }

注:这个方法本质还是轮询,而且由于客户端的ip可能是不断变化的,比如动态ip,代理,翻墙等,因此ip_hash并不能完全保证同一个客户端总是由同一个服务器来处理。

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

server {
  location / {
    proxy_pass http://webservers;				// 这里要和upstream段配置的域名一致
  }
}

nginx动静分离实验

实验环境说明:

主机IP服务
JLin-httpd192.168.23.160Apache
JLin-nginx192.168.23.161Nginx
lnmp192.168.23.180LNMP

安装httpd

#httpd
[root@JLin-httpd ~]# yum -y install httpd

// 开启httpd服务
[root@JLin-httpd ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.

// 关闭防火墙和selinux
[root@JLin-httpd ~]# systemctl disable --now  firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@JLin-httpd ~]# setenforce 0
[root@JLin-httpd ~]# vi /etc/selinux/config 
SELINUX=disabled

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

安装nginx服务

#nginx
//创建系统用户nginx
[root@JLin-nginx ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖包、包组和使用工具
[root@JLin-nginx ~]# yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel

[root@JLin-nginx ~]# yum -y groups mark install 'Development Tools'

//创建日志存放目录
[root@JLin-nginx ~]# mkdir -p /var/log/nginx
[root@JLin-nginx ~]# chown -R nginx.nginx /var/log/nginx

//下载nginx
[root@JLin-nginx ~]# cd /usr/src/
[root@JLin-nginx src]# wget https://nginx.org/download/nginx-1.20.1.tar.gz

//编译安装
[root@JLin-nginx src]# ls
debug  kernels  nginx-1.20.1.tar.gz
[root@JLin-nginx src]# tar xf nginx-1.20.1.tar.gz
[root@JLin-nginx src]# cd nginx-1.20.1
[root@JLin-nginx nginx-1.20.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-debug \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_image_filter_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--http-log-path=/var/log/nginx/access.log \
--error-log-path=/var/log/nginx/error.log

[root@JLin-nginx nginx-1.20.1]# make && make install

[root@JLin-nginx nginx-1.20.1]# cd
[root@JLin-nginx ~]#

// 配置环境变量
[root@JLin-nginx ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@JLin-nginx ~]# source /etc/profile.d/nginx.sh

// 编写nginx启动文件
[root@JLin-nginx ~]# cat > /usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=Nginx server daemon
After=network.target 

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx 
ExecStop=/usr/local/nginx/sbin/nginx -s quit
ExecReload=/bin/kill -HUP \$MAINPID

[Install]
WantedBy=multi-user.target
EOF

// 效果验证
[root@JLin-nginx ~]# systemctl daemon-reload
[root@JLin-nginx ~]# systemctl enable --now nginx.service

[root@JLin-nginx ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process                                                     
LISTEN 0      128          0.0.0.0:80          0.0.0.0:*                                                                
LISTEN 0      128          0.0.0.0:22          0.0.0.0:*                                                                
LISTEN 0      128             [::]:22             [::]:*    

// 关闭防火墙和selinux
[root@JLin-nginx ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@JLin-nginx ~]# setenforce 0
[root@JLin-nginx ~]# vim /etc/selinux/config
SELINUX=disabled

web页面访问nginx

LNMP安装

LNMP脚本下载

php

以上页面都访问成功之后就可以开始配置负载均衡和动静分离了

在nginx主机上配置负载均衡

#nginx
[root@JLin-nginx ~]# vim /usr/local/nginx/conf/nginx.conf
     upstream test {                  // 此字段要在server字段前面,之前提到过的
        server 192.168.23.161;
        server 192.168.23.180;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://test;   // test要与上面的upstream后面所跟的一样
        }

// 检查语法
[root@JLin-nginx ~]# 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@JLin-nginx ~]# nginx -s reload

web页面访问192.168.23.161http1php

在nginx主机上配置动静分离

#nginx
[root@JLin-nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream test1 {
        server 192.168.23.161;
    }

    upstream test2 {
        server 192.168.23.180;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://test1;
        }

        location ~ \.php$ {
            proxy_pass http://test2;
        }

        #error_page  404              /404.html;

// 检查语法
[root@JLin-nginx ~]# 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@JLin-nginx ~]# nginx -s reload

访问根目录下的静态资源会自动跳转到192.168.23.160上进行访问http1

访问根目录下的.php动态资源会自动跳转到192.168.23.180上进行访问

php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

汉只只

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值