nginx正则表达式和动静分离
一、nginx简介
Nginx (“engine x”)是一个高性能的HTTP和反向代理服务器,特点是占有内存少,并发能力强,事实上ngimx,的并发能力确实在同类型的网页服务器中表现较好
Nginx专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率﹐能经受高负载的考验,有报告表明能支持高达50, 000个并发连接数。
Nginx作用:反向代理、负载均衡、动静分离等
二、什么是反向代理
先了解一下正向代理 :通过在客户端配置代理服务器,通过代理服务器进行互联网访问!

反向代理
反向代理,其实客户端对代理是无感知的,因为客户端不需要任何配置就可以访问,我们只需要将请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据后,在返回给客户端,此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器IP地址。

什么是负载均衡
单个服务器解决不了,我们增加服务器的数量,然后将请求分发到各个服务器上,由反向代理服务器将原先请求集中到单个服务器上的情况改为将请求分发到多个服务器上,将负载分发到不同的服务器,也就是我们所说的负载均衡

什么是动静分离
在我们的软件开发中,有些请求是需要后台处理的,有些请求是不需要经过后台处理的(如:css、html、jpg、js等等文件),这些不需要经过后台处理的文件称为静态文件。让动态网站里的动态网页根据一定规则把不变的资源和经常变的资源区分开来,动静资源做好了拆分以后,我们就可以根据静态资源的特点将其做缓存操作。提高资源响应的速度。

三、配置Nginx和动静分离和平滑升级
拓扑图:

推荐步骤:
1、在Centos01安装Nginx服务,使用www.huhu.com访问Nginx
2、在Nginx服务器配置动静分离静,静态数据图片访问Nginx动态php访问跳转到LAMP
3、在Centos01安装新版Nginx实现平滑升级,配置Nginx监控访问监控进行基本身份验证
实验步骤:
一、在Centos01安装Nginx服务,使用www.huhu.com访问Nginx
1、传输文件
[root@centos01 ~]# rz
z waiting to receive.**B0100000023be50
[root@centos01 ~]# rz
z waiting to receive.**B0100000023be50
[root@centos01 ~]# ls
anaconda-ks.cfg nginx-1.12.0.tar.gz 公共 视频 文档 音乐
initial-setup-ks.cfg nginx-1.16.1.tar.gz 模板 图片 下载 桌面
2、挂载Centos7系统光盘
[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/
CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7
EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL
3、安装依赖程序
[root@centos01 ~]# yum -y install pcre-devel zlib-devel
4、创建管理Nginx用户
[root@centos01 ~]# useradd -M -s /sbin/nologin nginx
5、配置nginx
[root@centos01 ~]# tar zxf ./nginx-1.12.0.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.12.0/
[root@centos01 nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
6、编译安装Nginx
[root@centos01 nginx-1.12.0]# make && make install
7、优化命令
[root@centos01 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 nginx-1.12.0]# cd
8、修改hosts文件
[root@centos01 ~]# vim /etc/hosts

9、复制hosts文件到100.20
[root@centos01 ~]# scp /etc/hosts root@192.168.100.20:/etc/
10、创建nginx网站根目录,设置网站主页
[root@centos01 ~]# mkdir /www
[root@centos01 ~]# echo "www.bdqn.com" > /www/index.html
11、客户端访问nginx

二、在Nginx服务器配置动静分离静,静态数据图片访问Nginx动态php访问跳转到LAMP
1、安装LAMP平台
[root@centos02 ~]# yum -y install httpd php php-mysql mariadb-server
2、启动服务
[root@centos02 ~]# systemctl start httpd
[root@centos02 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@centos02 ~]# systemctl start mariadb
[root@centos02 ~]# systemctl enable mariadb
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
3、设置apache网站主页
[root@centos02 ~]# echo "www.huyan.com" > /var/www/html/index.html
4、配置php测试页
[root@centos02 ~]# vim /var/www/html/index.php

5、修改Nginx主配置文件
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.huhu.com.access.log;
location / {
root /www/;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|bmp|swf)$ {
root /www/;
index index.html index.htm;
}
location ~ \.php$ {
proxy_pass http://www.huyan.com;
}
}
}
6、重新启动Nginx服务
[root@centos01 ~]# killall nginx
nginx: no process found
[root@centos01 ~]# nginx
7、验证静态数据图片访问Nginx动态php访问跳转到LAMP


三、在Centos01安装新版Nginx实现平滑升级,配置Nginx监控访问监控进行基本身份验证
1、安装验证数据库程序
[root@centos01 ~]# rpm -ivh /mnt/Packages/httpd-tools-2.4.6-67.el7.centos.x86_64.rpm
2、生成验证数据库
[root@centos01 ~]# htpasswd -c /usr/local/nginx/password admin
New password:
Re-type new password:
Adding password for user admin
3、修改Nginx配置文件支持监控
user nginx;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log logs/access.log;
sendfile on;
keepalive_timeout 65;
server {
listen 192.168.100.10;
server_name www.bdqn.com;
charset utf-8;
access_log logs/www.huhu.com.access.log;
location / {
root /www/;
index index.html index.htm;
}
location ~ .*\.(gif|jpg|jpeg|bmp|swf)$ {
root /www/;
index index.html index.htm;
}
location ~ \.php$ {
proxy_pass http://www.huyan.com;
}
location /status {
stub_status on;
access_log off;
auth_basic "welcom Auth";
auth_basic_user_file /usr/local/nginx/password;
}
}
}
4、重启Nginx服务
[root@centos01 ~]# killall nginx
nginx: no process found
[root@centos01 ~]# nginx
5、配置Nginx平滑升级 停止老版本nginx服务
[root@centos01 ~]# killall nginx
nginx: no process found
6、解压新版本Nginx
[root@centos01 ~]# tar zxf ./nginx-1.16.1.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.16.1/
7、配置新版本Nginx
[root@centos01 nginx-1.16.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
8、编译Nginx
[root@centos01 nginx-1.16.1]# make
9、备份老版本进程
[root@centos01 ~]# mv /usr/local/sbin/nginx /usr/local/sbin/nginx.bak
10、生成新版本服务
[root@centos01 ~]# ln -s /usr/src/nginx-1.16.1/objs/nginx /usr/local/sbin/
11、启动服务
12、验证Nginx监控访问监控进行基本身份验证


13、验证新版Nginx实现平滑升级

Created By Henry 共同学习 共同进步. 点赞收藏加关注.