【问题标题】:django behind subdomain on localhost with nginxdjango 使用 nginx 在 localhost 上的子域后面
【发布时间】:2014-08-18 19:50:00
【问题描述】:

我有一个 django 项目,我想根据它正在访问的子域显示不同的内容,例如,当用户输入 url:http://e1.example.com/ 它显示 «Welcome to e1 site»,当用户输入:http://e2.example.com 时会显示«Welcome to e2 site»等等。

所以我有这个 nginx 配置:

upstream example_project{
    server localhost:8200;
}

server {
    listen  80;
    server_name ~^(?<event>.+)\.example\.com;

    location / {
        proxy_pass http://example_project/$event$request_uri;
    }
}

我有我的/etc/hosts 文件,如下所示:

127.0.0.1   example.com
127.0.0.1   e1.example.com
127.0.0.1   e2.example.com

当我转到 http://localhost:8200/e1/ 时它可以工作,但是当我转到 http://e1.example.com/ 时它显示 400 错误

在 django 控制台输出我有:

[18/Aug/2014 14:33:31] "GET /e1/ HTTP/1.1" 200 39 <-- this when going localhost:8200/e1/
[18/Aug/2014 14:33:33] "GET /e1/ HTTP/1.0" 400 26 <-- this when going e1.example.com

所以我可以看到e1.example.com 正在调用我的项目正确的 URL,但是为什么浏览器会显示 400 错误?

【问题讨论】:

    标签: python django nginx localhost subdomain


    【解决方案1】:

    好的,我意识到我必须将更多代理参数放入 nginx,现在我的 nginx 配置如下:

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_pass http://example_project/$event$request_uri;
    }
    

    end allthnig 工作正常。 也许这可以帮助任何打算做类似事情的人。 :)

    【讨论】:

      猜你喜欢
      • 2012-04-23
      • 2013-06-12
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2016-11-12
      • 2016-09-05
      • 2020-03-02
      • 1970-01-01
      相关资源
      最近更新 更多