博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx实现负载均衡和动静分离
阅读量:2134 次
发布时间:2019-04-30

本文共 9101 字,大约阅读时间需要 30 分钟。

反向代理与负载均衡

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 服务
c1 192.168.96.129 lnmp
c2 192.168.96.133 nginx
c3 192.168.96.134 apache

c1部署lnmp

安装nginx

#关闭防火墙和selinx[root@localhost ~]# systemctl disable firewalld[root@localhost ~]# vim /etc/selinux/configSELINUX=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 ~]# lsanaconda-ks.cfg  nginx-1.20.1.tar.gz

编译安装

[root@localhost ~]# tar xf nginx-1.20.1.tar.gz [root@localhost ~]# lsanaconda-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 -vnginx version: nginx/1.20.1#查看nginx有那些功能[root@localhost ~]# nginx -Vnginx version: nginx/1.20.1built by gcc 8.5.0 20210514 (Red Hat 8.5.0-2) (GCC) built with OpenSSL 1.1.1k FIPS 25 Mar 2021TLS SNI support enabledconfigure 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 -tnginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful[root@localhost ~]# nginx [root@localhost ~]# ss -antlState 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

安装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 -vPHP 7.3.20 (cli) (built: Jul  7 2020 07:53:49) ( NTS )Copyright (c) 1997-2018 The PHP GroupZend Engine v3.3.20, Copyright (c) 1998-2018 Zend Technologies

配置Nginx

[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conflocation / {
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修改为nginxuser = 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-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 ~]# lsanaconda-ks.cfg    apr-util-1.6.1.tar.bz2apr-1.7.0          httpd-2.4.43apr-1.7.0.tar.bz2  httpd-2.4.43.tar.bz2apr-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]# lsapr-config.in  build-outputs.mk  helpers       misc           stringsapr.dep        CHANGES           include       mmap           supportapr.dsp        CMakeLists.txt    libapr.dep    network_io     tablesapr.dsw        config.layout     libapr.dsp    NOTICE         testapr.mak        configure         libapr.mak    NWGNUmakefile  threadprocapr.pc.in      configure.in      libapr.rc     passwd         timeapr.spec       docs              LICENSE       poll           toolsatomic         dso               locks         random         userbuild          emacs-mode        Makefile.in   READMEbuild.conf     encoding          Makefile.win  README.cmakebuildconf      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 -antlState   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 firewalldRemoved /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; #添加这一行 }

效果

在这里插入图片描述

在这里插入图片描述

转载地址:http://ztugf.baihongyu.com/

你可能感兴趣的文章
【MachineLearning】数据挖掘中的分类和聚类的区别
查看>>
【LEETCODE】292-Nim Game
查看>>
【LEETCODE】237-Delete Node in a Linked List
查看>>
【LEETCODE】206-Reverse Linked List
查看>>
【LEETCODE】203-Remove Linked List Elements
查看>>
【LEETCODE】234-Palindrome Linked List
查看>>
【LEETCODE】141-Linked List Cycle
查看>>
【LEETCODE】142-Linked List Cycle II
查看>>
【LEETCODE】92-Reverse Linked List II
查看>>
【LEETCODE】283-Move Zeroes
查看>>
【LEETCODE】217-Contains Duplicate
查看>>
【LEETCODE】219-Contains Duplicate II
查看>>
【LEETCODE】220-Contains Duplicate III
查看>>
【LEETCODE】171-Excel Sheet Column Number
查看>>
【LEETCODE】169-Majority Element
查看>>
【LEETCODE】191-Number of 1 Bits
查看>>
【LEETCODE】13-Roman to Integer
查看>>
【LEETCODE】83-Remove Duplicates from Sorted List
查看>>
【LEETCODE】70-Climbing Stairs
查看>>
【LEETCODE】198-House Robber
查看>>