[关闭]
@xushengkai 2022-12-27T05:33:37.000000Z 字数 23511 阅读 2476

Nginx WEB架构实战篇--LNMP

nginx


[TOC]

一--动态网站架构

资源
资源文件识别--语言识别--框架识别
index.php--开源的php--Windows/Linux+nginx+php+mysql
index.py--开源的python--Windows/Linux+apache+python+mysql
index.jsp--商业JAVA--Windows/Linux+tomact+JDK+Oracle
index.asp--商业c#--Windows+iis+asp.net+sql-server/oracle/mogodb

二--LNMP架构原理

LNMP架构,是指在Linux平台下,由运行Nginx的web服务器,运行PHP的动态页面解析程序和运行MySQL的数据库组成的网站架构,也是当前常用的系统架构之一。
在LNMP架构中,Nginx本身只负责静态页面的处理,当需要处理动态页面时,则需要Nginx将相关.php页面转交给php-fpm来进行处理,php-fpm会将PHP页面解析成html文件,然后交给Nginx进行处理。
与LAMP架构相比,LNMP的主要区别在于对PHP的处理上,LAMP对于PHP动态资源的处理是通过Apache的libphp5.so模块进行的,该模块内嵌如Apache中,而Nginx对PHP动态资源的处理则是通过php-fpm进行的,php-fpm是一个独立的模块,因此,在搭建LNMP架构时,Nginx和php-fpm都需要进行开启。LNMP架构如下所示:
此处输入图片的描述

三--LNMP动态网站环境部署

Linux部署

打开一台全新的虚拟机,配置好阿里云yum仓库,关闭防火墙和selinux

  1. [root@192 ~]# ls /etc/yum.repos.d/
  2. CentOS-Base.repo epel.repo
  3. [root@192 ~]# systemctl stop firewalld
  4. [root@192 ~]# sestatus
  5. SELinux status: disabled

Nginx部署

  1. #安装nginx
  2. [root@192 ~]# yum -y install nginx
  3. #启动nginx,查看进程,或者打开主机浏览器访问nginx页面测试。
  4. [root@192 ~]# systemctl start nginx
  5. [root@192 ~]# ps -ef | grep nginx
  6. root 1355 1 0 17:33 ? 00:00:00 nginx: master process /usr/sbin/nginx
  7. nginx 1356 1355 0 17:33 ? 00:00:00 nginx: worker process
  8. nginx 1357 1355 0 17:33 ? 00:00:00 nginx: worker process
  9. root 1388 1232 0 17:35 pts/0 00:00:00 grep --color=auto nginx

php-fpm部署

安装php-fpm

  1. #php-fpm:,连接nginx,php接收动态请求的程序
  2. #php-mysql:php连接mysql的程序
  3. #php-gd:图形库程序(GD库可以处理图片,或者生成图片)
  4. [root@192 ~]# yum -y install php-fpm php-mysql php-gd
  5. #启动php-fpm,设置开机自启动
  6. [root@192 ~]# systemctl start php-fpm
  7. [root@192 ~]# systemctl enable php-fpm
  8. Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.
  9. #查看php-fpm端口号,默认监听9000端口
  10. [root@192 ~]# ss -anp | grep 9000
  11. tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=1506,fd=0),("php-fpm",pid=1505,fd=0),("php-fpm",pid=1504,fd=0),("php-fpm",pid=1503,fd=0),("php-fpm",pid=1502,fd=0),("php-fpm",pid=1500,fd=6))

