【问题标题】:nginx rewrite rule for a specific location特定位置的 nginx 重写规则
【发布时间】:2018-12-02 19:47:14
【问题描述】:

我有 nginx 服务器,想重写我的 url,它来自 index.php 中的 PHP 脚本。

我从脚本中获得了一个在我的服务器上并不存在的 URL。

如以下示例:mydomain.tld/location/that/doesn/t/exist/ 然后我用我的nginx conf文件中的重写规则重写这个不存在的url,如下所示:

location / {
    rewrite ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /welcome.php last;
}

然后我可以看到welcome.php 文件。这就像一个魅力。

但如果我想将其调整为这样的特定位置:

location /anotherfolder {
    rewrite ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /another.php last;
}

我总是得到 am 404 Not found 错误。

日志输出对我没有帮助。

2018/06/23 17:57:06 [错误] 29928#29928: *1 "/var/www/html/mydomain.tld/anotherfolder/location/that/dosen/t/exist/index.php"未找到(2:没有这样的文件或目录)客户端:127.0.0.1,服务器:mydomain.tld,请求:“GET /anotherfolder/location/that/dosen/t/exist/HTTP/2.0”,主机:“mydomain .tld"

我不明白,为什么它可以在我的根位置 (/var/www/html/mydomain.tld/) 但不是在另一个。

请帮我找出我的错误。

【问题讨论】:

  • 您的 正则表达式 与 URI 不匹配。 /anotherfolder/location/that/dosen/t/exist/ 有六个路径元素,而 ^/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ 预计只有五个。
  • 文件夹“anotherfolder”确实存在,并且存在创建其他 5 个路径元素的 index.php。我想将 5 个路径元素重写为 mydomain.tld/anotherfolder/another.php

标签: php nginx mod-rewrite


【解决方案1】:

将此添加到您的 nginx 配置文件中的“location /anotherfolder”段中

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
}

【讨论】:

  • 当我将它添加到我的配置并访问 mydomain.tld/anotherfolder 时,index.php 会立即下载。当我访问 mydomain.tld/anotherfolder/location/that/dosen/t/exist/ 时,错误 404 Not found 仍然存在
猜你喜欢
  • 2016-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 2019-06-24
  • 1970-01-01
  • 2010-11-20
相关资源
最近更新 更多