【问题标题】:Rewrite `example.com/foo/bar` to `foo.example.com/bar` with nginx. Redirect loop使用 nginx 将 `example.com/foo/bar` 重写为 `foo.example.com/bar`。重定向循环
【发布时间】:2016-01-24 19:23:19
【问题描述】:

我有一个 django 应用程序,它在 iframe 中提供一些用户内容。为避免会话劫持,我想从不同的子域提供此服务。

我目前将网站托管在subdomain.example.com,特定 iframe 内容位于subdomain.example.com/foo/bar/ID。我现在正在尝试设置 nginx 以使该特定内容可以通过 foo.example.com/bar/ID 访问。

我的 nginx 设置:

server {
    listen 80;
    server_name subdomain.example.com;

    location / {
        include proxy_params;
        #Served with gunicorn:
        proxy_pass http://unix:/home/example/example.sock;
    }
}

server {
    listen 80;
    server_name foo.example.com;

    location / {
        include proxy_params;

        #This is key:
        rewrite ^/(.*?) /foo$request_uri last;

        #Served with gunicorn:
        proxy_pass http://unix:/home/example/example.sock;
    }
}

这给了我在 nginx error.log 中的以下错误:

2016/01/24 14:10:41 [错误] 24286#0: *17 在处理“/foo/bar/66”时重写或内部重定向周期,客户端:xx.xx.xx.xx,服务器: foo.example.com,请求:“GET /bar/66 HTTP/1.1”,主机:“foo.example.com”

这是有道理的;重定向一次又一次地被重定向,因为它匹配重写正则表达式。

我尝试添加

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;

无济于事。

如果没有重定向,它确实可以正确托管,但我想避免将 url 设为 foo.example.com/foo/bar/ID

有没有更好的方法来解决这个问题?或者有办法阻止重定向?

PS:奖金问题?接下来将阻止 subdomain.example.com/foo/bar 并因此强制通过 foo.example.com/bar 访问

编辑:我通过删除重写解决了这个问题,并使用 django-subdomains (https://django-subdomains.readthedocs.org) 处理 django 中的子域

【问题讨论】:

  • 你应该使用break 而不是last

标签: django nginx gunicorn


【解决方案1】:

这可能是您额外问题的解决方案(即如何从 www.example.com 阻止 /foo/bar): https://stackoverflow.com/a/4677893/2319697

如果模式/位置匹配,基本上只返回 404

【讨论】:

    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 2015-02-01
    相关资源
    最近更新 更多