AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 653423
Accepted
Jeff Widman
Jeff Widman
Asked: 2014-12-19 01:35:25 +0800 CST2014-12-19 01:35:25 +0800 CST 2014-12-19 01:35:25 +0800 CST

Nginx:如何重写传递给 index.php 的参数?

  • 772

我正在为传递给 index.php 的 url 参数进行一些 Nginx 重写:

  • 老的:?topic=10126.msg36887
  • 新的:?posts/36887/

我的两个问题:

1)我如何重写新的参数结构,因为它使用“参数/值/”而不是传统的“参数=值”结构?

2)我的“if”语句没有触发,我不知道为什么......测试urldomain.com/forum/index.php?topic=10126.msg36887应该重定向到/success,但它根本没有被重写。

这是我当前的 Nginx 配置:

location /forum/ {
    index index.php index.html index.htm;
    try_files $uri $uri/ /forum/index.php?$uri&$args;

    location /forum/index.php {

        # I know Nginx 'If is Evil', but it's the only way 
        # to trigger rewrites on url parameters
        if ($arg_topic ~ [0-9]+\.\bmsg([0-9]+) {

            # testing whether 'if' triggers:
            rewrite ^ /success? redirect;

            # full rewrite:
            # rewrite ^\/forum\/index\.php ^\/forum\/index\.php\?posts\/$1\/? redirect;

            }
        }
    }

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass    127.0.0.1:9000;
    fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include         fastcgi_params;
    }
nginx
  • 2 2 个回答
  • 17270 Views

2 个回答

  • Voted
  1. Best Answer
    John Daniels
    2014-12-19T10:06:24+08:002014-12-19T10:06:24+08:00

    将它放在server配置块中应该可以工作:正则表达式应该正确匹配您需要的内容,并且通过将所需的 ID 存储在变量中,您可以稍后在重写中使用它。

    假设您在站点的其他部分没有“主题”参数,则实际上没有必要将其范围限定在某个位置 - 即使您这样做,您也可以更改重写的第一部分以匹配 /forum/index仅 .php。

    if ($arg_topic ~ [0-9]+\.msg([0-9]+)$) {
      set $postid $1;
      rewrite ^ /forum/index.php?posts/$postid? last;
    }
    
    • 3
  2. Vadim
    2014-12-21T12:50:00+08:002014-12-21T12:50:00+08:00

    如果它只是关于“主题”论点,这可能有效:

    在 nginx.conf 的“http”级别声明以下“地图”:

    map $arg_topic $topic_id {
        "~^\d+\.msg(?<id>\d+)" $id;
        default 0;
    }
    

    在“服务器”级别添加适当的“位置”:

    location = /forum/index.php {
    
                #if ($topic_id = 0) {
                #      return 403;
                #}
    
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param QUERY_STRING posts/$topic_id/;
                fastcgi_pass 127.0.0.1:9000;
    }
    

    确保您在服务器级别指定了“root”指令,以便“SCRIPT_FILENAME”填充正确的值。

    您也可以检查 '$topic_id' 是否为零(例如,topic_id= 缺失或值不正确)。

    使用这个你的 '$_GET' 数组将包含如下内容:

    array(1) {
      ["posts/36887/"]=>
      string(0) ""
    }
    
    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve