【问题标题】:Redirect an Nginx Vhost/URL unless on a specific URL?除非在特定的 URL 上,否则重定向 Nginx Vhost/URL?
【发布时间】:2014-02-16 12:45:25
【问题描述】:

是否有人将代码放入某个 Nginx 块中以用于以下场景。

我的网站通过 Cloudflare (url.com),但它们不支持超过 150mb 的上传,我们最多可以上传 300mb。他们的解决方案是创建一个未启用 Cloudflare 的子域 (upload.url.com) 并将其用于上传。我已经这样做了,没问题,并将上传页面的链接硬编码为通过上传 URL。现在的问题是,我的网站的链接只是“/page”,它通过upload.url.com/page 进行,Nginx 块中是否有一种方法可以设置它,所以任何要上传.url.com 的东西都可以工作在我希望它转到的上传页面上,如果他们点击返回到网站的任何其他部分,它将确保它通过“url.com”加载该页面?

【问题讨论】:

    标签: url file-upload nginx url-rewriting cloudflare


    【解决方案1】:
    server {
        server_name "upload.example.com";
    
        location ^~ /page {
            return 301 http://example.com$uri;
        }
    }
    

    或者,如果您的所有上传页面都可以通过 URL 中的特定部分进行识别。

    server {
        server_name "upload.example.com";
    
        location / {
            location ~* "^(?!.*?upload).*$" {
                return 301 http://example.com$uri;
            }
        }
    }
    

    或者,如果您只有一个有效位置。

    server {
        server_name "upload.example.com";
    
        location = /upload-script {
            # Code to process upload
        }
    
        # Redirect anything else!
        location / {
            return 301 http://example.com$uri;
        }
    }
    

    【讨论】:

    • 如何在第二个示例中输入特定的 URL?例如,如果我的 url 是“upload.example.com/upload-file”,我将如何将其列入白名单作为访问对象以及将其他任何内容重定向到 example.com 版本
    • 扩展了我的答案以包括一个示例。我认为甚至可以省略周围的 location / {} 块,但您必须对其进行测试。
    • 谢谢,我想最后一个是我可以使用的,我可以在位置放置多个网址吗?还是我需要使用多个位置?还有一个 /upload-page 和一个 /admin-upload 也可以看到,它只是这两个页面。虽然我有点担心,因为它们是多页,它可能会在 url 中添加字符,所以也许 /upload-script* ?
    • 啊,好吧,我可能应该扩展一下,我已经将我的主机文件设置为... server { listen 80; server_name url.com www.url.com upload.url.com;根 /var/www/vhosts/production/current;索引 index.html 索引.htm;字符集 utf-8;
    • 无论如何要在该配置中进行更改,还是应该将 upload.url.com 设为单独的配置文件并将其指向同一目录?因为我不能在这里让另一个服务器阻塞到特定域,所以只能重定向......
    【解决方案2】:

    我不确定我得到了你想要的东西,你想将任何不是根的 URI 重定向到主域,试试这个

    server {
      server_name upload.example.com;
      location = / {
        #pass to the upload script;
      }
      location / {
        # redirect to the main domain
        return 301 http://example.com$request_uri$is_args$query_string;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-06-01
      • 1970-01-01
      • 2018-12-15
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多