【问题标题】:Use multiple WSGI mount points in Apache with an Nginx reverse proxy通过 Nginx 反向代理在 Apache 中使用多个 WSGI 挂载点
【发布时间】:2011-02-04 14:32:05
【问题描述】:

我正在尝试使用 Nginx 和 Apache 在同一台服务器上设置多个虚拟主机,但遇到了一个奇怪的配置问题。

我已经为 nginx 配置了 apache 的通用上游。

upstream backend {
  server 1.1.1.1:8080;
}

我正在尝试在 nginx 中设置多个子域,这些子域会在 apache 中命中不同的挂载点。每个都将像以下示例一样。

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

    location / {
            proxy_pass http://backend/bar/;
            include /etc/nginx/proxy.conf;
    }
    ...
}

server {
    listen 80;
    server_name delta.yoursite.com;

    location / {
            proxy_pass http://backend/gamma/;
            include /etc/nginx/proxy.conf;
    }
    ...
}

这些挂载点指向 django 项目,但是每个 url 条目都以 apache 挂载点路径开头。因此,如果我调用 foo.yoursite.com/wiki/biz/ 的 django url 条目,django 似乎返回 foo.yoursite.com/bar/wiki/biz/。同样,如果我调用 delta.yoursite.com/wiki/biz/ 的 url 条目,我会得到 delta.yoursite.com/gamma/wiki/biz/。

有什么办法可以摆脱 django 和 apache 在 url 条目上返回的前缀?

【问题讨论】:

    标签: python django apache nginx wsgi


    【解决方案1】:

    最简单的方法是在 WSGI 脚本文件中使用以下内容:

    ... existing stuff
    
    import django.core.handlers.wsgi
    _application = django.core.handlers.wsgi.WSGIHandler()
    
    def application(environ, start_response):
        # Wrapper to clear SCRIPT_NAME..
        environ['SCRIPT_NAME'] = ''
        return _application(environ, start_response)
    

    问题的出现是因为每个服务器安装在不同的 URL。因此,您必须通过清除 SCRIPT_NAME 让后端认为它实际上已安装在服务器的根目录。

    请注意,如果您还直接访问后端,这将导致问题。在这种情况下,您需要在上面进行修改以仅在请求来自代理时才执行此操作。

    【讨论】:

    • 清除 SCRIPT_NAME 环境变量就可以了。谢谢,格雷厄姆!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-16
    • 1970-01-01
    • 2021-07-06
    • 2017-09-19
    • 2015-04-01
    • 2020-01-07
    • 1970-01-01
    相关资源
    最近更新 更多