当前位置: 首页 > news >正文

站长网ppt模板下载成都十大互联网公司

站长网ppt模板下载,成都十大互联网公司,服务器维护成本,淮北建设网站nginx平滑升级添加echo模块、location配置、rewrite配置 文章目录 nginx平滑升级添加echo模块、location配置、rewrite配置1.环境说明#xff1a;2.nginx平滑升级原理#xff1a;3.平滑升级nginx#xff0c;并添加echo模块3.1.查看当前nginx版本以及老版本编译参数信息3.2.下…nginx平滑升级添加echo模块、location配置、rewrite配置 文章目录 nginx平滑升级添加echo模块、location配置、rewrite配置1.环境说明2.nginx平滑升级原理3.平滑升级nginx并添加echo模块3.1.查看当前nginx版本以及老版本编译参数信息3.2.下载nginx-1.24.0源码包和echo模块3.3.编译安装nginx-1.24.03.4.echo模块的使用 4.location的配置5.rewrite也叫URL重定向5.1.工作原理5.2.配置rewrite 1.环境说明 主机名称IP地址所需服务系统版本nginx192.168.195.133nginx-1.22.1非最新版即可centos 8zabbix192.168.195.130无充当访问nginx主机的客户机centos 8 2.nginx平滑升级原理 Nginx 平滑升级是指在不中断服务的情况下将旧版本的 Nginx 服务器升级到新版本。其原理主要基于以下几个步骤 复制新版本的 Nginx 可执行文件首先将新版本的 Nginx 可执行文件复制到服务器上的指定目录。这个目录可以是旧版本 Nginx 的安装目录或其他合适的位置。重启旧版本的 Nginx 进程使用旧版本的 Nginx 启动脚本或命令来重启 Nginx 进程。这样做会使新的 Nginx 可执行文件生效但并不会立即中断正在运行的连接。新旧版本并存旧版本的 Nginx 进程会继续接收和处理来自客户端的连接同时新版本的 Nginx 进程在相同的端口上启动并开始监听新的连接。逐渐切换连接到新版本新版本的 Nginx 进程会逐步接管旧版本的连接。这可以通过逐渐降低旧版本 Nginx 的 worker 进程数并相应增加新版本 Nginx 的 worker 进程数来实现。当旧版本的连接逐渐结束时新版本的 Nginx 进程将完全接管所有连接。 通过这种平滑升级的方式可以最大程度地减少服务中断时间确保用户无感知地完成升级过程。但是在实际操作中仍需谨慎进行确保升级过程的稳定性和可靠性。 3.平滑升级nginx并添加echo模块 3.1.查看当前nginx版本以及老版本编译参数信息 //首先查看我们当前的nginx版本使用-v选项查看得知为nginx-1.22.1 [rootnginx ~]# nginx -v nginx version: nginx/1.22.1//使用-V选项可以查看nginx编译时的参数信息 [rootnginx ~]# nginx -V nginx version: nginx/1.22.1 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 --usernginx --groupnginx --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//备份老版本 [rootnginx ~]# cp /usr/local/nginx/sbin/nginx /opt/nginx-1.22.13.2.下载nginx-1.24.0源码包和echo模块 nginx官网nginx: download //下载nginx-1.24.0的源码包 [rootnginx ~]# wget https://nginx.org/download/nginx-1.24.0.tar.gz --2023-10-19 16:33:35-- https://nginx.org/download/nginx-1.24.0.tar.gz Resolving nginx.org (nginx.org)... 52.58.199.22, 3.125.197.172, 2a05:d014:edb:5704::6, ... Connecting to nginx.org (nginx.org)|52.58.199.22|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1112471 (1.1M) [application/octet-stream] Saving to: ‘nginx-1.24.0.tar.gz.1’nginx-1.24.0.tar.gz.1 100%[] 1.06M 648KB/s in 1.7s 2023-10-19 16:33:39 (648 KB/s) - ‘nginx-1.24.0.tar.gz.1’ saved [1112471/1112471][rootnginx ~]# ls anaconda-ks.cfg nginx-1.22.1.tar.gz nginx-1.24.0.tar.gzgithub网站拉取echo模块Repository search results · GitHub //安装所需命令的软件包 [rootnginx ~]# yum -y install git [rootnginx ~]# which git /usr/bin/git//通过git命令拉取到虚拟机中 [rootnginx ~]# git clone https://github.com/openresty/echo-nginx-module.git Cloning into echo-nginx-module... remote: Enumerating objects: 3061, done. remote: Counting objects: 100% (43/43), done. remote: Compressing objects: 100% (31/31), done. remote: Total 3061 (delta 21), reused 30 (delta 12), pack-reused 3018 Receiving objects: 100% (3061/3061), 1.18 MiB | 944.00 KiB/s, done. Resolving deltas: 100% (1645/1645), done. [rootnginx ~]# ls anaconda-ks.cfg echo-nginx-module nginx-1.22.1.tar.gz nginx-1.24.0.tar.gz3.3.编译安装nginx-1.24.0 //解压nginx-1.24.0 [rootnginx ~]# tar xf nginx-1.24.0.tar.gz [rootnginx ~]# ls anaconda-ks.cfg echo-nginx-module nginx-1.22.1.tar.gz nginx-1.24.0 nginx-1.24.0.tar.gz//编译安装nginx-1.24.0并添加一个echo模块 [rootnginx ~]# cd nginx-1.24.0/ [rootnginx nginx-1.24.0]# ./configure --help | grep add-module--add-modulePATH enable external module [rootnginx nginx-1.24.0]# ./configure --prefix/usr/local/nginx --usernginx --groupnginx --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 省略. . .nginx path prefix: /usr/local/nginxnginx binary file: /usr/local/nginx/sbin/nginxnginx modules path: /usr/local/nginx/modulesnginx configuration prefix: /usr/local/nginx/confnginx configuration file: /usr/local/nginx/conf/nginx.confnginx pid file: /usr/local/nginx/logs/nginx.pidnginx error log file: /var/log/nginx/error.lognginx http access log file: /var/log/nginx/access.lognginx http client request body temporary files: client_body_tempnginx http proxy temporary files: proxy_tempnginx http fastcgi temporary files: fastcgi_tempnginx http uwsgi temporary files: uwsgi_tempnginx http scgi temporary files: scgi_temp//编译新版本或新功能不能使用make install [rootnginx nginx-1.24.0]# make //直接使用make编译不能使用make install//编译完成后我们的主程序文件是安装目录中的objs目录中的nginx我们也可以看出/usr/local/nginx/sbin/nginx同当前目录下objs目录下的nginx文件大小的区别 [rootnginx nginx-1.24.0]# ls auto CHANGES CHANGES.ru conf configure contrib html LICENSE Makefile man objs README src [rootnginx nginx-1.24.0]# ls objs/ addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o autoconf.err nginx ngx_auto_config.h ngx_modules.c src [rootnginx nginx-1.24.0]# ll /usr/local/nginx/sbin/nginx -rwxr-xr-x 1 root root 6192536 Oct 16 15:54 /usr/local/nginx/sbin/nginx [rootnginx nginx-1.24.0]# ll objs/nginx -rwxr-xr-x 1 root root 6740696 Oct 19 16:46 objs/nginx//我们需要将objs/nginx替换掉/usr/local/nginx/sbin/nginx,此时我们只能使用一条命令的方式将原来版本启动的nginx停止然后将其替换后立即使用新版本的nginx启动服务 [rootnginx nginx-1.24.0]# ss -antl //此时nginx服务处于开启状态 State Recv-Q Send-Q Local Address:Port Peer Address:Port 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 [::]:* [rootnginx nginx-1.24.0]# cd objs/ [rootnginx objs]# ls addon Makefile nginx.8 ngx_auto_headers.h ngx_modules.o autoconf.err nginx ngx_auto_config.h ngx_modules.c src [rootnginx objs]# /usr/local/nginx/sbin/nginx -s stop;\cp nginx /usr/local/nginx/sbin/nginx;nginx //一个分号为一条命令\cp是为了覆盖时不询问是否覆盖而是直接覆盖 [rootnginx objs]# ss -antl //端口正常存在 State Recv-Q Send-Q Local Address:Port Peer Address:Port 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 [::]:* [rootnginx objs]# nginx -v //再次查看版本成功升级为nginx-1.24.0版本 nginx version: nginx/1.24.0平滑升级完成 3.4.echo模块的使用 Nginx中的echo模块是一个第三方模块用于自定义响应内容。它的主要用途是在Nginx服务器中生成自定义响应而不是从后端服务器获取内容。echo模块通常用于以下一些情况 测试和调试在开发和调试过程中echo模块允许管理员轻松生成自定义的HTTP响应以验证Nginx配置是否按预期工作。自定义错误页面你可以使用echo模块来创建自定义的错误页面以替代Nginx默认的错误页面。这样你可以提供更有吸引力或品牌一致的错误信息给用户。重定向和重写echo模块允许你创建自定义的重定向规则或URL重写规则以满足特定需求。这可以用于URL映射或路由规则。动态内容生成虽然不是最有效的方式但你可以使用echo模块来生成一些动态内容例如当前时间戳或其他简单的信息。这通常不如通过后端应用程序生成内容高效。HTTP头信息操作echo模块也允许你操作HTTP响应的头信息。你可以添加、删除或修改HTTP头以满足特定的需求。 以下是一个简单的示例展示了如何使用echo模块来生成自定义的HTTP响应 location /custom {echo This is a custom response;echo Generated by the echo module; }需要注意的是echo模块是一个第三方模块可能需要手动编译Nginx以包含该模块。此外它通常不建议用于生成大量动态内容因为Nginx的性能在处理静态内容和代理请求方面更出色。对于大规模的动态内容生成通常会使用专门的Web应用程序服务器如Node.js、Django、或Ruby on Rails。 4.location的配置 location区段通过指定模式来与客户端请求的URI相匹配 //功能允许根据用户请求的URI来匹配定义的各location匹配到时此请求将被相应的location配置块中的配置所处理例如做访问控制等功能//语法location [ 修饰符 ] pattern {......}常用修饰符说明 修饰符功能精确匹配~正则表达式模式匹配区分大小写~*正则表达式模式匹配不区分大小写^~前缀匹配类似于无修饰符的行为也是以指定模块开始不同的是如果模式匹配那么就停止搜索其他模式了不支持正则表达式定义命名location区段这些区段客户端不能访问只可以由内部产生的请求来访问如try_files或error_page等 没有修饰符表示必须以指定模式开始如 [rootnginx ~]# vim /usr/local/nginx/conf/nginx.conf [rootnginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . location /abc {echo this is /abc;root html;} . . . . . [rootnginx ~]# nginx -s reload//以下方法都能访问到?后面是用来传参数的相当于登录用户输入账号密码后会以变量的方式传给服务器 [rootzabbix ~]# curl http://192.168.195.137/abc this is /abc [rootzabbix ~]# curl http://192.168.195.137/abc?a10\b20 this is /abc [rootzabbix ~]# curl http://192.168.195.137/abc/ this is /abc表示必须与指定的模式精确匹配如 [rootnginx ~]# vim /usr/local/nginx/conf/nginx.conf [rootnginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . location /abc {echo this is abc;root html;} . . . . . [rootnginx ~]# nginx -s reload//以下方法都能访问到 [rootzabbix ~]# curl http://192.168.195.137/abc this is abc [rootzabbix ~]# curl http://192.168.195.137/abc?a10\b20 this is abc~表示指定的正则表达式要区分大小写如 [rootnginx ~]# vim /usr/local/nginx/conf/nginx.conf [rootnginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . location ~ ^/abc$ {echo this is ~abc;root html;} . . . . . [rootnginx ~]# nginx -s reload//以下方法都能访问到 [rootzabbix ~]# curl http://192.168.195.137/abc this is ~abc [rootzabbix ~]# curl http://192.168.195.137/abc?a10\b20 this is ~abc~*表示指定的正则表达式不区分大小写如 [rootnginx ~]# vim /usr/local/nginx/conf/nginx.conf [rootnginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . location ~* ^/abc$ {echo this is ~*abc;root html;} . . . . . [rootnginx ~]# nginx -s reload//以下方法都能访问到 [rootzabbix ~]# curl http://192.168.195.137/abc this is ~*abc [rootzabbix ~]# curl http://192.168.195.137/ABC this is ~*abc [rootzabbix ~]# curl http://192.168.195.137/abc?a10\b20 this is ~*abc [rootzabbix ~]# curl http://192.168.195.137/ABC?a10\b20 this is ~*abc^~表示指定的正则表达式的路径以他开头的都能匹配到如 [rootnginx ~]# vim /usr/local/nginx/conf/nginx.conf [rootnginx ~]# cat /usr/local/nginx/conf/nginx.conf . . . . . location ^~ /abc/ {echo this is ^~abc;root html;} . . . . . [rootnginx ~]# nginx -s reload//以下方法都能访问到 [rootzabbix ~]# curl http://192.168.195.137/abc/ this is ^~abc [rootzabbix ~]# curl http://192.168.195.137/abc/fdf this is ^~abc~类似于无修饰符的行为也是以指定模式开始不同的是如果模式匹配则停止搜索其他模式 查找顺序和优先级由高到底依次为 带有的精确匹配优先正则表达式按照他们在配置文件中定义的顺序带有^~修饰符的开头匹配带有~或~*修饰符的如果正则表达式与URI匹配没有修饰符的精确匹配 优先级次序如下 ( location 路径 ) -- ( location ^~ 路径 ) -- ( location ~ 正则 ) -- ( location ~* 正则 ) -- ( location 路径 )5.rewrite也叫URL重定向 语法rewrite regex replacement flag;如 rewrite ^/images/(.*\.jpg)$ /imgs/$1 break;此处的$1用于引用(.*.jpg)匹配到的内容又如 rewrite ^/bbs/(.*)$ http://www.idfsoft.com/index.html redirect;如上例所示replacement可以是某个路径也可以是某个URL 常见的flag flag作用last基本上都用这个flag表示当前的匹配结束继续下一个匹配最多匹配10个到20个 一旦此rewrite规则重写完成后就不再被后面其它的rewrite规则进行处理 而是由UserAgent重新对重写后的URL再一次发起请求并从头开始执行类似的过程break中止Rewrite不再继续匹配 一旦此rewrite规则重写完成后由UserAgent对新的URL重新发起请求 且不再会被当前location内的任何rewrite规则所检查redirect以临时重定向的HTTP状态302返回新的URLpermanent以永久重定向的HTTP状态301返回新的URL rewrite模块的作用是用来执行URL重定向。这个机制有利于去掉恶意访问的url也有利于搜索引擎优化SEO nginx使用的语法源于Perl兼容正则表达式PCRE库基本语法如下 标识符意义^必须以^后的实体开头$必须以$前的实体结尾.匹配任意字符[]匹配指定字符集内的任意字符[^]匹配任何不包括在指定字符集内的任意字符串|匹配 | 之前或之后的实体()分组组成一组用于匹配的实体通常会有 | 来协助 捕获子表达式可以捕获放在()之间的任何文本比如 ^(hello|sir)$ //字符串为“hi sir”捕获的结果$1hi$2sir//这些被捕获的数据在后面就可以当变量一样使用了5.1.工作原理 匹配请求URIrewrite 首先使用正则表达式来匹配请求的URI。这个正则表达式通常出现在 location 块内它会尝试与请求URI进行匹配。捕获组如果正则表达式中包含捕获组那么匹配成功后捕获组可以用于后续的处理。例如你可以在正则表达式中使用括号来创建捕获组然后在重写字符串中使用 $1、$2 等来引用这些捕获组。替换URI如果正则表达式匹配成功rewrite 将使用指定的替换字符串来修改请求的URI。替换字符串通常包含替代变量例如 $1、$2 等这些变量引用了正则表达式中的捕获组。选择重定向类型rewrite 通常还支持一些标志如 permanent 和 redirect用于指定重定向类型。例如permanent 表示永久重定向HTTP 301而 redirect 表示临时重定向HTTP 302。继续处理一旦URI被重写Nginx会继续处理重写后的URI。这可能涉及进一步的location块匹配、其他rewrite指令的处理或将请求传递给后端服务器或静态资源。 下面是一个示例演示了如何使用rewrite来重定向请求 location /old-path/ {rewrite ^/old-path/(.*)$ /new-path/$1 permanent; }在这个示例中如果请求URI匹配/old-path/rewrite将捕获匹配的部分例如xxx然后将请求重定向到/new-path/xxx使用permanent标志表示永久重定向。 总之rewrite指令的工作原理是基于正则表达式的匹配和URI的替换使得你可以对传入的HTTP请求进行重定向和修改。 5.2.配置rewrite //在html目录下新建一个目录文件存放跳转页面 [rootnginx html]# cd [rootnginx ~]# cd /usr/local/nginx/html [rootnginx html]# ls 50x.html index.html [rootnginx html]# mkdir images [rootnginx html]# ls 50x.html images index.html [rootnginx html]# cd images/ [rootnginx images]# echo hello world index.html [rootnginx images]# cat index.html hello world [rootnginx images]#能够访问 //现在将html下面的images目录改为其他的名字imgs然后再次访问images看是否能够访问 [rootnginx images]# cd .. [rootnginx html]# ls 50x.html images index.html [rootnginx html]# mv images imgs [rootnginx html]# ls 50x.html imgs index.html无法访问 然而我们并不能因为更改了域名就让用户无法通过原地址访问所以我们就需要用到rewrite [rootnginx conf]# vim nginx.conf [rootnginx conf]# cat nginx.conf . . . . . location /images {rewrite ^/images/(.*)$ /imgs/$1 break;} . . . . . [rootnginx conf]# nginx -s reload再次访问访问成功 如果想让URL跳转到网络上的某一个网站也是同样的配置方法 [rootnginx conf]# vim nginx.conf [rootnginx conf]# cat nginx.con . . . . . location /images {rewrite ^/images/(.*)$ https://www.bing.com/images/search?q%E5%B0%8F%E7%8B%97%E5%9B%BE%E7%89%87formIQFRBAid52207A2CC7D8B74EFE142A76A4CDE2B26A8594C3first1disoverlay1 break;} . . . . . [rootnginx conf]# nginx -s reload成功跳转 flag中last和break的用法 先通过images跳转到imgs然后再次请求请求发现还有一个URL跳转最终跳转到百度的页面break停止规则检查 [rootnginx conf]# vim nginx.conf [rootnginx conf]# cat nginx.conf . . . . .location /images {rewrite ^/images/(.*)$ /imgs/$1 last;}location /imgs {rewrite ^/imgs/(.*)$ /ftx/$1 last;}location /ftx {rewrite ^/ftx/(.*)$ http://baidu.com break;} 成功访问百度 转存中…(img-Ap4VKcmL-1697727432436)] flag中last和break的用法 先通过images跳转到imgs然后再次请求请求发现还有一个URL跳转最终跳转到百度的页面break停止规则检查 [rootnginx conf]# vim nginx.conf [rootnginx conf]# cat nginx.conf . . . . .location /images {rewrite ^/images/(.*)$ /imgs/$1 last;}location /imgs {rewrite ^/imgs/(.*)$ /ftx/$1 last;}location /ftx {rewrite ^/ftx/(.*)$ http://baidu.com break;} 成功访问百度
http://www.yutouwan.com/news/217588/

