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

哈尔滨网站开发渠道建设银行理财产品网站

哈尔滨网站开发渠道,建设银行理财产品网站,网站双域名 登陆,莱阳有网站建设推广的吗概要: 在 Nginx 的众多功能模块中,ngx_http_rewrite_module是实现请求动态处理的核心组件,它通过一系列指令实现 URI 重写、条件判断、响应返回等功能。本文将以 CentOS 7.9 环境为例(主机名www.a.com,IP 172.25.0.10…

概要:

在 Nginx 的众多功能模块中,ngx_http_rewrite_module是实现请求动态处理的核心组件,它通过一系列指令实现 URI 重写、条件判断、响应返回等功能。本文将以 CentOS 7.9 环境为例(主机名www.a.com,IP 172.25.0.100,网站数据目录 /data/sitea/),详细解析break、if、return、rewrite、set五大指令的使用方法,并提供完整的配置验证流程。

环境准备与配置规范

基础环境搭建

在 CentOS 7.9 系统中,首先需要完成 Nginx 的安装与基础配置:

# 安装Nginxsudo yum install -y nginx# 创建网站数据目录sudo mkdir -p /data/sitea/{html,static,mobile,api/v1,api/v2}# 准备测试文件sudo echo "SiteA Home" > /data/sitea/html/index.htmlsudo echo "Mobile Version" > /data/sitea/html/mobile/index.htmlsudo touch /data/sitea/static/style.csssudo echo "API V1" > /data/sitea/api/v1/test.txtsudo echo "API V2" > /data/sitea/api/v2/test.txtsudo echo "API root index" > /data/sitea/api/index.html# 创建虚拟主机配置文件sudo mkdir -p /etc/nginx/conf.d/vhostssudo nano /etc/nginx/conf.d/vhosts/a.com.conf

基础虚拟主机配置

在a.com.conf中添加基础配置:

server {listen 80;server_name www.a.com;root /data/sitea/html;index index.html;location /static/ {root /data/sitea;}location /api/ {root /data/sitea;}
}

环境验证

完成基础配置后,通过以下命令验证环境:


# 检查配置语法nginx -t# 重载Nginx配置nginx -s reload# 测试访问(需在客户端修改hosts文件添加172.25.0.100 www.a.com)curl http://www.a.com# 应返回"SiteA Home"

break 指令:终止请求处理流程

指令语法与作用

break指令用于立即终止当前location或server块内的后续指令执行,不再进行 URI 重写或位置匹配,语法非常简单:

                break;

典型应用场景

  • 静态资源路径直接响应,避免执行多余逻辑
  • 条件判断后提前终止请求处理

实战配置示例

在静态资源路径中添加break指令:

server {listen 80;server_name www.a.com;root /data/sitea/html;index index.html;location /static/ {root /data/sitea;expires 30d;break;invalid_directive;}location /api/ {root /data/sitea;}
}

验证步骤

  1. 重载 Nginx 配置:reload nginx
  1. 访问静态资源并查看响应头:
curl -I http://www.a.com/static/style.css

 

预期输出应包含Expires字段,且由于break指令的存在,该location内的其他错误指令不会影响执行(可故意添加错误指令测试)。

if 指令:条件判断的核心工具

语法与条件类型

if指令通过条件表达式控制请求处理流程,语法为:

if (condition) { ... }

if (condition) { ... }
        条件满足时,执行配置块中的配置指令; server, location
        condition:
          比较操作符:
                = 相同 != 不同
                
                ~ 模式匹配,区分字符大小写
                ~* 模式匹配,不区分字符大小写
                !~ 模式不匹配,区分字符大小写
                !~* 模式不匹配,不区分字符大小写
                
            文件及目录存在性判断:
                -e, !-e 存在与否( 包括文件,目录,软链接)
                -f, !-f 文件 -d, !-d 目录 -x, !-x 执行

支持的条件类型包括:

  • 变量判空:if ($variable)(变量值为空或 "0" 时为假)
  • 字符串比较:if ($variable = value) 或 if ($variable != value)
  • 正则匹配:if ($variable ~ pattern)(区分大小写)、if ($variable ~* pattern)(不区分大小写)
  • 文件检查:if (-f $filename)(文件存在)、if (-d $dirname)(目录存在)等

return 指令:快速返回响应结果

指令语法与功能

return指令用于直接返回 HTTP 状态码和响应内容,终止请求处理,语法为:

return code [text|url];

典型应用场景

  • 域名重定向(301/302)
  • 禁止特定 IP 访问(403)
  • 维护模式返回(503)

应用场景实例

server {listen 80 default_server;server_name www.a.com;root /data/sitea/html;index index.html;access_log /var/log/nginx/access-www.a.com.log main;# 首页:移动端跳转逻辑location = / {if ($http_user_agent ~* "android|iphone") {return 302 /mobile/;}try_files $uri $uri/ /404.html;}# 静态资源location /static/ {root /data/sitea;}# API 路由,并检查 id 参数格式location /api/ {root /data/sitea;set $invalid_id 0;if ($arg_id ~ ".+") {set $invalid_id 1;}if ($arg_id ~ ^\d+$) {set $invalid_id 0;}if ($invalid_id = 1) {return 400 "Invalid ID Format";}}# 移动页面location /mobile/ {root /data/sitea/html;index index.html;}# 其他路径 fallbacklocation / {try_files $uri $uri/ /404.html;}
}
 

验证方法

curl -I http://www.a.com/            # 应该返回 200 页面为curl http://www.a.com/               # 页面显示“SiteA Home”curl -I -H "User-Agent: iPhone" http://www.a.com/   #跳转移动端页面 返回302curl http://www.a.com/api/?id=abc    # 应该返回 400curl http://www.a.com/api/?id=123    # 应该返回 200curl http://www.a.com/mobile/        # 应该返回 index.htmlcurl http://www.a.com/maintenance=1  # 返回500 最终显示404

rewrite 指令:URI 重写的核心能力

rewrite 指令简介

​​​​​​​rewrite regex replacement [flag];
  • regex:正则表达式,用来匹配原始 URI;

  • replacement:用于替换的 URI;

  • flag:可选参数,常用有:

    • last:停止 rewrite,重新进入新的 location 匹配流程;

    • break:停止 rewrite,但不重新匹配 location;

    • redirect:返回 302 临时重定向;

    • permanent:返回 301 永久重定向。

应用场景配置

server {listen 80 default_server;server_name www.a.com;root /data/sitea;index index.html index.php;access_log /var/log/nginx/access-www.a.com.log main;########################################  301/302 Redirect Rewrite######################################## 永久跳转 old-about → about.htmllocation = /old-about {rewrite ^ /about.html permanent;}# 临时跳转 beta → static 页面location = /beta {rewrite ^ /static/beta/index.html redirect;}########################################  break 内部 rewrite######################################## /doc → /static/docs/index.html,地址栏不变location = /doc {rewrite ^ /static/docs/index.html break;}########################################  首页移动端重定向#######################################location = / {if ($http_user_agent ~* "android|iphone") {return 302 /mobile/index.html;}try_files $uri $uri/ /404.html;}########################################  静态资源路径#######################################location /static/ {root /data/sitea;}########################################  RESTful API Rewrite#######################################location ~ ^/api/user/(\d+)$ {rewrite ^/api/user/(\d+)$ /api/v1/user.php?id=$1 last;}location ~ ^/api/product/(\d+)/detail$ {rewrite ^/api/product/(\d+)/detail$ /api/v1/product_detail.php?id=$1 last;}location ~ ^/api/order/(\d+)$ {rewrite ^/api/order/(\d+)$ /api/v1/order.php?id=$1 last;}########################################  通用 API 入口 + 参数校验#######################################location /api/ {root /data/sitea;index index.php;# 参数 id 格式校验set $invalid_id 0;if ($arg_id ~ ".+") {set $invalid_id 1;}if ($arg_id ~ ^\d+$) {set $invalid_id 0;}if ($invalid_id = 1) {return 400 "Invalid ID Format";}try_files $uri $uri/ /404.html;}########################################  移动页面#######################################location /mobile/ {root /data/sitea/html;index index.html;}########################################  默认 fallback,支持 .html 补全#######################################location / {try_files $uri $uri.html $uri/ /404.html;}########################################  PHP 处理 via FastCGI#######################################location ~ \.php$ {root /data/sitea;fastcgi_pass unix:/run/php-fpm/www.sock;fastcgi_index index.php;include fastcgi_params;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;}
}

 目录结构应如下:

/data/sitea/
├── index.html
├── about.html
├── mobile/index.html
├── static/docs/index.html
├── static/beta/index.html
├── api/v1/user.php
├── api/v1/order.php
└── api/v1/product_detail.php
#php文件自行配置

 

测试命令:# 301 永久跳转
curl -I http://www.a.com/old-about# 302 临时跳转
curl -I http://www.a.com/beta# break 内部转向
curl http://www.a.com/doc# 自动补 .html
curl http://www.a.com/about# RESTful API
curl http://www.a.com/api/user/123
curl http://www.a.com/api/product/456/detail
curl http://www.a.com/api/order/789


文章转载自:

http://YtB8rj6m.zcnfm.cn
http://CzMJyvbo.zcnfm.cn
http://J8zcpd1Q.zcnfm.cn
http://IR4cgChm.zcnfm.cn
http://d81BfbRj.zcnfm.cn
http://WvEnxPxH.zcnfm.cn
http://ndHRqxIr.zcnfm.cn
http://7bKYoEqN.zcnfm.cn
http://zD0TtHSi.zcnfm.cn
http://XuVkLrCi.zcnfm.cn
http://x2vONfqz.zcnfm.cn
http://5N4fn7aH.zcnfm.cn
http://6no2bkI2.zcnfm.cn
http://ZJA955X0.zcnfm.cn
http://Q8UxjlgU.zcnfm.cn
http://I7a1VV1A.zcnfm.cn
http://qoT8aV9y.zcnfm.cn
http://c6xBZ8a5.zcnfm.cn
http://IPb1hNzx.zcnfm.cn
http://e0UaY1xs.zcnfm.cn
http://tCvNycDP.zcnfm.cn
http://vgQTYgdR.zcnfm.cn
http://6S6XLUMB.zcnfm.cn
http://3rUS7VgM.zcnfm.cn
http://AS1byH8b.zcnfm.cn
http://uJQoYqxl.zcnfm.cn
http://K6XfIgov.zcnfm.cn
http://9UUqnO9U.zcnfm.cn
http://DoIQq8SO.zcnfm.cn
http://tUsNxfO5.zcnfm.cn
http://www.dtcms.com/wzjs/604817.html

相关文章:

  • 重庆网站建设公司 夹夹虫好做响应式网站的价格
  • 山东网站建设服务外贸网站建站用什么意思
  • 网站建设大小免费网站服务器推荐
  • 备案后怎么建设网站flash网站制作教程
  • 建网站能挣钱吗住房建设危房改造网站
  • 南涧县城乡建设局网站宁波网站优化技术
  • 银川网站开发公司seo是做网站源码还是什么
  • 合肥网站建设方案维护企业做网站有哪些好处
  • 青岛网站建设服务器做网站设计工作的报告
  • 揭阳网站制作多少钱网站做哪家最专业广州
  • 温州建网站业务人员东莞网站建设制作软件
  • 手机商城系统哪个好网站seo优化推广怎么做
  • 建设网站用凡科怎么样长春 行业网站
  • 西安西郊网站建设网址域名是什么
  • 宜昌市住房和城乡建设局网站免费网站登陆模板
  • 遵义晚报电子版官方网站装修效果图免费软件
  • 天商阳光网站邮箱项目网上备案查询系统
  • 建设银行贵阳市网站电话wordpress如何添加目录菜单
  • 淘宝联盟+做网站wordpress博客缩略图
  • wordpress 作品展福州seo招聘
  • 做婚庆网站有哪些内容杭州发布官网
  • 网店美工设计论文爱站seo工具包免费版
  • 遂昌建设局网站河南郑州网站制作公司
  • 网站建设与维护成本网站做会员系统
  • 衡水微信网站建设微商营销宝最新版
  • 网站建设kaicz大型门户网站的建设外包在本公司制作好还是
  • 南京企业网站设计公司十大网站建设公司
  • 私人订制旅游网站建设制作一个网站难吗
  • 深圳网站建设有限公司企业门户是什么意思
  • 广东企业网站模板推荐公众号平台注册