php - IIS 到 nginx 迁移 : `unexpected " {"` erron on\d{^anum^, ^anum^} 条目到映射文件

标签 php azure iis nginx

我的任务是将在 azure/iis 中运行的 php 应用程序迁移到 GNU+Linux 机器上的 nginx+fpm 组合中。

web.config 包含:

<rule name="Some match">
<match url=".*"/>
<conditions>
 <add input="{URL}" pattern="^power_ranger/(\d{4,9})$" />
</conditions>
<action type="Rewrite" url="/power_ranger.php?ranger={C:1}"/>
</rule>

然后按照 https://serverfault.com/questions/441235/maintaining-redirects-in-nginx-from-an-external-source#441517 中的说明进行操作我创建了以下名为 rewrites.map 的 map 文件:

^power_ranger/(\d{4,9})$  /power_ranger.php?ranger=$2

在虚拟主机配置上,我添加了以下内容:

map $uri $new {
       include /home/user/powerangerApp/rewrites.map;
}

if ($new) {
       rewrite ^ $new redirect;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/user/powerangerApp;
    index index.php;

    server_name _;

    location / {
        try_files $uri $uri/ =404;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

然后,当我尝试重新加载 nginx 时,出现以下错误:

nginx: [emerg] unexpected "{" in /home/user/powerangerApp/rewrites.map:1

所以我猜测 nginx 无法映射 PCRE 正则表达式。那么我怎样才能让 nginx 做到这一点呢?

编辑 1:

https://serverfault.com/questions/482372/nginx-httpmapmodule-regex-variables#482391中所示:

我尝试更改正则表达式:

?P^power_ranger/(\d{4,9})$  /power_ranger.php?ranger=$2

仍然遇到同样的错误。

编辑2

到目前为止,我发现我的 nginx 配置需要一个 if 条件:

 if ( $url ~* "^power_ranger/(\d{4,9})$"){
   rewrite ^ /power_ranger.php?ranger=$2
 }

但是我收到错误:

unknown directive ranger=$2 in /etc/nginx/nginx.conf:^some_number^

你们知道如何解决这个问题吗?

最佳答案

正则表达式应更改为:

~\/power_ranger\/(\d{4,9})$  /power_ranger.php?ranger=$1;

正如您所看到的,存在语法错误。

nginx.conf 也应该是:

map $uri $new {
       include /home/user/powerangerApp/rewrites.map;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/user/powerangerApp;
    index index.php;

    server_name _;

    location / {
        # On way too many rewrite rules you may avoit the line above
        try_files $uri $uri/ =404; 

        if ($new) {
          rewrite ^ $new redirect redirect;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

请记住,重写时的重定向可能被证明是完成工作的救世主。

关于php - IIS 到 nginx 迁移 : `unexpected " {"` erron on\d{^anum^, ^anum^} 条目到映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46587215/

相关文章:

azure - 如何从 Kusto.Explorer 查询我的 Application Insights 日志(分析)?

c# - 在 IIS 中托管 ASP.NET Core 时不会动态压缩响应

c# - ASP.NET Core 2.0 - 部署到 IIS

.net - IIS网址重写不起作用-404错误

php - 单击提交表单后找不到页面/对象

php - 如何防止web socket DDOS攻击?

php - 'a.Email Address' 错误 MySQL 中的未知列 'field list'

php - Codeigniter 找不到 url 抛出 404 错误

Azure存储: Error checking for existence of existing Storage Share

Azure API应用程序代理生成错误