相关文章:

  • 网站建设财务计划与预测软件开发学院
  • 网页升级紧急通知页面seo服务商
  • 域名网站负责人的责任wordpress每页不显示文章
  • 汇泽网站建设asp网站下载
  • 顺德网站建设要多少钱木藕设计网
  • 郑州手机网站搭建免费白嫖国外服务器app
  • 龙华响应式网站建设唐山哪个公司可以建网站
  • 烟台网站制作公司在线咨询怎么自己开发app软件
  • 网站友链查询接口梅州高铁
  • 电子商务如何做网站销售启航做网站好吗
  • 潍坊企业网站制作wordpress 链接失效
  • 沈阳做网站优秀公司制作图片工具
  • 电子商务企业网站建设计划书泰安网站建设泽讯
  • 百度网站官方认证怎么做温州网络推广平台建设
  • 济南网站建设q479185700惠网站开发的案例分析模板
  • 自己建的网站有乱码wordpress中文网站
  • 软文网站外包全球网站域名
  • 案例学 网页设计与网站建设关于房子的最新政策
  • php开源网站 网上商城卖房网站排名
  • 公司设计网站有哪些辽宁省网站备案注销
  • 中国建设规划采购网站公司培训网站需要广播证吗
  • 简易购物网站前端模板设计公司是做什么的
  • 南明区住房和城乡建设局网站上网站关键词快速排名
  • 福州网站建设新闻十大最好的网站
  • vue适合做门户网站吗服装订单接单网站
  • 苏州吴江做网站长沙传媒公司排名
  • 做黑彩票的网站赚钱深圳建筑工程交易网
  • 手机网站实例建设通官网入口
  • 网站建设狼盾网络ps培训机构排名
  • 隆昌网站建设项城网站建设