测试php程序

  1. #设置php程序,创建index.php文件添加以下内容
  2. [root@192 ~]# vim /usr/share/nginx/html/index.php
  3. <?php #调用php版本信息
  4. phpinfo();
  5. ?>
  6. #在nginx的主配置文件中设置默认访问页面
  7. [root@192 ~]# vim /etc/nginx/nginx.conf
  8. 38 server {
  9. 39 #在此行下面添加以下内容
  10. 40 location ~ \.php$ {
  11. 41 root /usr/share/nginx/html;
  12. 42 fastcgi_pass 127.0.0.1:9000;
  13. 43 fastcgi_index index.php;
  14. 44 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  15. 45 include fastcgi_params;
  16. 46 }
  17. 53 include /etc/nginx/default.d/*.conf;
  18. #在此行的下面添加以下内容
  19. 54 location / {
  20. 55 index index.php index.html;
  21. 56 }
  22. 重启nginx
  23. [root@192 ~]# systemctl restart nginx

打开主机浏览器访问web页面,显示内容PHP Version 5.4.16设置成功

mysql部署

1.RPM部署

  1. #安装mysql服务器程序和客户机程序
  2. [root@192 ~]# yum -y install mariadb-server mariadb
  3. #启动mysql,设置开机自启动
  4. [root@192 ~]# systemctl start mariadb
  5. [root@192 ~]# systemctl enable mariadb
  6. Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
  7. #为MySQL数据库设置密码
  8. [root@192 ~]# mysqladmin password '123456'
  9. #创建空库
  10. MariaDB [(none)]> create database bbs;
  11. Query OK, 1 row affected (0.00 sec)
  12. #给用户phptest授权
  13. MariaDB [(none)]> grant all on bbs.* to phptest@'192.168.200.%' identified by '123456';
  14. Query OK, 0 rows affected (0.00 sec)
  15. MariaDB [(none)]> flush privileges;
  16. Query OK, 0 rows affected (0.00 sec)
  17. #利用php语言登录数据库,查看数据库能否使用
  18. [root@192 ~]# vim /usr/share/nginx/html/index.php
  19. <?php
  20. $link=mysql_connect('192.168.200.147','phptest','123456'); #mysql在哪台服务器上就写哪台服务器的IP
  21. if($link)
  22. echo "xushengkai Successfuly";
  23. else
  24. echo "xushengkai Faile";
  25. mysql_close();
  26. ?>

打开主机浏览器访问web页面,多次刷新页面,显示内容为:xushengkai Successfuly,lnmp准备工作已经完成

业务上线

购买服务器


购买域名


上传app

  1. [root@192 ~]# wget http://cn.wordpress.org/wordpress-4.9.1-zh_CN.zip
  2. [root@192 ~]# yum -y install unzip
  3. [root@192 ~]# unzip wordpress-4.9.1-zh_CN.zip
  4. [root@192 ~]# rm -rf /usr/share/nginx/html/index.php
  5. [root@192 ~]# cp -rf /root/wordpress/* /usr/share/nginx/html/
  6. [root@192 ~]# chown -R nginx.nginx /usr/share/nginx/html/*
  7. [root@192 ~]# chmod 777 /usr/share/nginx/html/

打开主机浏览器访问web页面,页面显示:欢迎使用WordPress。在开始前,我们需要您数据库的一些信息。请准备好如下信息。
输入数据库名:bbs
用户名:phptest
密码:自己设置
数据库主机:数据库在哪个服务器就写哪个服务器的IP
表前缀:wq_

四--PHP开发深入了解

自学网站:https://www.w3school.com.cn/


工作流程
UI:负责构图
前端:通过表单,文本框,提交按钮,页面布局
后端:php连接函数
DBA:实现后台数据库的写入
OP:业务上线

准备前台html页面

前端

  1. #删除默认页面
  2. [root@192 ~]# cd /usr/share/nginx/html/
  3. [root@192 html]# rm -rf *
  4. #准备需要设置的页面
  5. [root@192 html]# vim index.html
  6. <html>
  7. <body>
  8. <img src="logo.jpg" width="600" height="600" />
  9. <form action="insert.php" method="post">
  10. Firstname: <input type="text" name="firstname" />
  11. Lastname: <input type="text" name="lastname" />
  12. Age: <input type="text" name="age" />
  13. <input type="submit" />
  14. </form>
  15. </body>
  16. </html>

UI

  1. #拷贝图片到网页主目录下
  2. [root@192 html]# rz
  3. [root@192 html]# ls
  4. 1.html logo.jpg

准备php中间件

后端

  1. [root@192 html]# vim insert.php
  2. <?php
  3. $con = mysql_connect("192.168.200.147","root","123456");
  4. if (!$con)
  5. {
  6. die('Could not connect: ' . mysql_error());
  7. }
  8. mysql_select_db("my_db", $con);
  9. $sql="INSERT INTO Persons (FirstName, LastName, Age)
  10. VALUES
  11. ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
  12. if (!mysql_query($sql,$con))
  13. {
  14. die('Error: ' . mysql_error());
  15. }
  16. echo "1 record added";
  17. mysql_close($con)
  18. ?>

准备表和库

DBA

  1. [root@192 html]# mysql -p'123456'
  2. Welcome to the MariaDB monitor. Commands end with ; or \g.
  3. Your MariaDB connection id is 2
  4. Server version: 5.5.68-MariaDB MariaDB Server
  5. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  6. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  7. MariaDB [(none)]> create database my_db;
  8. Query OK, 1 row affected (0.00 sec)
  9. MariaDB [(none)]> use my_db;
  10. Database changed
  11. MariaDB [my_db]> create table Persons (FirstName varchar(50), LastName varchar(50),Age int );
  12. Query OK, 0 rows affected (0.00 sec)
  13. MariaDB [my_db]> grant all on *.* to root@'192.168.200.147' identifi by '123456';
  14. Query OK, 0 rows affected (0.01 sec)
  15. MariaDB [my_db]> grant all on *.* to root@'%' identified by '123456';
  16. Query OK, 0 rows affected (0.00 sec)
  17. MariaDB [my_db]> flush privileges;
  18. Query OK, 0 rows affected (0.00 sec)

输入页面

打开主机浏览器访问页面,写入自己的信息提交,下图仅供参考
转载的原图片链接:https://blog.csdn.net/Lancelotest/article/details/108834468
此处输入图片的描述


跳转页面会显示下图的内容

此处输入图片的描述

输出页面

  1. [root@192 html]# vim select.php
  2. {
  3. <?php
  4. $con = mysql_connect("localhost","root","123456");
  5. if (!$con)
  6. {
  7. die('Could not connect: ' . mysql_error());
  8. }
  9. mysql_select_db("my_db", $con);
  10. $result = mysql_query("SELECT * FROM Persons");
  11. echo "<table border='1'>
  12. <tr>
  13. <th>Firstname</th>
  14. <th>Lastname</th>
  15. </tr>";
  16. while($row = mysql_fetch_array($result))
  17. {
  18. echo "<tr>";
  19. echo "<td>" . $row['FirstName'] . "</td>";
  20. echo "<td>" . $row['LastName'] . "</td>";
  21. echo "</tr>";
  22. }
  23. echo "</table>";
  24. mysql_close($con);
  25. ?>

前台输出界面

打开主机访问页面目录的select.php,下图仅供参考
此处输入图片的描述

后台输出界面

此处输入图片的描述

五--fastcgi和php-fpm

1.静态网站

nginx服务器能处理的是静态元素:.html .jpg .mp4 .css


2.nginx_fastcgi_module

处理动态请求的接口
nginx通过ngx_fastcgi_module模块,链接php-fpm处理动态请求


3.PHP-FPM

PHP-FPM(PHP FastCGI Process Manager)意:PHP FastCGI 进程管理器,用于管理PHP进程池的软件,用于接受web服务器的请求。比如想后端MySQL进行查询请求后,将铲鲟结果返回给前台nginx


4.PHP-MYSQL

是php连接MySQL的接口程序


5.MySQL

存储数据


  • 重点:
  • 什么是fastcgi
  • nginx+fastchi运行原理
  • LNMP的运行原理

六--php-fpm初始化配置

php-fpmd的配置文件

1.核心配置文件

  1. [root@192 ~]# vim /etc/php.ini
  2. date.timezone = PRC #设置PHP的时区
  3. open_basedir #设置PHP脚本允许访问的目录

2.全局配置文件

  1. [root@192 ~]# vim /etc/php-fpm.conf
  2. pid = /run/php-fpm/php-fpm.pid #pid存放的位置
  3. error_log = /var/log/php-fpm/error.log #日志文件的位置
  4. log_level = notice #日志的级别
  5. process.max = 3 #默认没有,控制子进程最大数的全局变量,后边的设置子进程数量的指令受到这个值的限制,0表示无限制
  6. daemonize = yes #将fpm转至后台运行

3.扩展配置文件

  1. [root@192 ~]# vim /etc/php-fpm.d/www.conf
  2. user = nginx #设置用户和用户组
  3. listen.allowed_clients = 127.0.0.1 #允许访问FastCGI进程的IP,设置any为不限制IP,如果要设置其他主机的nginx也能访问这台FPM进程,listen处要设置成本地可被访问的IP。默认值是any。每个地址是用逗号分隔,如果没有设置或者为空,则允许任何服务器请求连接
  4. listen = 127.0.0.1:9000 #fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: 'ip:port',如果是分离部署,nginx在哪个服务器就写哪个服务器的IP
  5. slowlog = /var/log/php-fpm/$pool-slow.log #开启慢日志
  6. pm=dynamic #动态模式进程管理开启
  7. pm.start_servers=5 #最初开启多少进程
  8. pm.min_spare_server =5 #最小的多余进程数。最少空闲。用户访问会消耗掉进程。然后为了满足后续用户随时随地开启进程保持空闲数为5。
  9. pm.max_children = 50 #最大进程数 #max_children是PHP-FPM Pool 最大的子进程数,他数值取决于你的服务器内存。 假设你打算给10G内存给当前配置的PHP-FPM Pool,一般一个PHP请求占用内存10M-40M,我们按站点每个PHP请求占用内存25M,这样max_children = 10G/25M = 409。所以,这个值可以根据情况算出来
  10. pm.max_spare_servers=10 #最大的多余进程。大规模断开后,高并发访问过后,还剩多少,空闲进程数超过10个的时候,杀掉只剩10个
  11. pm.max_requests = 500 #每个子进程能响应的请求数量,到达此数字,该PHP进程就被释放掉了。
  12. #max_requests是每个子进程重生之前处理的请求数, 默认值为unlimited(默认为1024),可以设置小一点(如500左右),这样可以避免内存泄露带来的问题

初始化php-fpm

1.初始化之前php程序的进程数是5个

  1. [root@192 ~]# ps aux | grep php
  2. root 944 0.0 0.8 351920 10932 ? Ss 09:34 0:00 php-fpm: master process (/etc/php-fpm.conf)
  3. apache 999 0.0 0.3 351920 4824 ? S 09:34 0:00 php-fpm: pool www
  4. apache 1001 0.0 0.3 351920 4824 ? S 09:34 0:00 php-fpm: pool www
  5. apache 1002 0.0 0.3 351920 4824 ? S 09:34 0:00 php-fpm: pool www
  6. apache 1003 0.0 0.3 351920 4824 ? S 09:34 0:00 php-fpm: pool www
  7. apache 1004 0.0 0.3 351920 4824 ? S 09:34 0:00 php-fpm: pool www
  8. root 2070 0.0 0.0 112724 984 pts/0 S+ 10:25 0:00 grep --color=auto php

2.设置生产环境下常用数值

  1. [root@192 ~]# vim /etc/php-fpm.d/www.conf
  2. pm = dynamic #启动动态管理模式
  3. pm.start_servers = 32 #初始启动32个进程
  4. pm.max_children = 512 #最大进程数(子进程会在最大和最小范围中变化)512个进程数是在大于16G内存的前提下。
  5. pm.min_spare_servers = 32 #随着用户访问的增加,保持32个空闲进程。
  6. pm.max_spare_servers = 64 #随着用户离去。杀死大量空闲进程来节约资源。
  7. pm.max_requests = 1500 #是每个子进程重生之前处理的请求数,默认值为unlimited(为1024)
  8. #重启php
  9. [root@192 ~]# systemctl restart php-fpm

3.初始化以后

  1. #一共32个进程,多两个一个是父进程,一个是grep。
  2. [root@192 ~]# ps aux | grep php | wc -l
  3. 34

启动php动态监控页面

1.启动测试页面功能

  1. [root@192 ~]# vim /etc/php-fpm.d/www.conf
  2. #这里写什么就访问什么,把前面的分号去掉,在php语言中,分号是注释的意思。
  3. pm.status_path = /php_status

2.nginx配置页面转发

  1. [root@192 ~]# vim /etc/nginx/nginx.conf
  2. #在server{ 下面写上以下内容
  3. server {
  4. location = /php_status {
  5. fastcgi_pass 127.0.0.1:9000;
  6. fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  7. include fastcgi_params;
  8. }
  9. #这个配置的意思是 在浏览器中访问的.php文件,实际读取的是 fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
  10. $document_root(网站根目录)下的.php文件 -- 也就是说 当访127.0.0.1/index.php的时候,需要读取网站根目录下面的index.php文件,如果没有配置这一配置项时,nginx不回去网站根目录下访问.php文件,所以返回空白
  11. #include fastcgi_params是常用变量所在的文件名。
  12. #重启nginx和php
  13. [root@192 ~]# systemctl restart nginx
  14. [root@192 ~]# systemctl restart php-fpm

3.访问测试页面

打开浏览器访问/php_status

  1. pool: www #池子名称,大多数为www
  2. process manager: dynamic #进程管理方式,值:static, dynamic or ondemand. dynamic
  3. start time: 11/Dec/2022:11:00:53 +0800 #启动日期,如果reload了php-fpm,时间会更新
  4. start since: 4 # 运行时长
  5. accepted conn: 1 #当前池子接受的请求数
  6. listen queue: 0 #请求等待队列,如果这个值不为0,那么要增加FPM的进程数量
  7. max listen queue: 0 #请求等待队列最高的数量
  8. listen queue len: 128 #socket等待队列长度
  9. idle processes: 31 #空闲进程数量
  10. active processes: 1 #活跃进程数量
  11. total processes: 32 #总进程数量
  12. max active processes: 1 #最大的活跃进程数量(FPM启动开始算)
  13. max children reached: 0 #进程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量太小了,请改大一点。
  14. slow requests: 0 #启用了php-fpm slow-log,缓慢请求的数量

七--Nginx-location

前言

通常网站的部分页面,需要特殊设置。
比如,/1.html 页面,需要用户访问控制(如allow all)
location = /1.html {
allow all;
}
那部分页面该如何表达呢?
答案:就是位置 Location URL { module }. 其中URL的表达方式中使用的正则表达式,常会有冲突的情况,请通过下面的实验,了解常见的冲突符号,并掌握其中的优先级。

语法

  1. location [=|~|~*|!~|!~*|^~] /uri/ {
  2. module;
  3. module;
  4. }
  • = 表示精确匹配,优先级也是最高的
  • ~ 区分大小写的正则匹配
  • ~* 不区分大小写的正则匹配
  • / 通用匹配,任何请求都会匹配到
  • ^~ 以某些字符串开头
  • !~ 非(区分大小写匹配的正则)
  • !~* 非(不区分大小写匹配的正则)

优先级

= 优先于 ^~ 优先于 ~| ~* | !~ | !~* 优先于 /
精确匹配>字符开头>正则匹配>通配

八--Nginx rewrite

nginx rewrite又叫nginx URL重写

什么是nginx URL重写

1.什么是rewrite

  • URL Rewrite最常见的应用是URL伪静态化,是将动态页面显示为静态页面方式的一种技术。比如http://www.123.com/news/index.php?id=123 使用URLRewrite 转换后可以显示为 http://www.123
    .com/news/123.html对于追求完美主义的网站设计师,就算是网页的地址也希望看起来尽量简洁明快。
    理论上,搜索引擎更喜欢静态页面形式的网页,搜索引擎对静态页面的评分一般要高于动态页面。所
    以,UrlRewrite可以让我们网站的网页更容易被搜索引擎所收录。
  • 从安全角度上讲,如果在URL中暴露太多的参数,无疑会造成一定量的信息泄漏,可能会被一些黑客
    利用,对你的系统造成一定的破坏,所以静态化的URL地址可以给我们带来更高的安全性。
  • 实现网站地址跳转,例如用户访问360buy.com,将其跳转到jd.com。

rewrite相关指令

重定向

  1. rewrite
  2. 将用户的访问(url),更换成指定的文件。

if 语句

  1. 应用位置:serverlocation
  2. 语法:if (condition) { }

条件判断

符号 含义
~* 正则匹配 (不区分大小写)
!~ 非正则匹配 (区分大小写)
!~* 非正则 匹配 (不区分大小写)
-f 和!-f 用来判断是否存在文件
-d 和!-d 用来判断是否存在目录
-e 和!-e 用来判断是否存在文件或目录
-x 和!-x 用来判断文件是否可执行

全局变量

变量 含义
$document_root 针对当前请求的根路径设置值
$remote_addr 客户端地址
$request_filename 当前请求的文件路径名(带网站的主目录/usr/local/nginx/html/images/a.jpg)
$request_uri 当前请求的文件路径名(不带网站的主目录/images/a.jpg)
$scheme 用的协议,比如http或者是https
$server_name 请求到达的服务器名
$args 请求中的参数
$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$limit_rate 对连接速率的限制
$request_method 请求的方法,比如"GET"、"POST"等
$remote_port 客户端端口号
$remote_user 客户端用户名,认证用
$query_string 与$args相同
$server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"
$server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)
$document_uri $uri一样,URI地址
$server_port 请求到达的服务器端口号

rewrite flag

每行rewrite指令最后跟一个flag标记,支持的flag标记有:

  1. last #停止处理当前ngx_http_rewrite_module,从指令之后的一个新的位置改变URI匹配搜索;
  2. break #本条规则匹配完成后,终止匹配,不再匹配后面的规则
  3. redirect #返回302临时重定向,浏览器地址会显示跳转后的URL地址
  4. redirect url permanent #返回301永久重定向,浏览器地址会显示跳转后URL地址

redirect 和 permanent区别则是返回的不同方式的重定向,对于客户端来说一般状态下是没有区别的。而对于搜索引擎,相对来说301的重定向更加友好
如果我们把一个地址采用301跳转方式跳转的话,搜索引擎会把老地址的相关信息带到新地址,同时在搜索引擎索引库中彻底废弃掉原先的老地址。

使用302重定向时,搜索引擎(特别是google)有时会查看跳转前后哪个网址更直观,然后决定显示哪个,如果它觉的跳转前的URL更好的话,也许地址栏不会更改,那么很有可能出现URL劫持的现像。

rewrite匹配参考示例

案例1

目的:站内跳转,当用户访问http://192.168.100.10/abc/a/1.html 地址时,
通过redirect 重定向至http://192.168.100.10/ccc/bbb/2.html
注意:
192.168.100.10/abc/a/1.html是否存在已经不重要了
192.168.100.10/ccc/bbb/2.html页面必须存在

1.还还原默认站点,把之前实验的环境都还原

  1. #卸载nginx,这样快一点
  2. [root@192 ~]# yum remove -y nginx
  3. #重新安装nginx
  4. [root@192 ~]# yum -y install nginx
  5. #清除网页主目录
  6. [root@192 ~]# rm -rf /usr/share/nginx/html/*
  7. #只设置一个主页文件
  8. [root@192 ~]# vim /usr/share/nginx/html/index.html
  9. 写上下行内容,可以随便写
  10. /usr/share/nginx/html/index.html
  11. #重启nginx
  12. [root@192 ~]# systemctl restart nginx
  13. #访问页面,设置默认主页成功
  14. [root@192 ~]# curl 192.168.200.147
  15. /usr/share/nginx/html/index.html

2.配置地址重写

  1. #创建abc/a/1.html和ccc/bbb/2.html并添加内容
  2. [root@192 ~]# cd /usr/share/nginx/html/
  3. [root@192 html]# mkdir -p abc/a
  4. [root@192 html]# echo "abc/a/1.html" > abc/a/1.html
  5. [root@192 html]# mkdir -p ccc/bbb
  6. [root@192 html]# echo "ccc/bbb/2.html" >ccc/bbb/2.html
  7. [root@192 html]# cat abc/a/1.html ccc/bbb/2.html
  8. abc/a/1.html
  9. ccc/bbb/2.html

2.在nginx配置文件里配置rewrite

  1. [root@192 ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. #找到server在下面写上以下内容,固定格式。
  4. location /abc {
  5. rewrite .* /ccc/bbb/2.html permanent; #匹配请求中的“abc”字段将请求中的url换成新的url
  6. }
  7. #重启nginx
  8. [root@192 ~]# systemctl restart nginx

3.访问旧页面进行测试,就是访问/abc/a/1.html

  1. [root@192 ~]# curl 192.168.200.147/abc/a/1.html
  2. ccc/bbb/2.html
  3. #访问的是1.html,显示的是2.html重定向成功,实验完毕

4.关于permanent

permanent会将地址显示为新的URL地址(也就是重定向之后的URL),一般都要添加


不添加permanent的结果,URL还是老的,服务器内部转换请求,服务器内部转换URL,内部转换URL
此处输入图片的描述


添加permanent的结果,URL被替换,生成两次请求,服务器只转换了URL,客户端重新申请
此处输入图片的描述

5.请思考表达式问题1

加个=,精确匹配

  1. [root@192 ~]# vim /etc/nginx/nginx.conf
  2. location = /abc { #加个=,精确匹配
  3. #访问结果,不会被重定向,不可行
  4. [root@192 ~]# curl 192.168.200.147/abc/a/1.html
  5. abc/a/1.html

加个~,通过htpp://192.168.200.147/abc/a/1.html访问服务器,结果会重定向,可行
加个^~,通过htpp://192.168.200.147/abc/a/1.html访问服务器,结果会重定向,可行

6.请思考表达式问题2

设置location ~ /abc
用户访问http://192.168.200.147/cde/abc/a/1.html,结果会重定向,因为只要含有abc就行

如何阻止这样的URL重定向

设置location ~ ^/abc,只要是abc开头就可以

示例2

利用正则中的()和\1,替换URL中一部分的内容,将http://192.168.200.147/2016/a/b/c/1.html,换成http://192.168.200.147/2017/a/b/c/1.html

  1. 方法:
  2. location /2016 {
  3. rewrite ^/2016/(.*)$1 permanent;
  4. }

1.注释掉上个实验的重定向部分,避免实验出现交叉影响

  1. [root@192 ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. #location ~ ^/abc {
  4. #rewrite .* /ccc/bbb/2.html permanent;
  5. #}

2.配置地址重写

  1. #准备目标目录
  2. [root@192 ~]# mkdir /usr/share/nginx/html/2017/a/b/c -p
  3. #准备目标页面
  4. [root@192 ~]# echo "2017/a/b/c/1.html" > /usr/share/nginx/html/2017/a/b/c/1.html
  5. #修改配置文件
  6. [root@192 ~]# vim /etc/nginx/nginx.conf
  7. server {
  8. #添加以下内容
  9. location /2016 {
  10. rewrite ^/2016/(.*)$ /2017/$1;
  11. }
  12. #location ~ ^/abc {
  13. #rewrite .* /ccc/bbb/2.html permanent;
  14. #}
  15. #重启nginx
  16. [root@192 ~]# systemctl restart nginx

3.访问旧页面进行测试,没加permanent

  1. [root@192 ~]# curl 192.168.200.147/2016/a/b/c/1.html
  2. 2017/a/b/c/1.html

示例3

目的:了解判断在重定向中的使用方法
location{rewrite}只能替换URL中的目录路径,使用if (){rewrite}可以替换协议主机目录全部内容。将http://www.xushengkai.com换成http://jd.com

1.注释掉上个实验中添加的部分

  1. [root@192 ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. #location /2016 {
  4. #rewrite ^/2016/(.*)$ /2017/$1;
  5. #}

2.配置地址重写

  1. [root@192 ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. #$host调用主机地址,如果像是xushengkai.com的时候(不区分大小写),那么进行重写,都去访问www.jd.com
  4. if ( $host ~* xushengkai.com ) {
  5. rewrite .* http://jd.com permanent;
  6. }
  7. #重启nginx
  8. [root@192 ~]# systemctl restart nginx
  9. #在主机上设置域名解析
  10. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  11. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  12. 192.168.200.147 www.xushengkai.com

3.访问旧页面

打开浏览器,输入www.xushengkai.com,回车以后跳转到京东的页面,实验成功

示例4

前言:上一个实验中,不论输入的URL中内容是什么:http://xushengkai.com/1.html或者http://xushengkai/2.html,其结果都是把主机地址由xushengkai.com换成jd.com,$request全部都重定向至http://jd.com/1.htmlhttp://jd.com/2.html


目的:如果希望替换掉域名中的主机,保留后端url路径,可以使用nginx内置变量调用老的URL路径
示例:将http://xushengkai.com/ccc/bbb/2.html换成http://cloud.com/ccc/bbb/2.html
也就是只替换主机地址,跟上一个实验很相似
1.延续上一个实验

  1. #准备一个新的网站
  2. [root@192 ~]# vim /etc/nginx/conf.d/cloud.conf
  3. server {
  4. listen 80;
  5. server_name cloud.com;
  6. location / {
  7. root /cloud;
  8. index index.html;
  9. }
  10. }
  11. #重启nginx
  12. [root@192 ~]# systemctl restart nginx
  13. #准备新页面
  14. [root@192 ~]# mkdir /cloud
  15. [root@192 ~]# vim /cloud/index.html
  16. cloud.com #页面内容
  17. [root@192 ~]# mkdir /cloud/ccc/bbb -p
  18. [root@192 ~]# vim /cloud/ccc/bbb/2.html
  19. /cloud/ccc/bbb/2.html #页面内容
  20. #设置域名解析
  21. [root@192 ~]# vim /etc/hosts
  22. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  23. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  24. 192.168.200.147 xushengkai.com cloud.com

2.配置地址重写

  1. #将上个实验jd的内容注释掉,添加以下内容
  2. [root@192 ~]# vim /etc/nginx/nginx.conf
  3. server {
  4. if ( $host ~* xushengkai.com ) {
  5. rewrite .* http://cloud.com$request_uri permanent;
  6. } #用户只要访问xushengkai.com不管是哪个内容,域名重写为 http://cloud.com,请求的地址还是用户之前输入的地址
  7. #if ( $host ~* xushengkai.com ) {
  8. #rewrite .* http://jd.com;
  9. #}
  10. #重启nginx
  11. [root@192 ~]# systemctl restart nginx

3.访问旧的页面测试

  1. [root@192 ~]# curl xushengkai.com/ccc/bbb/2.html
  2. /cloud/ccc/bbb/2.html

示例5

目的:在访问URL是目录时,在URL自动添加一个“/”,如果不是目录,则不添加“/”。但是先做个判断,是目录才需要添加,不是目录就不添加
本示例是一个美化的例子

  1. 1.输入的URL是目录时,自动添加“/”
  2. http://wwwbaidu.com/abc
  3. 2.输入的URL是文件时,不添加“/”
  4. http:://www.baidu.com/abc/index.html
  5. 3.输入的URL是目录,但已经添加“/”时,不自动添加“/”
  6. http://www.baidu.com/abc/

语法

  1. if ( -d $request_filename ) {
  2. rewrite ^(.*)([^/])$ http:://$host$1$2/ permanent;
  3. }

1.注释掉上个实验的内容

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. #if ( $host ~* xushengkai.com ) {
  4. #rewrite .* http://cloud.com$request_uri permanent;
  5. #}
  6. #把之前的实验的配置文件删除,不然后面会报错
  7. [root@xushengkai html]# rm -rf /etc/nginx/conf.d/cloud.conf

2.配置地址重写

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. if ( -d $request_filename ) {
  4. rewrite ^(.*)([^/])$ http://$host$1$2/ permanent;
  5. } # [^/]排除的意思
  6. listen 80;
  7. listen [::]:80;
  8. server_name xushengkai.com; #将默认网站改为xushengkai.com
  9. root /usr/share/nginx/html;
  10. # Load configuration files for the default server block.
  11. include /etc/nginx/default.d/*.conf;
  12. location / {
  13. index index.html;
  14. } #设置默认页面
  15. #设置主页索引文件,如果主页不设置索引文件,会报错403
  16. [root@xushengkai ~]# vim /usr/share/nginx/html/ccc/bbb/index.html
  17. ccc/bbb/index.html
  18. #重启nginx
  19. [root@xushengkai ~]# systemctl restart nginx

3.访问页面进行测试

打开浏览器输入http://www.xushengkai.com/ccc/bbb,浏览器会将后面加“/”,输入http://www.xushengkai.com/index.html,浏览器不会添加“/”。只要是目录就加,后面加了“/”,浏览器不会帮我们加“/”

示例6(了解)

目的:将旧的URL的字段,引入重定向后新的URL中
http://www.xushengkai/login/haha.html
转为http://www.xushengkai/reg/login.php?user=haha

  1. 语法:
  2. location /login {
  3. rewrite ^/login/(.*)\.html$ /reg/login.php?user=$1
  4. } #以login开头,中间任意()方便后面调用,以.html结尾 替换成/reg/login.php?user=$1

因需要动态站点环境,理解概念即可

示例7

目的:目录的表达形式发生了变化。原先的“-”分割,变成了“/”目录层次
http://www.xushengkai/aa/11-22-33/1.html
转换为将http://www.xushengkai/aa/11/22/33/1.html

  1. 语法:
  2. location /aa {
  3. rewrite ^/aa/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /aa/$1/$2/$3$4 permanent;
  4. }

1.将上个实验的配置内容注释掉

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. #if ( -d $request_filename ) {
  3. #rewrite ^(.*)([^/])$ http://$host$1$2/;
  4. #}

2.配置地址重定向

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. location /aa {
  4. rewrite ^/aa/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /aa/$1/$2/$3$4 permanent;
  5. root /usr/share/nginx/html;
  6. }
  7. #准备aa/11/22/33目录
  8. [root@xushengkai ~]# mkdir -p /usr/share/nginx/html/aa/11/22/33
  9. [root@xushengkai ~]# echo "/usr/share/nginx/html/aa/11/22/33/1.html" >/usr/share/nginx/html/aa/11/22/33/1.html
  10. #重启nginx
  11. [root@xushengkai ~]# systemctl restart nginx

3.访问页面进行测试

打开浏览器访问http://xushengkai.com/aa/11-22-33/1.html,显示内容/usr/share/nginx/html/aa/11/22/33/1.html,设置成功

示例8

目的:引用原URL中的信息,重定向至目标的URL

http://alice.xushengkai.com ==>http://xushengkai.com/alice

http://jack.xushengkai.com ==>http://xushengkai.com/jack

  1. 语法:
  2. if ( $host ~* "^www.xushengkai.com$" ) {
  3. break;
  4. }
  5. if ($host ~*\.xushengkai.com$) {
  6. set $user $1;
  7. rewrite .* http://www.xushengkai.com/$user permanent;
  8. }

1.注释掉上个实验的配置内容

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. #location /aa {
  4. #rewrite ^/aa/([0-9]+)-([0-9]+)-([0-9]+)(.*) /aa/$1/$2/$3$4 permanent;
  5. #root /usr/share/nginx/html;
  6. #}

2.配置地址重写

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. if ( $host ~* "^www.xushengkai.com$" ) {
  4. break;
  5. }
  6. #参数解释:如果请示的主机像是以www.xushengkai.com结尾的,停止匹配
  7. if ( $host ~* "(.*)\.xushengkai\.com$" ){
  8. set $user $1;
  9. rewrite .* http://www.xushengkai.com/$user permanent;
  10. }
  11. #参数解释:如果请求的地址$host像是xushengkai.com,那就进行rewrite,
  12. #重启nginx
  13. [root@xushengkai ~]# systemctl restart nginx
  14. #在网站的主目录中创建两个新目录
  15. [root@xushengkai ~]# mkdir /usr/share/nginx/html/{jack,alice}
  16. #jcak的主页
  17. [root@xushengkai ~]# echo "is jack" > /usr/share/nginx/html/jack/index.html
  18. #alice的主页
  19. [root@xushengkai ~]# echo "is alice" > /usr/share/nginx/html/alice/index.html
  20. #在主机上设置域名解析
  21. 192.168.200.147 www.xushengkai.com jack.xushengkai.com alice.xushengkai.com

3.访问页面进行测试

打开浏览浏览器访问alice.xushengkai.com

打开浏览器访问jack.xushengkai.com

示例9

目的:如果服务器中的特殊文件,如.sh结尾的文件,则返回403操作拒绝

1.在网页主目录下创建.sh结尾的文件,在网页中测试

  1. [root@xushengkai ~]# cd /usr/share/nginx/html/
  2. [root@xushengkai html]# echo 111 > 1.sh
  3. #打开浏览器访问
  4. http://192.168.200.147/1.sh

2.在配置文件中设置不可访问

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. location ~* \.sh$ {
  4. return 403;
  5. }
  6. #重启nginx
  7. [root@xushengkai ~]# systemctl restart nginx

3.再次访问页面,权限拒绝

示例10

目的:last标记

1.准备3个页面

  1. [root@xushengkai html]# mkdir test
  2. [root@xushengkai html]# echo "break" > test/break.html
  3. [root@xushengkai html]# echo "last" > test/last.html
  4. [root@xushengkai html]# echo "test" > test/test.html

2.打开网站的主配置文件,填写测试语句

  1. [root@xushengkai ~]# vim /etc/nginx/nginx.conf
  2. server {
  3. location /break {
  4. rewrite .* /test/break.html break;
  5. }
  6. #用户的此次访问被重定向之后,将停止所有的匹配
  7. location /last {
  8. rewrite .* /test/last.html last;
  9. }
  10. #last在匹配成功后,不会进行立刻显示,显示下一个
  11. location /test {
  12. rewrite .* /test/test.html break;
  13. }
  14. #test测试页面
  15. #重启nginx
  16. [root@xushengkai ~]# systemctl restart nginx

3.测试页面

访问http://192.168.200.147/break

访问http://192.168.200.147/last,会显示test页面内容

九--CA&HTTPS

私有CA

前言

CA 证书颁发机构(CA, Certificate Authority)
基于https的协议工作的一中虚拟主机,要构建这样的网站需要mod_ssl模块的支持。且需要提供两个文件:证书文件和私钥文件,证书文件是标识这个网站服务器身份的,私钥文件主要用来实现在服务器端对数据进行加密,然后在网站中传输的。证书在生产生活中需要到对应的机构去申请,在实验环境中本应该搭建一台证书服务器.

生成证书及秘钥文件

1.准备存放证书和秘钥文件

  1. [root@xushengkai ~]# mkdir /etc/nginx/ssl

2.生成私钥,使用openssl生成基于rsa数学算法长度为1024bit的秘钥,文件必须以key结尾

  1. [root@xushengkai ~]# openssl genrsa 1024 > /etc/nginx/ssl/server.key
  2. Generating RSA private key, 1024 bit long modulus
  3. .......................................++++++
  4. .....++++++
  5. e is 65537 (0x10001)

3.使用秘钥文件生成证书-申请书

  1. [root@xushengkai ~]# openssl genrsa 1024 > /etc/nginx/ssl/server.key
  2. Generating RSA private key, 1024 bit long modulus
  3. .......................................++++++
  4. .....++++++
  5. e is 65537 (0x10001)
  6. [root@xushengkai ~]# openssl req -new -key /etc/nginx/ssl/server.key >/etc/nginx/ssl/server.csr
  7. You are about to be asked to enter information that will be incorporated
  8. into your certificate request.
  9. What you are about to enter is what is called a Distinguished Name or a DN.
  10. There are quite a few fields but you can leave some blank
  11. For some fields there will be a default value,
  12. If you enter '.', the field will be left blank.
  13. -----
  14. Country Name (2 letter code) [XX]:cn
  15. State or Province Name (full name) []:BJ
  16. Locality Name (eg, city) [Default City]:BJ
  17. Organization Name (eg, company) [Default Company Ltd]:SK
  18. Organizational Unit Name (eg, section) []:CLOUD
  19. Common Name (eg, your name or your server's hostname) []:LNMP
  20. Email Address []:111.1
  21. Please enter the following 'extra' attributes
  22. to be sent with your certificate request
  23. A challenge password []:
  24. An optional company name []:
  25. #查看证书
  26. [root@xushengkai ~]# ls /etc/nginx/ssl/
  27. server.csr server.key

4.同意申请,生成证书

  1. [root@xushengkai ~]# openssl req -x509 -days 365 -key /etc/nginx/ssl/server.key -in /etc/nginx/ssl/server.csr > /etc/nginx/ssl/server.crt
  2. #参数解释:
  3. -x 509:证书的格式。固定的
  4. -days :证书的有效期,固定的
  5. -key:指定秘钥文件
  6. -in:指定证书申请文件
  7. #查看证书
  8. [root@xushengkai ~]# ls /etc/nginx/ssl/
  9. server.crt server.csr server.key

私有CA的https部署实战

1.创建网页目录

  1. [root@xushengkai ~]# mkdir /bj
  2. [root@xushengkai ~]# echo "bj id ssl web" > /bj/index.html
  3. [root@xushengkai ~]# vim /etc/nginx/conf.d/bj.conf
  4. server {
  5. listen 80;
  6. server_name www.bj.com;
  7. location / {
  8. root /bj;
  9. index index.html;
  10. }
  11. }
  12. #重启nginx
  13. [root@xushengkai ~]# systemctl restart nginx

2.在主机上设置域名解析,访问页面,此时还不是加密访问

  1. 192.168.200.147 www.bj.com

3.修改bj页面的配置文件,设置端口,加密文件的位置

  1. [root@xushengkai ~]# vim /etc/nginx/conf.d/bj.conf
  2. server {
  3. listen 443 ssl;
  4. server_name www.bj.com;
  5. ssl_certificate /etc/nginx/ssl/server.crt;
  6. ssl_certificate_key /etc/nginx/ssl/server.key;
  7. location / {
  8. root /bj;
  9. index index.html;
  10. }
  11. }
  12. #重启nginx
  13. [root@xushengkai ~]# systemctl restart nginx
  14. #查看443端口
  15. [root@xushengkai ~]# ss -anp | grep 443
  16. tcp LISTEN 0 128 *:443 *:* users:(("nginx",pid=7451,fd=6),("nginx",pid=7450,fd=6),("nginx",pid=7449,fd=6))

4.测试访问https://www.bj.com

继续访问

公网CA构建阿里云服务器https服务

在阿里云上操作

十--Nginx的平滑升级

前言

随着 Nginx 越来越流行,并且 Nginx 的优势也越来越明显,Nginx 的版本迭代也开起了加速模式。新版本也带来了新的功能,例如 stream 四层代理功能等,伴随着 Nginx 的广泛应用,版本升级必然越来越快。一般有两种情况下需要升级 Nginx:一种是确实要升级 Nginx 的版本,另一种是要为 Nginx 添加新的模块。

原理

  • 在不停掉老进程的情况下,启动新进程。
  • 老进程负责处理仍然没有处理完的请求,但不再接受处理请求。
  • 新进程接受新请求。
  • 老进程处理完所有请求,关闭所有连接后,停止。

平滑升级1.12版本到1.14版本

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注