• 03--nginx架构实战


    前言:这应该是nginx梳理的最后一章,写一些关于网站架构和网站上线的知识内容,主要是感觉到运维并不是单一方向的行业,这一章概念会有一些广泛,但是非常重要,都是这几年工作中遇到的情况,整理一下相关知识,遇到时可以直接按照目录寻找解决方案。

    1、动态网站简介

    当用户访问一个网站时,动态网站会根据用户的请求,实时生成并展示页面内容,而静态网站则是提前生成好的页面内容,直接展示给用户。动态网站更适合需要频繁更新和交互性强的场景(如LOL官网登录),而静态网站则更适合内容稳定的情况

    动态网站根据架构不同大致有以下几种

    资源文件类型开发语言网站框架
    index.php

    开源的php

    Windows/Linux+nginx+php+mysql

    index.py

    开源python

    Windows/Linux+apache+python+mysql

    index.jsp

    商业JAVA

    windows/Linux+tomcat+JDK+Oracle

    index.asp

    商业c#

    Windows+iis+asp.net+sqlserver/oracle/mogodb

    2、LNMP动态网站部署

    2.1、linux部署

    设定静态ip为192.168.189.143

    修改安全配置,这里实验环境直接关闭防火墙和SELINUX

    1. [root@localhost ~]# systemctl stop firewalld.service
    2. [root@localhost ~]# systemctl disable firewalld.service
    3. Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service".
    4. Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
    5. [root@localhost ~]# setenforce 0
    6. [root@localhost ~]# vim /etc/selinux/config
    7. [root@localhost ~]# cat /etc/selinux/config
    8. SELINUX=disabled

    2.2、nginx部署

    nginx部署过程第一章所示

    2.3、php环境部署

    这里使用rpm包部署

    [root@localhost ~]# yum install -y php  php-mysqlnd gd php-gd
    

    2.3.1、测试LNP环境

    启动nginx

    1. [root@localhost ~]# systemctl start nginx
    2. [root@localhost ~]# systemctl enable nginx
    3. Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.

    修改nginx默认网站文件

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. #access_log /var/log/nginx/host.access.log main;
    7. location / {
    8. root /usr/share/nginx/html;
    9. index index.php index.html index.htm;
    10. }
    11. #error_page 404 /404.html;
    12. # redirect server error pages to the static page /50x.html
    13. #
    14. error_page 500 502 503 504 /50x.html;
    15. location = /50x.html {
    16. root /usr/share/nginx/html;
    17. }
    18. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    19. #
    20. #location ~ \.php$ {
    21. # proxy_pass http://127.0.0.1;
    22. #}
    23. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    24. #
    25. #location ~ \.php$ {
    26. # root html;
    27. # fastcgi_pass 127.0.0.1:9000;
    28. # fastcgi_index index.php;
    29. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
    30. # include fastcgi_params;
    31. #}
    32. # deny access to .htaccess files, if Apache's document root
    33. # concurs with nginx's one
    34. #
    35. #location ~ /\.ht {
    36. # deny all;
    37. #}
    38. }

    修改部分主要有网站主页,将index.php放在优先显示页面,需要注意的是fastcgi模块在一些旧版本中默认不调用,在遇到特殊适配需求需要注意nginx版本

    编辑php测试页面

    1. [root@localhost ~]# vim /usr/share/nginx/html/index.php
    2. [root@localhost ~]# cat /usr/share/nginx/html/index.php
    3. <?php
    4. phpinfo();
    5. ?>

    重启nginx,使用浏览器访问网站,显示如下

    2.3.2、故障排除

    此处因版本不同需要配置也各不相同,遇到访问网页不解析php文件,反而开始下载这类问题可以参考下面的排错思路:

    此处有可能没有开启php解析,也有可能php-fpm未启动,也有可能php-fpm启动但未监听9000端,按照下方步骤排查大概率能找出故障原因。

    检查php是否能够正常解析

    1. [root@localhost ~]# php -v
    2. PHP 8.0.30 (cli) (built: Aug 3 2023 17:13:08) ( NTS gcc x86_64 )
    3. Copyright (c) The PHP Group
    4. Zend Engine v4.0.30, Copyright (c) Zend Technologies
    5. with Zend OPcache v8.0.30, Copyright (c), by Zend Technologies

    nginx开启fastcgi

    1. server {
    2. 。。。。。。
    3. location ~ \.php$ {
    4. root /usr/share/nginx/html;
    5. fastcgi_pass 127.0.0.1:9000;
    6. fastcgi_index index.php;
    7. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    8. include fastcgi_params;
    9. }
    10. 。。。。。。

    检查php-fpm状态

    [root@localhost ~]# systemctl status php-fpm
    

    检查9000端口监听状态

    1. [root@localhost php-fpm.d]# netstat -tuln | grep 9000
    2. tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN

    如果发现9000未被监听可以修改php-fpm文件开启监听

    1. [root@localhost ~]# vim /etc/php-fpm.d/www.conf
    2. listen = 127.0.0.1:9000
    3. [root@localhost ~]# systemctl restart php-fpm

     这里可以结合下一小节的php-fpm加强理解

    2.4、mysql部署

    这里使用mariadb平替mysql(其实就是同一个软件)

    1. [root@localhost ~]# yum -y install mariadb-server mariadb
    2. [root@localhost ~]# systemctl start mysqld.service
    3. [root@localhost ~]# systemctl enable mysqld.service
    4. [root@localhost ~]# grep password /var/log/mysqld.log
    5. 查看初始密码
    6. [root@localhost ~]# mysqladmin -uroot -p'4rt+s?yIgIQl' password 'Liumuquan@123'
    7. 修改数据库root密码
    8. [root@localhost ~]# mysql -uroot -p'Liumuquan@123'
    9. 登录数据库
    10. mysql> create database discuz;
    11. 创建一个库
    12. mysql> flush privileges;
    13. Query OK, 0 rows affected (0.01 sec)

    2.5、动态网站部署

    下载一个源码包替代工作中前后端工程师提交的代码,这里使用开源代码discuz代替

    源代码下载地址

    将代码上传至网站文件夹,解压设置权限

    1. [root@localhost html]# unzip Discuz_X3.5_SC_UTF8_20240520.zip
    2. [root@localhost html]# cp -rf upload/* /usr/share/nginx/html/
    3. [root@localhost html]# chmod -R 777 /usr/share/nginx/html

     使用浏览器访问网址,按提示配置即可

    数据库按照刚才的配置填写

     配置成功

    3、fastcgi、php-fpm、php-mysql

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

    动态网站:需要多个软件合作完成。

    以LNMP为例,linux作为操作系统,提供了整个服务器环境的基础。常见的 Linux 发行版如 Ubuntu、CentOS、Debian 等都可以用于搭建 LNMP 架构。

    nginx借助模块ngx_fastcgi_modul模块,通过php与mysql完成互动,fastcgi是处理动态请求的接口,nginx 通过ngx_fastcgi_modul模块 链接 php-fpm处理动态请求

    PHP通过php-fpm接收前台nginx的动态访问的请求,比如向后端Mysql进行查询请求后,将查询结果返回给前台nginx。PHP-FPM(FastCGI Process Manager:FastCGI进程管理器) 是一个PHP FastCGI管理器。

    php向后端Mysql进行查询请求时,需要通过模块php-mysql(centos9中模块名称为php-mysqlnd)

    连接mysql,php-mysql是php连接mysql的接口程序。

    mysql负责存储数据,这样构成一个基础的LNMP结构。

    4、php-fpm初始化配置

    4.1、php-fpm相关配置文件

    4.1.1、核心配置文件

    1. vim /etc/php.ini
    2. date.timezone = PRC
    3. #设置PHP的时区
    4. open_basedir
    5. #设置PHP脚本允许访问的目录.

    4.1.2、全局配置文件

    1. vim /etc/php-fpm.conf
    2. pid = /run/php-fpm/php-fpm.pid
    3. #设置pid文件的位置
    4. error_log = log/php-fpm.log
    5. #记录错误日志的文件
    6. log_level = notice
    7. #记录日志的等级
    8. #alert(必须立即处理), error(错误情况), warning(警告情况), notice(一般重要信息), debug(调试信息). 默认: notice.
    9. process.max = 3
    10. #默认没设置,process.max: 控制子进程最大数的全局变量, 后边的设置子进程数量的指令受到这个值的限制, 0表示无限制,3代表允许三个人同时访问,第四个来了后需要排队等待。
    11. daemonize = yes
    12. #守护进程,将fpm转至后台运行

    4.1.3、扩展配置文件

    1. vim /etc/php-fpm.d/www.conf
    2. user = nginx
    3. #设置用户和用户组,此时nginx是php的客户
    4. listen.allowed_clients = 127.0.0.1
    5. #这里配置的是客户机的ip,也就是nginx的IP,分离部署的时候ip需要填写正常的ip
    6. listen = 127.0.0.1:9000
    7. #fpm监听端口,即nginx中php处理的地址,一般默认值即可。可用格式为: 'ip:port'
    8. slowlog = /var/log/php-fpm/$pool-slow.log
    9. #开启慢日志,慢日志记录请求时间很长的操作,用于优化
    10. pm=dynamic
    11. #动态模式进程管理开启,动态调整php进程数量
    12. start_servers=5
    13. #最初开启多少进程
    14. min_spare_server =5
    15. #最小的空闲进程数。用户访问会消耗进程,然后为了满足后续访问随时随地开启进程,保持空闲数为5。
    16. max_children = 50
    17. #最大进程数,取决于你的服务器内存。 假设你打算给10G内存给当前配置的PHP-FPM Pool,一般一个PHP请求占用内存10M-40M(操作简繁程度不同),按每个PHP请求占用内存25M,这样max_children = 10G/25M = 409。所以,这个值是根据硬件情况算出来的
    18. max_spare_servers=10
    19. #最大的多余进程。大规模断开后,高并发访问过后,还剩多少。
    20. max_requests = 500
    21. #每个子进程能响应的请求数量,响应500次后杀死该进程,进程处理完请求后会生成一部分缓存用来记录数据,需要定时清理。

    4.2、php-fpm实操配置

    进入刚布置好的LNMP环境,查看默认状态

     编辑拓展配置文件

    1. vim /etc/php-fpm.d/www.conf
    2. 修改对应参数如下
    3. pm = dynamic
    4. pm.start_servers = 10
    5. pm.max_children = 100
    6. pm.min_spare_servers = 10
    7. pm.max_spare_servers = 30
    8. pm.max_requests = 1000

    修改效果

    4.3、启动php状态监控页

    1. vim /etc/php-fpm.d/www.conf
    2. 修改对应参数如下
    3. pm.status_path = /php_status.php

    重启php-fpm然后使用浏览器访问设置的状态页面

    5、Nginx Location

    在LNMP的nginx配置中有三个配置位置

    1. http{
    2. 此处为主配置文件,在这里配置的参数将会影响本结构内所有虚拟主机
    3. }
    4. server{
    5. 此处为虚拟主机配置文件,配置内容只影响本台虚拟主机
    6. }
    7. server{
    8. location = /1.html{
    9. 此处配置参数影响上方使用正则匹配到的页面
    10. }
    11. }

    Location优先级:=             >                   ^~                >                  ~|~*|!~|!~*            >         /

    精确匹配>字符开头>正则匹配>通配

    6、Nginx Rewrite实战

    Nginx Rewrite是Nginx服务器软件中的一个模块,它允许用户根据一定的规则对请求的URL进行重写和重定向。通过使用Rewrite模块,用户可以在服务器级别或特定位置级别配置重写规则,以匹配和转换URL,实现URL重定向、重写以及其他高级URL操作。主要用途包括以下几点:

    • url伪静态化,提升搜索引擎评分。
    • 隐藏部分url参数,提高服务器安全性。
    • 实现网站地址跳转。

    6.1、Rewrite示例1

    要求:

    对当用户访问地址192.168.189.143/abc/a/1.html时,通过redirect重定向至192.168.189.143/ccc/bbb/2.html
    使用全新安装的nginx,此时默认主页为/usr/share/nginx/html/index.html

    修改默认主页内容

    1. [root@localhost ~]# vim /usr/share/nginx/html/index.html
    2. [root@localhost ~]# cat /usr/share/nginx/html/index.html
    3. <h1>/usr/share/nginx/html/index.html</h1>

    访问效果如下

     配置Nginx Rewrite

    1. [root@localhost ~]# mkdir -p /usr/share/nginx/html/ccc/bbb
    2. [root@localhost ~]# vim /usr/share/nginx/html/ccc/bbb/2.html
    3. [root@localhost ~]# cat /usr/share/nginx/html/ccc/bbb/2.html
    4. /usr/share/nginx/html/ccc/bbb/2.html

    5. #此处写的是跳转终点的内容

    修改网站配置文件

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. #access_log /var/log/nginx/host.access.log main;
    7. location / {
    8. root /usr/share/nginx/html;
    9. index index.html index.htm;
    10. }
    11. location /abc{
    12. rewrite .* /ccc/bbb/2.html permanent;
    13. }
    14. #最后一段为rewrite配置,意为永久(permanent)将模糊匹配/abc(里面带/abc的url)跳转到/ccc/bbb/2.html

    重启nginx,访问abc页面

    示例一结束

    关于permanent:permanent 会将地址显示为新的URL地址(重定向之后的URL)上面的示例中为添加后的效果,根据浏览器控制台可以看到添加上permanent后url被替换生成两次请求,服务器的角色只转换了url,客户端重新申请新获得的url。去掉permanent,url是旧的,服务器内部转换请求url,此处会略微增加服务器负载。如下图所示。(忽略404报错)

    6.2、Rewrite示例2

    要求:利用正则中的”()和\1 “, 替换url中一部分的内容。 将

    http://192.168.189.143/2023/a/b/c/1.html          换成

    http://192.168.189.143/2024/a/b/c/1.html

    使用全新安装的nginx,此时默认主页为/usr/share/nginx/html/index.html

    步骤如下:

    1. [root@localhost ~]# mkdir -p /usr/share/nginx/html/2024/a/b/c
    2. [root@localhost ~]# vim /usr/share/nginx/html/2024/a/b/c/2024abc.html
    3. [root@localhost ~]# cat /usr/share/nginx/html/2024/a/b/c/2024abc.html
    4. /usr/share/nginx/html/2024/a/b/c/2024abc.html

    5. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    6. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    7. server {
    8. listen 80;
    9. server_name localhost;
    10. #access_log /var/log/nginx/host.access.log main;
    11. location / {
    12. root /usr/share/nginx/html;
    13. index index.html index.htm;
    14. }
    15. location /2023{
    16. rewrite ^/2023/(.*)$ /2024/$1 ;
    17. }
    18. # (.*):这是一个捕获组,用于匹配任意字符,并将其保存供后续引用
    19. # $:匹配字符串的结束位置。
    20. # ^/2023/(.*)$匹配以 /2023/ 开头并以任意字符结尾的字符串
    21. # $1引用第一个捕获组的值

    重启nginx,访问2023

    6.3、Rewrite示例3

    要求:

    location { rewrite } 只能替换url中的目录路径, 使用if (){rewrite}可以替换协议主机目录全部能容。 将http://liumuquan.com 换http://jd.com

    使用全新安装的nginx,此时默认主页为/usr/share/nginx/html/index.html

    步骤如下:

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name localhost;
    6. #access_log /var/log/nginx/host.access.log main;
    7. location / {
    8. root /usr/share/nginx/html;
    9. index index.html index.htm;
    10. }
    11. if ($host ~* liumuquan.com) {
    12. rewrite .* http://jd.com;
    13. }
    14. if ($host ~* liumuquan.com):这个条件表示如果请求的主机名匹配正则表达式 liumuquan.com,不区分大小写形式,都会匹配。
    15. 如果请求的主机名符合上述条件,就会执行下面的重定向操作。
    16. rewrite .* http://jd.com;:这条指令表示对匹配条件下的所有请求,将URL重写为 http://jd.com。也就是说,无论用户请求的是 liumuquan.com 下的任何路径,比如 liumuquan.com/some/path,都会被重定向到 http://jd.com。

    此实验需要配置hosts用来解析liumuquan.com

    C:\Windows\System32\drivers\etc\hosts

    访问liumuquan.com,查看浏览器控制台

    6.4、Rewrite示例4

    要求:

    替换掉域名中的主机,保留后端url路径。可以使用nginx内置变量调用老的url目录路径。 如:将http://liumuquan.com/ccc/bbb/2.html 换成 http://cloud.com/ccc/bbb/2.html

    操作步骤

    1. #建立一个新网站cloud.com
    2. [root@localhost ~]# vim /etc/nginx/conf.d/cloud.conf
    3. [root@localhost ~]# cat /etc/nginx/conf.d/cloud.conf
    4. server{
    5. listen 80;
    6. server_name cloud_liumuquan.com;
    7. location / {
    8. root /cloud;
    9. index index.html;
    10. }
    11. }
    12. [root@localhost ~]# mkdir -p /cloud/ccc/bbb
    13. [root@localhost ~]# echo '

      /cloud/index.html

      ' > /cloud/index.html
    14. [root@localhost ~]# echo '

      /cloud/ccc/bbb/2.html

      ' > /cloud/ccc/bbb/2.html
    15. [root@localhost ~]# cat /cloud/index.html
    16. /cloud/index.html

    17. [root@localhost ~]# cat /cloud/ccc/bbb/2.html
    18. /cloud/ccc/bbb/2.html

    19. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    20. server {
    21. listen 80;
    22. server_name liumuquan.com;
    23. #access_log /var/log/nginx/host.access.log main;
    24. location / {
    25. root /usr/share/nginx/html;
    26. index index.html index.htm;
    27. }
    28. if ($host ~* liumuquan.com) {
    29. rewrite .* http://cloud_liumuquan.com$request_uri ;
    30. }
    31. #这里的变量来源均为nginx提供,参考日志格式设置(第一章)
    32. ..............
    33. [root@localhost ~]# systemctl restart nginx

    尝试访问主页及其他url

    6.5、Rewrite示例5

    要求:

    1.输入的URL是目录时,自动添加“/” http://www.baidu.com/abc

    2.输入的URL是文件时,不添加“/” http://www.baidu.com/abc/index.html

    3.输入的URL是目录,但已经添加"/"时,不添加“/” http://www.baidu.com/abc/

    步骤如下:

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name liumuquan.com;
    6. #access_log /var/log/nginx/host.access.log main;
    7. location / {
    8. root /usr/share/nginx/html;
    9. index index.html index.htm;
    10. }
    11. if (-d $request_filename){
    12. rewrite ^(.*)([^/])$ http://$host$1$2/;
    13. }
    14. #if (-d $request_filename):这是一个 if 指令,用于检查请求的文件路径是否为目录。如果请求的文件路径是一个目录,则条件成立。如果请求的文件路径是一个目录,就会执行下面的重定向操作。
    15. #rewrite ^(.*)([^/])$ http://$host$1$2/;:这是一个 rewrite 指令,用于对请求的 URL 进行重写。它的含义如下:
    16. # ^(.*)([^/])$:这是一个正则表达式,用于匹配不以斜杠结尾的 URL 路径。(.*)([^/])表示匹配任意字符直到不是斜杠的字符,并将其保存供后续引用。http://$host$1$2/:如果请求的文件路径是一个目录,且路径不以斜杠结尾,那么请求的 URL 将被重定向到以斜杠结尾的同一路径,并保持原始的协议和主机名不变。
    17. #()是捕获器 $是行尾

     准备用于测试的文件夹

    1. [root@localhost ~]# mkdir /usr/share/nginx/html/dir
    2. [root@localhost ~]# vim /usr/share/nginx/html/dir/index.html
    3. [root@localhost ~]# cat /usr/share/nginx/html/dir/index.html
    4. /usr/share/nginx/html/dir/index.html

    5. [root@localhost ~]# systemctl restart nginx

    访问http://liumuquan.com/dir

    6.6、Rewrite示例6

    将 http://liumuquan.com/dir/11-22-33/1.html 转换为

    http://liumuquan.com/dir/11/22/33/1.html

    步骤如下

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# mkdir -p /usr/share/nginx/html/dir/11/22/33/
    3. [root@localhost ~]# echo '

      /usr/share/nginx/html/dir/11/22/33/1.html

      ' > /usr/share/nginx/html/dir/11/22/33/1.html
    4. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    5. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    6. server {
    7. listen 80;
    8. server_name liumuquan.com;
    9. #access_log /var/log/nginx/host.access.log main;
    10. location / {
    11. root /usr/share/nginx/html;
    12. index index.html index.htm;
    13. }
    14. location /dir{
    15. rewrite ^/dir/([0-9]+)-([0-9]+)-([0-9]+)(.*)$ /dir/$1/$2/$3$4;
    16. root /usr/share/nginx/html;
    17. }

     重启nginx使用浏览器访问

    6.7、Rewrite示例7

    要求:

    将http://alice.liumuquan.com 转换为http://www.liumuquan.com/alice

    将http://jack.liumuquan.com 转换为http://www.liumuquan.com/jack

    步骤如下

    1. #环境准备
    2. [root@localhost ~]# mkdir /usr/share/nginx/html/{jack,alice}
    3. [root@localhost ~]# echo "jack" > /usr/share/nginx/html/jack/index.html
    4. [root@localhost ~]# echo "alice" > /usr/share/nginx/html/alice/index.html
    5. [root@localhost ~]# vim /etc/nginx/nginx.conf

    修改配置文件

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    3. server {
    4. listen 80;
    5. server_name www.liumuquan.com alice.liumuquan.com jack.liumuquan.com;
    6. #access_log /var/log/nginx/host.access.log main;
    7. location / {
    8. root /usr/share/nginx/html;
    9. index index.html index.htm;
    10. }
    11. if ($host ~* "^www.liumuquan.com$"){
    12. break;
    13. }
    14. if ($host ~* "^(.*)\.liumuquan\.com$"){
    15. set $user $1;
    16. rewrite .* http://www.liumuquan.com/$user;
    17. }
    18. # 注意server_name处修改
    19. # 第一段if防止死循环
    20. # set $user $1;的含义是将变量 $user 的值设置为正则表达式捕获组 $1 的值
    21. # 客户端也需要添加域名解析

    访问alice.liumuquan.com效果如下图

    6.8、Rewrite示例8

    要求:如果访问服务器中的特殊文件。如:.sh结尾的文件。则返回403操作拒绝错误

    步骤如下

    1. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    2. [root@localhost ~]# vim /usr/share/nginx/html/1.sh
    3. [root@localhost ~]# cat /usr/share/nginx/html/1.sh
    4. #!/bin/bash
    5. pwd
    6. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    7. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    8. server {
    9. listen 80;
    10. server_name liumuquan.com;
    11. #access_log /var/log/nginx/host.access.log main;
    12. location / {
    13. root /usr/share/nginx/html;
    14. index index.html index.htm;
    15. }
    16. location ~* \.sh$ {
    17. return 403;
    18. }

    访问1.sh效果如下

    6.9、Rewrite示例9

    要求:

    使用last实现重定向跳过,暂时隐藏页面

    演示步骤:

    1. [root@localhost ~]# mkdir /usr/share/nginx/html/test
    2. [root@localhost ~]# echo 'break' > /usr/share/nginx/html/test/break.html
    3. [root@localhost ~]# echo 'last' > /usr/share/nginx/html/test/last.html
    4. [root@localhost ~]# echo 'test' > /usr/share/nginx/html/test/test.html
    5. [root@localhost ~]# vim /etc/nginx/conf.d/default.conf
    6. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    7. server {
    8. listen 80;
    9. server_name liumuquan.com;
    10. #access_log /var/log/nginx/host.access.log main;
    11. location / {
    12. root /usr/share/nginx/html;
    13. index index.html index.htm;
    14. }
    15. location /break {
    16. rewrite .* /test/break.html break;
    17. root /usr/share/nginx/html;
    18. }
    19. location /last {
    20. rewrite .* /test/last.html last; # 这个位置的last会导致重定向失效直接去往下面的重定向
    21. root /usr/share/nginx/html;
    22. }
    23. location /test {
    24. rewrite .* /test/test.html break;
    25. root /usr/share/nginx/html;
    26. }

    访问/break

    访问/last(此处为重点)

     访问/test

    7、CA与HTTPS

    CA(Certificate Authority,证书颁发机构)和HTTPS(Hypertext Transfer Protocol Secure,超文本传输安全协议)之间有着密切的关系。

    简要了解一下这两个概念:

    • 证书颁发机构(CA)是一个权威的第三方实体,负责验证个人、组织或网站的身份,并签发数字证书来证明其身份的合法性。
    • HTTPS是一种通过加密和身份验证保护信息安全的通信协议。它使用SSL/TLS协议来创建加密连接,确保在客户端和服务器之间传输的数据是安全的。

    CA和HTTPS之间的关系可以总结如下:

    1. CA负责颁发SSL/TLS数字证书,这些证书用于在HTTPS连接中进行身份验证和加密通信。当用户访问一个使用HTTPS的网站时,浏览器会检查网站提供的数字证书是否由受信任的CA签发。
    2. 如果证书是由受信任的CA签发的,浏览器就会信任该证书,建立安全连接并显示一个锁形状的图标,表示连接是安全的。
    3. 如果证书无效或由不受信任的CA签发,浏览器会显示警告信息,提示用户存在安全风险。

     7.1、私有CA的https部署实战

    准备存放证书和秘钥的目录

    [root@localhost ~]# mkdir /etc/nginx/ssl
    

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

    centos8以前可以设置为1024长度,本次演示用的是9,故长度设置为2048

    [root@localhost ~]# openssl genrsa 2048 > /etc/nginx/ssl/server.key

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

    1. [root@localhost ~]# openssl req -new -key /etc/nginx/ssl/server.key > /etc/nginx/ssl/server.csr
    2. You are about to be asked to enter information that will be incorporated
    3. into your certificate request.
    4. What you are about to enter is what is called a Distinguished Name or a DN.
    5. There are quite a few fields but you can leave some blank
    6. For some fields there will be a default value,
    7. If you enter '.', the field will be left blank.
    8. -----
    9. Country Name (2 letter code) [XX]:CN
    10. #国家
    11. State or Province Name (full name) []:SC
    12. #省会
    13. Locality Name (eg, city) [Default City]:CD
    14. #城市
    15. Organization Name (eg, company) [Default Company Ltd]:liumuquan
    16. #组织
    17. Organizational Unit Name (eg, section) []:cloud
    18. #单位名
    19. Common Name (eg, your name or your server's hostname) []:liumuquan.com
    20. #网站名
    21. Email Address []:123456@qq.com
    22. #邮箱
    23. Please enter the following 'extra' attributes
    24. to be sent with your certificate request
    25. A challenge password []:
    26. #密码:空
    27. An optional company name []:
    28. #密码:空
    29. [root@localhost ~]# ls /etc/nginx/ssl/
    30. server.csr(证书申请) server.key(私钥)

    同意申请,生成证书

    1. [root@localhost ~]# openssl req -x509 -days 365 -key /etc/nginx/ssl/server.key -in /etc/nginx/ssl/server.csr > /etc/nginx/ssl/server.crt
    2. Warning: Not placing -key in cert or request since request is used
    3. Warning: No -copy_extensions given; ignoring any extensions in the request

    配置网站

    1. [root@localhost ~]# cat /etc/nginx/conf.d/default.conf
    2. server {
    3. listen 443 ssl;
    4. server_name liumuquan.com;
    5. #access_log /var/log/nginx/host.access.log main;
    6. ssl_certificate /etc/nginx/ssl/server.crt;
    7. ssl_certificate_key /etc/nginx/ssl/server.key;
    8. location / {
    9. root /usr/share/nginx/html;
    10. index index.html index.htm;
    11. }
    12. #error_page 404 /404.html;

    测试网站

    1. [root@localhost ~]# nginx -t
    2. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    3. nginx: configuration file /etc/nginx/nginx.conf test is successful
    4. [root@localhost ~]# nginx -s reload
    5. [root@localhost ~]# s
    6. Display all 287 possibilities? (y or n)
    7. [root@localhost ~]# ss -antp | grep nginx
    8. LISTEN 0 511 0.0.0.0:443 0.0.0.0:* users:(("nginx",pid=1863,fd=12),("nginx",pid=1862,fd=12),("nginx",pid=912,fd=12))
    9. [root@localhost ~]#

    使用浏览器访问网站

    选择接受风险并继续

    7.2、公有CA

    公有CA(Certificate Authority,证书颁发机构)是指受信任的第三方机构,为申请证书的企业组织颁发证书,并管理公共密钥和证书。这些证书必须符合严格的基准要求,以确保网站的高安全性。

    公有云网站都会提供数字证书管理服务(SSL)证书,可以为自己的网站获取免费证书。

    8、Nginx的平滑升级

    升级会造成程序的关闭和启动,平滑是指用户的访问不受影响。程序的升级就是替换文件,替换文件不可能不影响用户,所以所谓平滑只是相对影响较小。

    操作方法是使用源码安装新版本(必须使用源码),然后使用kill -USR2重启nginx,kill -USR2是一种特殊的重启,是指服务完当前用户后重启,此时并不处理新生成的链接,重启后后续用户进入新版本。

    (1)编译安装新版本的nginx,指定安装目录为新目录

    1. [root@server nginx]# tar xf nginx-1.14.2.tar.gz -C /usr/local/src/
    2. [root@server nginx]# cd /usr/local/src/nginx-1.14.2/
    3. [root@server nginx-1.14.2]# ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx14 --with-http_stub_status_module --with-http_ssl_module && make && make install

    (2)查看就的nginx的主进程号和工作进程号

    1. [root@server ~]# ps aux |grep ngin[x]
    2. root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx
    3. nobody 75083 0.0 0.1 21060 1632 ? S 12:17 0:00 nginx: worker process
    4. [x] 是一个正则表达式中的特殊字符,用于匹配前面字符 "gin" 后跟着任意一个字符的情况。因此,ngin[x] 可以匹配到 "nginx" 这个关键词,定位与 Nginx 服务相关的进程信息

    (3)替换旧的执行程序

    1. [root@server ~]# mv /usr/local/nginx/sbin/nginx{,.bak}
    2. #移除并备份,这个命令表示在文件名末尾添加 .bak 后缀
    3. [root@server ~]# cp /usr/local/nginx14/sbin/nginx /usr/local/nginx/sbin/nginx
    4. #完成主程序替换
    5. [root@server ~]# /usr/local/nginx/sbin/nginx -v
    6. nginx version: nginx/1.14.2
    7. #验证版本,此时文件已经替换,但nginx仍正常运行

    (4)给主进程发送USR2信号

    1. [root@server ~]# cat /usr/local/nginx/logs/nginx.pid
    2. 68595
    3. [root@server ~]# kill -USR2 68595
    4. #查看nginx pid,会出现一个nginx.pid.oldbin
    5. [root@server ~]# cat /usr/local/nginx/logs/nginx.pid.oldbin
    6. 68595
    7. #给旧的进程发送一个kill -USR2的信号,会启动一个新的nginx主进程,实现热升级

    (5)给进程发送WINCH信号

    1. [root@server ~]# kill -WINCH 68595
    2. #kill -WINCH“从容关闭”
    3. [root@server ~]# ps aux |grep ngin[x]
    4. root 58943 0.0 0.3 45940 3260 ? S 13:34 0:00 nginx: master process /usr/local/nginx/sbin/nginx
    5. nginx 58944 0.0 0.1 46388 1888 ? S 13:34 0:00 nginx: worker process
    6. root 68595 0.0 0.1 20640 1548 ? Ss 12:12 0:00 nginx: master process /usr/local/nginx/sbin/nginx

  • 相关阅读:
    我与Vue.js 2.x 的七年之痒
    Spring Boot + Vue的网上商城之商品分类
    如何通用系统平台这个黑科技把网店做大,需要注意什么?
    时间序列分析|数据裁剪和滚动异常值检测
    容器化技术最佳实践1--容器化技术简介与Docker入门
    隆云通PM2.5+PM10+TSP传感器
    精解括号匹配问题与极致栈设计:揭开最大栈和最小栈的奥秘
    【Java集合类面试三十】、BlockingQueue中有哪些方法,为什么这样设计?
    SAP-写了一个FUNCTION,用于读取订单中,指定工序的状态。
    C# CodeFormer Colorization 人脸着色
  • 原文地址:https://blog.csdn.net/qq_43387908/article/details/139361988