简体   繁体   English

nginx 重写特定位置的规则

[英]nginx rewrite rule for a specific location

I have nginx server and want to rewrite my url who comes from a PHP script located in the index.php.我有 nginx 服务器,想重写来自 index.php 中的 PHP 脚本的 url。

I get a URL from the script that doesn't really exist on my server.我从我的服务器上并不真正存在的脚本中获得了一个 URL。

Like the follow example: mydomain.tld/location/that/doesn/t/exist/ then I rewrite this url who doesn't exist with a rewrite rule in my nginx conf-file like following:像下面的例子一样: mydomain.tld/location/that/doesn/t/exist/然后我用我的nginx conf文件中的重写规则重写这个不存在的url,如下所示:

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

Then I can see the welcome.php file.然后我可以看到welcome.php 文件。 This works like a charm.这就像一个魅力。

But If I want to adapt it for a specific location like this:但是,如果我想针对这样的特定位置进行调整:

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

I always get am 404 Not found error.我总是收到 404 Not found 错误。

Log output doesn't really help me.日志输出并没有真正帮助我。

2018/06/23 17:57:06 [error] 29928#29928: *1 "/var/www/html/mydomain.tld/anotherfolder/location/that/dosen/t/exist/index.php" is not found (2: No such file or directory) client:127.0.0.1, server: mydomain.tld, request: "GET /anotherfolder/location/that/dosen/t/exist/ HTTP/2.0", host: "mydomain.tld" 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”

I don't get it, why it works in my root location (/var/www/html/mydomain.tld/) but not in the other one.我不明白,为什么它在我的根位置(/var/www/html/mydomain.tld/)中有效,但在另一个位置无效。

Please help me to find my mistake.请帮我找出我的错误。

Add this to your nginx conf-file into the " location /anotherfolder " segment将此添加到您的 nginx conf 文件中的“ location /anotherfolder ”部分

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM