一步一步教你Nginx平滑升级和动静分离

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一步一步教你Nginx平滑升级和动静分离相关的知识,希望对你有一定的参考价值。

一、在Centos01安装nginx服务,配置网站主页,使用www.szt.com访问Nginx

1、在Centos01上安装Nginx依赖

1)挂载光盘配置本地yum仓库

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# rm -rf /etc/yum.repos.d/CentOS-*

2)安装依赖

[root@centos01 ~]# yum -y install pcre-devel zlib-devel

3)创建管理Nginx用户

[root@centos01 ~]# useradd -M -s /sbin/nologin nginx
[root@centos01 ~]# umount /mnt/

2、配置安装Nginx服务

1)切换光盘

[root@centos01 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos01 ~]# ls /mnt/

2)解压配置安装nginx

[root@centos01 ~]# tar zxf
/mnt/nginx-1.6.0.tar.gz -C /usr/src/
[root@centos01 nginx-1.6.0]# ./configure \\
> --prefix=/usr/local/nginx \\
> --user=nginx \\
> --group=nginx \\
> --with-http_stub_status_module

3)编译安装Nginx

[root@centos01 nginx-1.6.0]# make && make install

4)优化nginx命令检查配置文件

[root@centos01 ~]# ln -s
/usr/local/nginx/sbin/* /usr/local/sbin/
[root@centos01 ~]# 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

3、设置Nginx服务器网站根目录设置主页修改配置文件

1)创建网站根目录设置主页

[root@centos01 ~]# mkdir /www
[root@centos01 ~]# echo "www.stz.com" > /www/index.html

2)修改Nginx主配置文件

[root@centos01 ~]# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user
nobody;
worker_processes 1;
events
worker_connections 1024;

http
include mime.types;
default_type application/octet-stream;
worker_connections 1024;

default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location /
root /www;
index index.html index.htm;


3)启动Nginx服务监听端口号

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3740/nginx: master

二、在Centos02配置DNS服务器使用,创建stz.com域和Benet.com域分别添加主机指向Nginx主机IP地址和lamp主机IP地址

1、安装DNS服务设置开机自动启动

1)挂载系统光盘安装DNS服务

[root@centos02 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos02 ~]# ls /mnt/
CentOS_BuildTag images repodata
EFI isolinux RPM-GPG-KEY-CentOS-7
EULA LiveOS RPM-GPG-KEY-CentOS-Testing-7
GPL Packages TRANS.TBL

2)安装DNS服务

[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-9.9.4-50.el7.x86_64.rpm
[root@centos02 ~]# rpm -ivh /mnt/Packages/bind-chroot-9.9.4-50.el7.x86_64.rpm

3)设置服务开机自动启动

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

2、修改主配置文件和区域配置文件

1)修改DNS主配置文件

[root@centos02 ~]# vim /etc/named.conf
options
listen-on port 53 any; ;
directory "/var/named";
;
zone "stz.com" IN
type master;
file "/var/named/stz.com.zone";
;
zone "benet.com" IN
type master;
file "/var/named/benet.com.zone";
;

2)检查主配置文件是否错误

[root@centos02 ~]# named-checkconf /etc/named.conf

3)修改区域配置文件

[root@centos02 ~]# vim
/var/named/stz.com.zone
$TTL 86400
@ SOA stz.com. root.stz.com. (
2022021609
1H
15M
1W
1D
)
@ NS centos02.stz.com.
centos02 A 192.168.100.20
www A 192.168.100.10
[root@centos02 ~]# vim
/var/named/benet.com.zone
$TTL 86400
@ SOA benet.com. root.benet.com. (
2022021609
1H
15M
1W
1D
)
@ NS centos02.benet.com.
centos02 A 192.168.100.20
www A 192.168.100.30

4)检查区域配置文件是否错误

[root@centos02 ~]# named-checkzone stz.com /var/named/stz.com.zone
zone stz.com/IN: loaded serial 2022021609
OK
[root@centos02 ~]# named-checkzone benet.com /var/named/benet.com.zone
zone benet.com/IN: loaded serial 2022021609
OK

3、启动DNS服务查看服务运行状态

1)启动DNS服务

[root@centos02 ~]# systemctl start named
[root@centos02 ~]# systemctl enable named

2)查看服务运行状态

[root@centos02 ~]# netstat -anptu | grep named
tcp
0 0 127.0.0.1:53 0.0.0.0:* LISTEN 1449/named
tcp
0 0 127.0.0.1:953 0.0.0.0:* LISTEN 1449/named
tcp6
0 0 ::1:53 :::* LISTEN 1449/named
tcp6
0 0 ::1:953 :::* LISTEN
1449/named
udp
0 0 127.0.0.1:53 0.0.0.0:* 1449/named
udp6
0 0 ::1:53 :::* 1449/named

3)测试域名解析

[root@centos02 ~]# nslookup www.stz.com
Server: 127.0.0.1
Address: 127.0.0.1#53

Name: www.stz.com
Address: 192.168.100.10
[root@centos02 ~]# nslookup www.benet.com
Server: 127.0.0.1
Address: 127.0.0.1#53

Name: www.benet.com
Address: 192.168.100.30

三、在Centos03使用yum安装LAMP服务配置,使用域名wwwbenet.Com访问lamp,在Nginx配置动静分离,静态Nginx处理动态LAMP处理

1、配置yum安装LAMP平台

1)挂载系统光盘配置yum仓库

[root@centos03 ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos03 ~]# rm -rf /etc/yum.repos.d/CentOS-*
[root@centos03 ~]# cat /etc/yum.repos.d/local.repo
[local]
name=centos
baseurl=file:///mnt
enabled=1
gpgcheck=0

2)使用yum安装LAMP平台

[root@centos03 ~]# yum -y install httpd php php-mysql mariadb-server

3)启动服务设置开机自动启动

[root@centos03 ~]# systemctl start httpd
[root@centos03 ~]# systemctl enable httpd
[root@centos03 ~]# systemctl start mariadb
[root@centos03 ~]# systemctl enable mariadb

4)设置网站主页

[root@centos03 ~]# vim /var/www/html/index.html
www.benet.com

5)设置php测试页

[root@centos03 ~]# vim /var/www/html/index.php
<?php
phpinfo();
?>

2、访问Nginx和LAPM平台测试

1)给客户端配置ip

一步一步教你Nginx平滑升级和动静分离_Nginx

2)访问Nginx

一步一步教你Nginx平滑升级和动静分离_nginx_02

3)访问LAMP平台

一步一步教你Nginx平滑升级和动静分离_Nginx_03

3)访问LAMP的php

一步一步教你Nginx平滑升级和动静分离_Nginx_04

3、在Nginx服务器配置动静分离,静态数据图片访问Nginx动态php访问跳转到LAMP

1)修改Nginx主配置文件支持动静分离

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events
worker_connections 1024;

http
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location ~ .*\\.(gif|jpg|bmp|swf)$
root /www;
index index.html index.htm;

location ~ \\.php$
proxy_pass http://www.benet.com;


2)检查配置出错没有

[root@centos01 ~]# 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

3)重新启动nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

4)客户端访问php动态数据查看抓包信息

四、在Centos01上安装nginx实现平滑升级

1、安装老版本nginx

1)安装依赖程序创建管理nginx用户

[root@centos01 ~]# yum -y install pcre-devel zlib-devel
[root@centos01 ~]# useradd -M -s /sbin/nologin nginx

2)解压配置老版本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

3)编译安装

[root@centos01 nginx-1.12.0]# make && make install

4)优化Nginx命令

[root@centos01 nginx-1.12.0]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/

2、配置老版本nginx网站服务器

1)创建网站根目录设置主页

[root@centos01 ~]# mkdir /www
[root@centos01 ~]# echo "www.stz.com" > /www/index.html

2)修改Nginx配置文件

[root@centos01 ~]# vim /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events
worker_connections 1024;

http
include mime.types;
default_type application/octet-stream;
worker_connections 1024;

default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location /
root /www;
index index.html index.htm;


3)启动nginx服务

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 47091/nginx: master

4)客户端访问

一步一步教你Nginx平滑升级和动静分离_nginx_05

3、安装新版nginx服务器平滑升级,配置nginx监控访问监控进程基本身份验证

1)解压配置nginx

[root@centos01 ~]# ls
anaconda-ks.cfg nginx-1.16.1.tar.gz 视频 下载
initial-setup-ks.cfg 公共 图片 音乐
nginx-1.12.0.tar.gz 模板 文档 桌面
[root@centos01 ~]# tar -zxf ./nginx-1.16.1.tar.gz -C /usr/src/
[root@centos01 ~]# cd /usr/src/nginx-1.6.0/
[root@centos01 nginx-1.6.0]# ./configure \\
> --user=nginx \\
> --group=nginx \\
> --with-http_stub_status_module

2)编译安装

[root@centos01 nginx-1.6.0]# make

3)关闭老版本服务

[root@centos01 nginx-1.6.0]# killall -I -QUIT nginx

4)生成管理服务文件

[root@centos01 nginx-1.6.0]# cp objs/nginx /usr/local/sbin

5)查看nginx运行版本

[root@centos01 ~]# nginx -V
nginx version: nginx/1.6.0
built by gcc 4.8.5 20150623 (Red Hat
4.8.5-16) (GCC)
configure arguments: --user=nginx
--group=nginx --with-http_stub_status_module

6)启动新版

[root@centos01 ~]# nginx
[root@centos01 ~]# netstat -anptu | grep nginx
tcp
0 0 0.0.0.0:80 0.0.0.0:* LISTEN 49485/nginx: master

7)查看升级后的版本

一步一步教你Nginx平滑升级和动静分离_nginx_06

4、配置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 bob
New password:
Re-type new password:
Adding password for user bob

3)修改Nginx主配置文件加载验证和采集监控数据

[root@centos01 ~]# vim
/usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
events
worker_connections 1024;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server
listen 80;
server_name www.stz.com;
charset utf-8;
access_log www.stz.com.access.log;
location /
root /www;
index index.html index.htm;

location /status
stub_status on;
access_log off;
auth_basic "welcom tomauth";
auth_basic_user_file /usr/local/nginx/password;



4)重新启动nginx

[root@centos01 ~]# killall -I -QUIT nginx
[root@centos01 ~]# nginx

5)客户端访问验证

一步一步教你Nginx平滑升级和动静分离_nginx_07

6)查看监控

一步一步教你Nginx平滑升级和动静分离_centos_08

以上是关于一步一步教你Nginx平滑升级和动静分离的主要内容,如果未能解决你的问题,请参考以下文章

2.18-2.24博客精彩回顾

一步一步教你Nginx优化和防盗链

一步一步教你Nginx优化和防盗链

一步一步教你Nginx地址重写

一步一步教你nginx.conf配置

一步一步教你 https 抓包