文章目录

  • 反向代理与负载均衡
  • nginx动静分离实验
  • 在nginx主机上配置负载均衡
  • 在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 {                           // 此字段要写在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.91.134 weight=3;    // weight表示访问三次134后访问一次147server 192.168.91.137;}

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

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

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

nginx动静分离实验

实验环境说明:

主机名字 IP 安装服务 系统
httpd 192.168.91.134 Apache centos8
nginx 192.168.91.135 Nginx centos8
lnmp 192.168.91.137 LNMP centos8

安装httpd
编译安装apache查看这篇文章:LAMP

// yum 安装就可以了
#httpd
[root@httpd ~]# yum -y install httpd// 开启httpd服务
[root@httpd ~]# systemctl enable --now httpd
Created symlink /etc/systemd/system/multi-user.target.wants/httpd.service → /usr/lib/systemd/system/httpd.service.// 关闭防火墙和selinux
[root@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@httpd ~]# setenforce 0
[root@httpd ~]# vi /etc/selinux/config
SELINUX=disabled

web 页面访问

安装nginx服务

#nginx
//创建系统用户nginx
[root@nginx ~]# useradd -r -M -s /sbin/nologin nginx//安装依赖包、包组和使用工具
[root@nginx ~]# yum -y install pcre-devel pcre gcc gcc-c++ openssl-devel zlib zlib-devel make vim wget openssl openssl-devel gd-devel
安装过程略....[root@nginx ~]# yum -y groups mark install 'Development Tools'
Marked install: Development Tools//创建日志存放目录
[root@nginx ~]# mkdir -p /var/log/nginx
[root@nginx ~]# chown -R nginx.nginx /var/log/nginx//下载nginx
[root@nginx ~]# cd /usr/src/
[root@nginx src]# wget https://nginx.org/download/nginx-1.20.1.tar.gz
安装过程略....//编译安装
[root@nginx src]# ls
debug  kernels  nginx-1.20.1.tar.gz
[root@nginx src]# tar xf nginx-1.20.1.tar.gz
[root@nginx src]# cd nginx-1.20.1
[root@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@nginx nginx-1.20.1]# make && make install
安装过程略....[root@nginx nginx-1.20.1]# cd
[root@nginx ~]#// 配置环境变量
[root@nginx ~]# echo "export PATH=/usr/local/nginx/sbin:$PATH" > /etc/profile.d/nginx.sh
[root@nginx ~]# source /etc/profile.d/nginx.sh// 编写nginx启动文件
[root@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@nginx ~]# systemctl daemon-reload
[root@nginx ~]# systemctl enable --now nginx.service[root@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
```shell
[root@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@nginx ~]# setenforce 0
[root@nginx ~]# vim /etc/selinux/config
SELINUX=disabled

web页面访问

LNMP安装
nginx包下载地址
安装nginx

#lnmp
// 关闭防火墙和selinux
[root@lnmp ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@lnmp ~]# systemctl stop --now firewalld
[root@lnmp ~]# vim /etc/selinux/config
SELINUX=disabled
[root@lnmp ~]# reboot
[root@lnmp ~]# setenforce 0
setenforce: SELinux is disabled// 创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin nginx
[root@lnmp ~]# id nginx
uid=994(nginx) gid=991(nginx) groups=991(nginx)// 安装epel源、依赖包、工具包(安装lnmp所有需要的依赖包都在里面)
[root@lnmp ~]# yum -y install epel-release
[root@lnmp ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++ make  ncurses-devel cmake mariadb-devel ncurses-compat-libs  libxml2 libxml2-devel bzip2 bzip2-devel libcurl libcurl-devel libicu-devel libjpeg libjpeg-devel libpng libpng-devel openldap-devel  pcre-devel freetype freetype-devel gmp gmp-devel libmcrypt libmcrypt-devel readline readline-devel libxslt libxslt-devel mhash mhash-devel php-mysqlnd libsqlite3x-devel oniguruma libzip-devel// 将之前下载好的包传上去,并解压
[root@lnmp ~]# cd /usr/src/
[root@lnmp src]# ls
debug
kernels
mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz
nginx-1.20.1.tar.gz
php-8.0.10.tar.gz[root@lnmp src]# tar xf nginx-1.20.1.tar.gz -C /usr/local/// 创建日志存放目录
[root@lnmp src]# mkdir -p /var/log/nginx
[root@lnmp src]# chown -R nginx.nginx /var/log/nginx/// 编译安装
[root@lnmp 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 \
&& make && make install // 设置环境变量
[root@lnmp ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@lnmp ~]# . /etc/profile.d/nginx.sh
[root@lnmp ~]# nginx
[root@lnmp ~]# 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包下载链接

#lnmp
// 创建用户
[root@lnmp ~]# useradd -r -M -s /sbin/nologin mysql// 传包到当前目录然后解压mysql包
[root@lnmp ~]# tar xf /usr/src/mysql-5.7.34-linux-glibc2.12-x86_64.tar.gz -C /usr/local/// 做软链接
[root@lnmp local]# mv mysql-5.7.34-linux-glibc2.12-x86_64/ mysql
[root@lnmp local]# chown -R mysql.mysql mysql/// 头文字连接
[root@lnmp local]# ln -s /usr/local/mysql/include/ /usr/include/mysql/// 库文件
[root@lnmp local]# vim /etc/ld.so.conf.d/mysql.conf
[root@lnmp local]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib// 创建数据存放目录
[root@lnmp local]# mkdir /opt/data
[root@lnmp local]# chown -R mysql.mysql /opt/data/// 设置环境变量
[root@lnmp local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@lnmp local]# . /etc/profile.d/mysql.sh // 初始化
[root@lnmp local]# mysqld --initialize-insecure --user mysql --datadir /opt/data// 编写配置文件
[root@lnmp local]# cat > /etc/my.cnf << EOF
> [mysqld]
> basedir = /usr/local/mysql/
> datadir = /opt/data/
> socket = /tmp/mysql.sock
> port = 3306
> pid-file = /opt/data/mysql.pid
> user = mysql
> skip-name-resolve
> EOF// 编写服务控制脚本
[root@lnmp local]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=Mysql server daemon
After=network.target sshd-keygen.target[Service]
Type=forking
ExecStart=/usr/local/mysql/support-files/mysql.server start
ExecReload=/bin/kill -HUP $MAINPID
ExecStop=/usr/local/mysql/support-files/mysql.server stop[Install]
WantedBy=multi-user.target
basedir=/usr/local/mysql
datadir=/opt/data// 启动mysql服务
[root@lnmp local]# systemctl daemon-reload
[root@lnmp local]# systemctl start mysqld
[root@lnmp local]# systemctl enable --now mysqld
Created symlink /etc/systemd/system/multi-user.target.wants/mysqld.service → /usr/lib/systemd/system/mysqld.service.
[root@lnmp local]# 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      80                 *:3306              *:*
LISTEN 0      128             [::]:22             [::]:*  

安装php
php包下载地址

#lnmp
// 把下载好的包传到当前目录然后解压
[root@lnmp ~]# tar xf /usr/src/php-8.0.10.tar.gz -C /usr/local/
[root@lnmp ~]# cd /usr/local/php-8.0.10/// 编译安装
[root@lnmp php-8.0.10]# ./configure --prefix=/usr/local/php8  \
--with-config-file-path=/etc \
--enable-fpm \
--disable-debug \
--disable-rpath \
--enable-shared \
--enable-soap \
--with-openssl \
--enable-bcmath \
--with-iconv \
--with-bz2 \
--enable-calendar \
--with-curl \
--enable-exif  \
--enable-ftp \
--enable-gd \
--with-jpeg \
--with-zlib-dir \
--with-freetype \
--with-gettext \
--enable-mbstring \
--enable-pdo \
--with-mysqli=mysqlnd \
--with-pdo-mysql=mysqlnd \
--with-readline \
--enable-shmop \
--enable-simplexml \
--enable-sockets \
--with-zip \
--enable-mysqlnd-compression-support \
--with-pear \
--enable-pcntl \
--enable-posix \
&& make && make install // 设置环境变量
[root@lnmp php-8.0.10]#  echo 'export PATH=/usr/local/php8/bin:$PATH' > /etc/profile.d/php.sh
[root@lnmp php-8.0.10]# . /etc/profile.d/php.sh// 配置php
[root@lnmp php-8.0.10]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# chmod +x /etc/init.d/php-fpm
[root@lnmp php-8.0.10]# cd /usr/local/php8
[root@lnmp php8]# cd etc/
[root@lnmp etc]# cp php-fpm.conf.default  php-fpm.conf
[root@lnmp etc]# cd php-fpm.d/
[root@lnmp php-fpm.d]# cp www.conf.default www.conf
[root@lnmp php-fpm.d]# ls
www.conf  www.conf.default// 启动服务
[root@lnmp php-fpm.d]# service php-fpm start
Starting php-fpm  done
[root@lnmp php-fpm.d]# ss -antl
State  Recv-Q Send-Q Local Address:Port   Peer Address:Port Process
LISTEN 0      128        127.0.0.1:9000        0.0.0.0:*
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      80                 *:3306              *:*
LISTEN 0      128             [::]:22             [::]:*

nginx配置

// 配置网页文件
[root@lnmp ~]# cd /usr/local/nginx/html/
[root@lnmp html]# vim index.php
[root@lnmp html]# cat index.php
<?phpphpinfo();
?>
[root@lnmp html]# chown -R nginx.nginx index.php //修改配置文件
[root@lnmp nginx]# vim conf/nginx.conf
server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index index.php index.html index.htm;}location ~ \.php$ {root           html;fastcgi_pass   127.0.0.1:9000;fastcgi_index  index.php;fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;include        fastcgi_params;}// 重新加载nginx
[root@lnmp nginx]# nginx -s reload

web 页面访问

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

在nginx主机上配置负载均衡

#nginx
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confupstream test {                  // 此字段要在server字段前面,之前提到过的server 192.168.91.134;server 192.168.91.137;}server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {proxy_pass http://test;   // test要与上面的upstream后面所跟的一样}// 检查语法
[root@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@nginx ~]# nginx -s reload

web页面访问192.168.91.135

在nginx主机上配置动静分离

#nginx
[root@nginx ~]# vim /usr/local/nginx/conf/nginx.confupstream test1 {server 192.168.91.134;}upstream test2 {server 192.168.91.137;}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@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@nginx ~]# nginx -s reload

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

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

Nginx负载均衡与动静分离相关推荐

  1. 使用Nginx负载均衡及动静分离

    使用Nginx负载均衡及动静分离 目录 使用Nginx负载均衡及动静分离 一.系统拓扑图 二.环境准备 三.服务器安装 1.jdk,tomcat,mysql 2.Nginx的安装 2.1 gcc安装 ...

  2. [Nginx]负载均衡和动静分离

    负载均衡 客户端发送多个请求到服务器,服务器处理请求,有一些可能要与数据库进行交互,服务器处理完毕后,再将结果返回给客户端. 这种架构模式对于早期的系统相对单一,并发请求相对较少的情况下是比较适合的, ...

  3. Nginx+Tomcat负载均衡、动静分离

    目录 一:Nginx实现负载均衡原理 二:Nginx动静分离原理 三:Nginx+Tomcat负载均衡.动静分离实验 3.1部署Nginx负载均衡器 3.1.1关闭防火墙,将安装nginx所需的软件包 ...

  4. Nginx+Tomcat实现负载均衡与动静分离

    Nginx+Tomcat实现负载均衡与动静分离 一.Nginx负载均衡和动静分离 1.Nginx 实现负载均衡是通过反向代理实现 反向代理(Reverse Proxy) 是指以 代理服务器(例:Ngi ...

  5. Nginx之反向代理与负载均衡实现动静分离实战

    Nginx之反向代理与负载均衡实现动静分离实战 什么是反向代理与负载均衡 Nginx仅仅作为Nginx  proxy反向代理使用的,因为这个反向代理功能表现的效果是负载均衡集群的效果. 负载均衡指的是 ...

  6. Nginx入门教程-简介、安装、反向代理、负载均衡、动静分离使用实例

    场景 Nginx入门简介和反向代理.负载均衡.动静分离理解 https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102790862 Ub ...

  7. 使用nginx+Apache负载均衡及动静分离

    使用nginx+Apache负载均衡及动静分离 介绍    LB负载均衡集群分两类: LVS (四层)和 nginx或haproxy (七层)    客户端都是通过访问分发器的VIP来访问网站 在七层 ...

  8. Nginx总结(反向代理、负载均衡、动静分离)篇

    一.Nginx简介 什么是Nginx Nginx ("engine x")是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能力强,事实上ngimx,的并发能力确实在同 ...

  9. Docker的安装和镜像管理并利用Docker容器实现nginx的负载均衡、动静分离

    Docker的安装 一.Docker的概念 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化 ...

最新文章

  1. flask源码学习-helloworld与本地启动流程
  2. 贝叶斯定理的实际应用
  3. Linux服务器上最简单的Nginx反向代理配置
  4. linux ubuntu 获取仓库源码并构建
  5. asp.net用Zxing库实现条形码输出
  6. ES6函数第二篇:剩余参数与展开运算符的练习
  7. Spring Boot中带有CKEditor的AJAX
  8. 关于国内厂商的国际版杀毒软件
  9. Android官方开发文档Training系列课程中文版:动画视图之应用场景
  10. 推荐几个好的域名交易站点
  11. 阿里平头哥研发专用 SoC 芯片;部分 MacBook Pro 被禁止上飞机;VS Code 1.37 发布 | 极客头条...
  12. Rob Papen Quad for Mac - 合成器插件
  13. HtmlDocument.ExecCommand() 方法
  14. python清空屏幕代码_python 清屏
  15. 粉红噪音测试软件,煲耳机方法二:粉红噪音
  16. android 中文 拼音首字母,拼音首字母翻译成中文app
  17. Onlyoffice安装步骤
  18. 组织的目的是使平凡的人做出不平凡的事 --- 彼得.德鲁克 《卓有成效的管理者》
  19. Linux学习:用户和用户组管理(大量用户创建)
  20. 分享7个实用的电脑软件,满满的干货,大家低调收藏

热门文章

  1. 在线计算机励志文案,励志简短文案 正能量励志文案
  2. 《OCCLUSION-AWARE GAN FOR FACE DE-OCCLUSION IN THE WILD 》
  3. qq离开状态经典语句
  4. Spring实战(第4版)
  5. kali linux 安装qq 详细操作
  6. @JSONField注解的使用
  7. java时间格式大全_java时间格式大全
  8. edge浏览器使用ie兼容模式以及不安全的TLS设置
  9. python 保存xls文件
  10. 洗牌算法(Fisher–Yates高纳德置乱算法)