nginx实现负载均衡和动静分离

反向代理与负载均衡

nginx`通常被用作后端服务器的反向代理,这样就可以很方便的实现动静分离以及负载均衡,从而大大提高服务器的处理能力。

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

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

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;
}

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

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

server {
  location / {
    proxy_pass http://idfsoft.com;
  }
}
主机名ip服务
c1192.168.96.129lnmp
c2192.168.96.133nginx
c3192.168.96.134apache

c1部署lnmp

安装nginx

#关闭防火墙和selinx
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
#创建系统用户nginx
[root@localhost ~]# useradd -r -M -s /sbin/nologin nginx
#安装依赖环境
[root@localhost ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make
#安装过程略....
[root@localhost ~]# yum -y groups mark install 'Development Tools'
#创建日志存放目录
[root@localhost ~]# mkdir -p /var/log/nginx
[root@localhost ~]# chown -R nginx.nginx /var/log/nginx
#下载nginx
[root@localhost ~]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.1.tar.gz

编译安装

[root@localhost ~]# tar xf nginx-1.20.1.tar.gz 
[root@localhost ~]# ls
anaconda-ks.cfg  nginx-1.20.1  nginx-1.20.1.tar.gz
[root@localhost ~]# cd nginx-1.20.1
[root@localhost 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@localhost nginx-1.20.1]# make
[root@localhost nginx-1.20.1]# make install

nginx安装后配置

#配置环境变量
[root@localhost ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@localhost ~]# source /etc/profile.d/nginx.sh
//服务控制方式,使用nginx命令
    -t  //检查配置文件语法
    -v  //输出nginx的版本
    -c  //指定配置文件的路径
    -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}
#输出nginx的版本
[root@localhost ~]# nginx -v
nginx version: nginx/1.20.1
#查看nginx有那些功能
[root@localhost ~]# nginx -V
nginx version: nginx/1.20.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-2) (GCC) 
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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@localhost ~]# 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@localhost ~]# nginx 
[root@localhost ~]# 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              [::]:*

安装mysql

请参考mysql二进制安装

安装php

运行以下命令添加并更新epel源

[root@localhost ~]# dnf -y install epel-release
[root@localhost ~]# dnf update epel-release

运行以下命令删除缓存的无用软件包并更新软件源。

[root@localhost ~]# dnf clean all
[root@localhost ~]# dnf makecache

启用php:7.3模块

[root@localhost ~]# dnf module enable php:7.3

运行以下命令安装PHP相应的模块

[root@localhost ~]# dnf install php php-curl php-dom php-exif php-fileinfo php-fpm php-gd php-hash php-json php-mbstring php-mysqli php-openssl php-pcre php-xml libsodium
#安装过程省略
#查看PHP版本
[root@localhost ~]# php -v
PHP 7.3.20 (cli) (built: Jul  7 2020 07:53:49) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.20, Copyright (c) 1998-2018 Zend Technologies

配置Nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
            root   html;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            #添加默认首页信息index.php。
            index  index.html index.htm index.php;
        }
#去掉被注释的location ~ \.php$大括号内容前的#,并修改大括号的内容。修改完成如下所示
location ~ \.php$ {
            proxy_pass   http://127.0.0.1;
            root           /usr/local/nginx/html;
             #Nginx通过unix套接字与PHP-FPM建立联系,该配置与/etc/php-fpm.d/www.conf文件内的listen配置一致。
            fastcgi_pass   unix:/run/php-fpm/www.sock;
             fastcgi_index  index.php;
             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
              #Nginx调用fastcgi接口处理PHP请求。
            include        fastcgi_params;
        }
[root@localhost ~]# nginx -s stop
[root@localhost ~]# nginx

配置PHP

[root@localhost ~]# vim /etc/php-fpm.d/www.conf
#找到user = apache和group = apache,将apache修改为nginx
user = nginx
; RPM: Keep a group allowed to write in log dir.
group = nginx
#新建phpinfo.php文件,用于展示PHP信息
[root@localhost ~]# vim /usr/local/nginx/html/phpinfo.php
#输入下列内容,函数phpinfo()​会展示PHP的所有配置信息。
<?php echo phpinfo(); ?>
#运行以下命令启动PHP-FPM
[root@localhost ~]# systemctl start php-fpm
#运行以下命令设置PHP-FPM开机自启动
[root@localhost ~]# systemctl enable php-fpm

c2部署nginx和c1安装的步骤一样

c3部署apache

安装httpd

[root@localhost ~]# dnf -y groups mark install "Development Tools"
[root@localhost ~]# useradd -r -M -s /sbin/nologin apache
[root@localhost ~]# dnf -y install openssl-devel pcre-devel expat-devel libtool gcc gcc-c++ make bzip2 openssl
[root@localhost ~]# tar xf apr-1.7.0.tar.bz2 
[root@localhost ~]# tar xf apr-util-1.6.1.tar.bz2 
[root@localhost ~]# tar xf httpd-2.4.43.tar.bz2 
[root@localhost ~]# ls
anaconda-ks.cfg    apr-util-1.6.1.tar.bz2
apr-1.7.0          httpd-2.4.43
apr-1.7.0.tar.bz2  httpd-2.4.43.tar.bz2
apr-util-1.6.1     mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
[root@localhost ~]# cd apr-1.7.0
[root@localhost apr-1.7.0]# ls
apr-config.in  build-outputs.mk  helpers       misc           strings
apr.dep        CHANGES           include       mmap           support
apr.dsp        CMakeLists.txt    libapr.dep    network_io     tables
apr.dsw        config.layout     libapr.dsp    NOTICE         test
apr.mak        configure         libapr.mak    NWGNUmakefile  threadproc
apr.pc.in      configure.in      libapr.rc     passwd         time
apr.spec       docs              LICENSE       poll           tools
atomic         dso               locks         random         user
build          emacs-mode        Makefile.in   README
build.conf     encoding          Makefile.win  README.cmake
buildconf      file_io           memory        shmem
[root@localhost apr-1.7.0]# vim configure
$RM "$cfgfile"		#删除或注释此行
[root@localhost apr-1.7.0]#  ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.0]# make
[root@localhost apr-1.7.0]# make install
[root@localhost apr-1.7.0]# cd ../apr-util-1.6.1
[root@localhost apr-util-1.6.1]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.1]# make
[root@localhost apr-util-1.6.1]# make install
[root@localhost apr-util-1.6.1]cd ../httpd-2.4.43
[root@localhost httpd-2.4.43]# ./configure --prefix=/usr/local/apache \
--sysconfdir=/etc/httpd24 \
--enable-so \
--enable-ssl \
--enable-cgi \
--enable-rewrite \
--with-zlib \
--with-pcre \
--with-apr=/usr/local/apr \
--with-apr-util=/usr/local/apr-util/ \
--enable-modules=most \
--enable-mpms-shared=all \
--with-mpm=prefork
[root@localhost httpd-2.4.43]# make
[root@localhost httpd-2.4.43]# make install
#安装后配置
[root@localhost ~]#  echo 'export PATH=/usr/local/apache/bin:$PATH' > /etc/profile.d/httpd.sh
[root@localhost ~]# source /etc/profile.d/httpd.sh 
[root@localhost ~]# ln -s /usr/local/apache/include/ /usr/include/httpd
[root@localhost ~]# echo 'MANPATH /usr/local/apache/man' >> /etc/man.config
[root@localhost ~]# apachectl start
[root@localhost ~]# ss -antl
State   Recv-Q  Send-Q   Local Address:Port   Peer Address:Port Process  
LISTEN  0       128            0.0.0.0:22          0.0.0.0:*             
LISTEN  0       128                  *:80                *:*             
LISTEN  0       128               [::]:22             [::]:* 
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.

配置nginx主机的nginx配置文件

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
        location / {
            root   html;   #删掉这两行
            index  index.html index.htm;
        }
在server上面添加
    upstream html {
    server 192.168.96.129;
    }
    upstream php {
    server 192.168.96.134;
    }
在location下
        location / {
            proxy_pass http://html; #改成这个
        }
把下面的\.php的取消注释
        location ~ \.php$ {
            proxy_pass   http://php; #添加这一行
        }

效果

在这里插入图片描述

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值