【问题标题】:nginx rewrite & parameter full-url and file extensionsnginx 重写 & 参数完整 url 和文件扩展名
【发布时间】:2016-04-26 16:18:11
【问题描述】:

我会尽量简短。我有以下 nginx 重写 url:

rewrite ^/(.*)?$ /index.php?completeURL=$1 last;

我想要一个类似的网址:

http://mywebsite.com/http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1

请求:

http://mywebsite.com/index.php?completeURL=http://www.otherwebsite.com/dir1/dirx/article.php&id=2&category=1

目前nginx规则有问题。 示例:如果参数包含 .php 扩展名,他会在我的服务器上查找该文件。

示例:http://mywebsite.com/dir1/dirx/article.php

您认为我该如何解决这个问题?


更新: 这里是 nginx 配置(和重写)文件:

【问题讨论】:

    标签: nginx


    【解决方案1】:

    最简单的解决方案(假设服务器除了提供index.php 之外什么都不做)是删除通常的location ~ \.php$ 块并在与fastcgi_pass 相同的块中执行rewrite ... break;。有多种方法可以实现这一点,包括:

    location / {
        rewrite ^/(.*)?$ /index.php?completeURL=$1 break;
        fastcgi_pass ...
        ...
    }
    

    另一种策略是仅在本地文件不存在时执行重写,但您需要确保.php 文件也经过测试。例如:

    location / {
        try_files $uri $uri/ @rewrite;
    }
    location ~ \.php$ {
        try_files $uri @rewrite;
        fastcgi_pass ...
        ...
    }
    location @rewrite {
        rewrite ^/(.*)?$ /index.php?completeURL=$1 last;
    }        
    

    【讨论】:

    • 我尝试了您的第二个解决方案,但出现此错误:2016/04/26 22:21:05 [error] 10591#0: *1846 rewrite or internal redirection cycle while redirect to named location "@rewrites", client: xx.xx.xx.xx, server: website.com, request: "GET /http://www.website.com/dfgdfgghgh.cfm HTTP/2.0", host: “website.com”
    • 如果 /index.php 不存在,它将循环。 'root' 定义是否正确?
    • 嗨@Richard Smit。我更新了我的帖子。我已经包含了完整的 nginx 配置文件。试着给我们看看。
    • 你能看看我贴的nginx规则吗?你至少还有一个建议给我?非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 2023-04-05
    相关资源
    最近更新 更多