IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> nginx平滑升级与location修饰符 -> 正文阅读

[系统运维]nginx平滑升级与location修饰符

nginx平滑升级与location修饰符


一、nginx平滑升级


1、部署nginx

//创建系统用户nginx
[root@10 ~]# useradd -r -M -s /sbin/nologin nginx

//安装依赖环境
[root@10 ~]# yum -y install pcre-devel openssl openssl-devel gd-devel gcc gcc-c++
[root@10 ~]# yum -y groups mark install 'Development Tools'


//创建日志存放目录
[root@10 ~]# mkdir -p /var/log/nginx
[root@10 ~]# chown -R nginx.nginx /var/log/nginx
[root@10 ~]#


//下载nginx
[root@10 ~]# cd /usr/src/
[root@10 src]# wget http://nginx.org/download/nginx-1.22.0.tar.gz
--2022-10-10 14:15:30--  http://nginx.org/download/nginx-1.22.0.tar.gz
Resolving nginx.org (nginx.org)... 3.125.197.172, 52.58.199.22, 2a05:d014:edb:5702::6, ...
Connecting to nginx.org (nginx.org)|3.125.197.172|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1073322 (1.0M) [application/octet-stream]
Saving to: ‘nginx-1.22.0.tar.gz’

nginx-1.22.0.tar.gz                       100%[====================================================================================>]   1.02M   301KB/s    in 3.5s

2022-10-10 14:15:34 (301 KB/s) - ‘nginx-1.22.0.tar.gz’ saved [1073322/1073322]

[root@10 src]#

//编译安装
[root@10 src]# ls
debug  kernels  nginx-1.22.0.tar.gz
[root@10 src]# tar xf nginx-1.22.0.tar.gz
[root@10 src]# cd nginx-1.22.0
[root@10 nginx-1.22.0]# ./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@10 nginx-1.22.0]# make -j $(grep 'processor' /proc/cpuinfo | wc -l) && make install


//配置环境变量
[root@10 ~]# echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh
[root@10 ~]# . /etc/profile.d/nginx.sh
[root@10 ~]#

//服务控制方式,使用nginx命令
  -t  //检查配置文件语法
  -v  //输出nginx的版本
  -c  //指定配置文件的路径
  -s  //发送服务控制信号,可选值有{stop|quit|reopen|reload}

//启动nginx
[root@10 ~]# nginx
[root@10 ~]# 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                                     [::]:*
[root@10 ~]#

2、获取之前安装nginx的编译参数

[root@10 ~]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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@10 ~]#

3、下载新模块

[root@10 ~]# wget https://github.com/openresty/echo-nginx-module
[root@10 ~]# ls
anaconda-ks.cfg  echo-nginx-module-master.zip

4、重新编译软件

//安装unzip工具
[root@10 ~]# yum -y install unzip


//解压新的模块包
[root@10 ~]# unzip echo-nginx-module-master.zip

//再次解压nginx源码包
[root@10 ~]# ls
anaconda-ks.cfg  echo-nginx-module-master  echo-nginx-module-master.zip  nginx-1.22.0.tar.gz
[root@10 ~]# tar -zxf nginx-1.22.0.tar.gz


//添加新的模块进行编译安装
[root@10 ~]# cd nginx-1.22.0
[root@10 nginx-1.22.0]# ./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 \
--add-module=../echo-nginx-module-master


//查看objs目录下没有nginx程序
[root@10 nginx-1.22.0]# ls
auto  CHANGES  CHANGES.ru  conf  configure  contrib  html  LICENSE  Makefile  man  objs  README  src
[root@10 nginx-1.22.0]# ls objs/
addon  autoconf.err  Makefile  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  src

//使用make编译
[root@10 nginx-1.22.0]# make

//编译完成之后再次进行查看objs目录下是否有nginx程序
[root@10 nginx-1.22.0]# ls objs/
addon  autoconf.err  Makefile  nginx  nginx.8  ngx_auto_config.h  ngx_auto_headers.h  ngx_modules.c  ngx_modules.o  src
[root@10 nginx-1.22.0]#

5、备份源程序并停止、覆盖、启动服务

