Django nginx子域和域重写w代理传递

Django nginx子域和域重写w代理传递,django,proxy,nginx,rewrite,Django,Proxy,Nginx,Rewrite,我需要这两种类型的重写: subdomain.domain.com=>domain.com/website/subdomain otherdomain.com=>domain.com/userdomain/otherdomain.com 我的问题是,我希望用户看到的是subdomain.domain.com,以及otherdomain.com,而不是重定向版本。我目前在nginx中的重写工作正常,但用户的URL显示了重写,我希望这对用户是透明的,有什么想法吗 upstream domain_se

我需要这两种类型的重写:

subdomain.domain.com=>domain.com/website/subdomain

otherdomain.com=>domain.com/userdomain/otherdomain.com

我的问题是,我希望用户看到的是
subdomain.domain.com
,以及
otherdomain.com
,而不是重定向版本。我目前在nginx中的重写工作正常,但用户的URL显示了重写,我希望这对用户是透明的,有什么想法吗

upstream domain_server { server localhost:8000 fail_timeout=0; }     

server {
        listen  80;
        root  /var/www/domain.com;

        server_name domain.com ~^(?<subdomain>.*)\.domain\.com$ ~^(?<otherdomain>.*)$;
        if ( $subdomain ) {
                rewrite ^ http://domain.com/website/$subdomain break;
        }
        if ( $otherdomain ) {
                rewrite ^ http://domain.com/userdomain/$otherdomain break;
        }

        location / {
                proxy_redirect off;
                proxy_buffering off;
                proxy_set_header Host $http_host;
                proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
                if (!-f $request_filename) {
                        proxy_pass http://domain_server;
                        break;
                }
        }

}
上游域\u服务器{server localhost:8000失败\u超时=0;}
服务器{
听80;
root/var/www/domain.com;
服务器名称domain.com~^(?.*\.domain\.com$~^(?.*)$;
if($子域){
重写^http://domain.com/website/$subdomain break;
}
如果($otherdomain){
重写^http://domain.com/userdomain/$otherdomain中断;
}
地点/{
代理_重定向关闭;
代理缓冲关闭;
代理设置头主机$http\U主机;
proxy\u set\u header X-forwarded-for$proxy\u add\u X\u forwarded\u for;
如果(!-f$request\u文件名){
代理通行证http://domain_server;
打破
}
}
}

使用nginx,您根本不需要重写

upstream domain_server { server localhost:8000 fail_timeout=0; }

proxy_set_header Host domain.com;
proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;

server {
    listen  80 default_server;

    location / {
        proxy_pass http://domain_server/userdomain/$http_host;
    }
}

server {
    listen  80;
    server_name domain.com;

    root  /var/www/domain.com;

    location / {
        try_files $uri @backend;
    }

    location @backend {
        proxy_pass http://domain_server;
    }
}

server {
    listen  80;
    server_name ~^(?<subdomain>.+)\.domain\.com$;

    location / {
        proxy_pass http://domain_server/website/$subdomain$request_uri;
    }
}
上游域\u服务器{server localhost:8000失败\u超时=0;}
proxy_set_header Host domain.com;
proxy\u set\u header X-forwarded-for$proxy\u add\u X\u forwarded\u for;
服务器{
监听80个默认_服务器;
地点/{
代理通行证http://domain_server/userdomain/$http_主机;
}
}
服务器{
听80;
server_name domain.com;
root/var/www/domain.com;
地点/{
试试看文件$uri@backend;
}
位置@后端{
代理通行证http://domain_server;
}
}
服务器{
听80;
服务器名称~^(?.+)\.domain\.com$;
地点/{
代理通行证http://domain_server/website/$subdomain$request\u uri;
}
}

在这里发现了一些有趣的东西:。它说“Nginx重写的另一件事是,默认情况下,它们是内部重写,这意味着它们不会更改浏览器看到的URI。只有在指定“重定向”或“永久”重写标志,或者重写到包含http://部分的绝对URL时,它们才会这样做。”。试图用这个来找到一个解决方案…感谢您的良好响应,但是,/var/www/domain.com/userdomain/$host不存在,但URL domain.com/userdomain/$host存在。这个想法是,当用户使用自己的指向服务器的域时,透明地将用户代理到该URL。使用代理时,pass仍然重定向浏览器URL,我仍在尝试不同的配置…因此,可能,它现在在应用程序端。您的应用程序必须知道如何生成适当的链接。我刚刚生成了一个新实例,并使用apache复制了此域配置,并且对同一应用程序没有任何问题。所以肯定是nginx配置。仍在处理:)另一个添加可能是添加
$request\u uri
,使其成为
http://domain_server/website/$subdomain$request\u uri/