//先查看升级前和升级后的版本区别,主要看编译参数
//升级前
[root@10 nginx-1.22.0]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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@10 nginx-1.22.0]#


//升级后
[root@10 nginx-1.22.0]# objs/nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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 --add-module=../echo-nginx-module-master

//停止服务
[root@10 nginx-1.22.0]# nginx -s stop

//备份
[root@10 nginx-1.22.0]# cp /usr/local/nginx/sbin/nginx /opt/

//覆盖
[root@10 nginx-1.22.0]# cp objs/nginx /usr/local/nginx/sbin/
cp: overwrite '/usr/local/nginx/sbin/nginx'? y


//启动服务
[root@10 nginx-1.22.0]# /usr/local/nginx/sbin/nginx   //前面配置了环境变量,此处能直接nginx启动服务


//查看端口
[root@10 nginx-1.22.0]# 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                                     [::]:*
[root@10 nginx-1.22.0]#


//查看nginx信息
[root@10 nginx-1.22.0]# nginx -V
nginx version: nginx/1.22.0
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-4) (GCC)
built with OpenSSL 1.1.1k  FIPS 25 Mar 2021
TLS SNI support enabled
configure arguments: --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 --add-module=../echo-nginx-module-master
[root@10 nginx-1.22.0]#

6、测试–引用echo模块

[root@10 ~]# vim /usr/local/nginx/conf/nginx.conf
//修改location参数内容就行
server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
                 echo "mike";
        }


//使用nginx  -t 检查配置文件内容
[root@10 ~]# 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

//重载nginx
[root@10 ~]# nginx -s reload

使用浏览器访问,发现浏览器无法支持echo模块,会当成文件进行下载

使用命令进行访问
请添加图片描述

二、location案例


location区段,通过指定模式来与客户端请求的URI相匹配
功能:
允许根据用户请求的URI来匹配定义的各个location,匹配时,此请求将被相应的location配置块中的配置所处理,例如做访问控制等功能
语法:
location [修饰符] pattern {…}
修饰符
= 精确匹配
~ 正则表达式模式匹配,区分大小写
~* 正则表达式模式匹配,不区分大小写
^~ 前缀匹配,类似于无修饰符的行为,也是以指定模块开始,不同的是,如果模式匹配,那么就停止搜索其他模式了,不支持正则表达式
@ 定义命名location区段,这些区段客户端不能访问,只可以由内部产生的请求来访问,如try_files或者error_page等

//增加一个location字段
[root@10 ~]# cd /usr/local/nginx/conf/
[root@10 conf]# vim nginx.conf
location / {
         echo "jan";
}
[root@10 conf]# nginx -s reload

请添加图片描述

//用“=”精准匹配
[root@10 conf]# vim nginx.conf
 location = /147 {
          echo "jan";
 }
[root@10 conf]# nginx -s reload

请添加图片描述

// ~:表示指定的正则表达式要区分大小写
[root@10 conf]# vim nginx.conf
location ~ /666$ {
         echo "jan";
}
[root@10 conf]# nginx -s reload

请添加图片描述

//当“=”和“~”同时存在时,此时时 = 优先于 ~
[root@10 conf]# vim nginx.conf
location ~ /666$ {
         echo "jan";
}
location = /666 {
         echo "mike";
}
[root@10 conf]# nginx -s reload

请添加图片描述

// ~*:表示指定的正则表达式不区分大小写:
[root@10 conf]# vim nginx.conf
location ~* ^/666abc$ {
          echo "jan";
}
 [root@10 conf]# nginx -s reload

请添加图片描述

查找顺序和优先级:由高到低
1、带有“=”的精确匹配优先
2、正则表达式按照他们在配置文件中定义的顺序
3、带有“^~”修饰符的,开头匹配
4、带有或者*修饰符的,如果正则表达式与URI匹配
5、没有修饰符的精确匹配

location = 路径
location ^~ 路径
location ~ 正则
location ~* 正则
location 路径

  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2022-10-17 13:11:55  更:2022-10-17 13:12:53 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年9日历 -2024/9/19 9:48:12-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码