【问题标题】:Multiple websites with one IP: settings with proxy server and proxy cache (flask, uwsgi, nginx)一个 IP 的多个网站:设置代理服务器和代理缓存(flask、uwsgi、nginx)
【发布时间】:2017-06-07 17:39:00
【问题描述】:

我想让我的 nginx 服务器为多个子域提供服务。 每个站点由一个flask + uwsgi服务,监听自己的端口。

所有站点都有许多同名端点,并且响应缓存在不同的区域中:我想从代理服务器提供正确的缓存(或正确的站点)。

我阅读了https://askubuntu.com/questions/766352/multiple-websites-on-nginx-one-ip:在我的配置中,我一直将 domain2 重定向到 domain1。 我找不到合适的配置来收听 uwsgi 并让代理服务器为正确的站点提供服务。

如何正确设置 proxy_server 上的端口和 proxy_cache 以允许 nginx 从单个服务器为两个烧瓶站点提供服务?

以下是我设置的当前设置:

配置域_1
server {        
    server_name www.domain1.com;
    return 301 $scheme://domain1.com$request_uri;
}

server {
    listen 8000 default_server;
    server_name domain1.com;

    root /var/www/example_site_1;

    # common locations for all sites
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/example_site_1/domain1.sock;
    }

    # API
    location /api {                     
        include uwsgi_params;
        uwsgi_param UWSGI_SCRIPT wsgi;
        uwsgi_pass unix:/var/www/example_site_1/domain1.sock;
    }
}



# Set cache directory for site

proxy_cache_path /tmp/nginx/domain1 levels=1:2 keys_zone=my_zone_domain_1:10m max_size=50m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";


# Virtualhost/server configuration
server {
    listen  80  default_server;
    server_name domain1;

    root /var/www/domain1;

    ## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ?

    location / {        
        proxy_cache my_zone_domain_1;    
        add_header X-Proxy-Cache $upstream_cache_status;
        include proxy_params;
        proxy_pass http://domain1.com:8000;
    }

    location /api {
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_cache my_zone_domain_1;
        proxy_pass http://domain1.com:8000/api;
    }

}
配置域_2
server {        
    server_name www.domain2.com;
    return 301 $scheme://domain2.com$request_uri;
}

server {
    listen 3000;
    server_name domain2.com;

    root /var/www/example_site_2;

    # common locations for all sites
    location / {
        include uwsgi_params;
        uwsgi_pass unix:/var/www/example_site_2/domain2.sock;
    }

    # API
    location /api {                     
        include uwsgi_params;
        uwsgi_param UWSGI_SCRIPT wsgi;
        uwsgi_pass unix:/var/www/example_site_2/domain2.sock;
    }
}



# Set cache directory for site

proxy_cache_path /tmp/nginx/domain2 levels=1:2 keys_zone=my_zone_domain_2:10m max_size=50m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";


# Virtualhost/server configuration
server {

    # I tried listening on other ports than 80, but kept having redirects on domain_1     

    listen  80;
    server_name domain2;

    root /var/www/domain2;

    ## how to serve proxy_cache if locations of domain_1 and domain_2 are the same ?

    location / {        
        proxy_cache my_zone_domain_2;    
        add_header X-Proxy-Cache $upstream_cache_status;
        include proxy_params;
        proxy_pass http://domain2.com:3000;
    }

    location /api {
        add_header X-Proxy-Cache $upstream_cache_status;
        proxy_cache my_zone_domain_2;
        proxy_pass http://domain2.com:3000/api;
    }

}

【问题讨论】:

    标签: caching nginx reverse uwsgi proxy-server


    【解决方案1】:

    您的 domain_2 配置使用 proxy_pass http://domain2.com:8000,但只有 domain_1 服务器在端口 8000 上侦听,因此它可以处理定向到 domain_2 的请求。

    我还建议您重新考虑在您的配置中使用 proxy_pass,这并不是必须的。

    【讨论】:

    • 我的错字:proxy_pass for location /api 监听两个不同的门(3000 和 8000),但它仍然将 domain_2 重定向到 domain_1。您能否详细说明重新考虑 proxy_pass 的用法?我的用例是为 domain.com/entity/one 等端点缓存带有响应 200 的 GET API(省略 domain.com/entity?q=one) - 并避免 domain_1 和 domain_2 之间的冲突,因为 apis 具有相同的名称。你想发布一个例子吗?
    • domain2 端口 80 配置的另一个问题是 server_name domain2; 不会匹配到 domain2.com 的请求,这些请求将由带有 server_name domain1 default_server;' as it has default_server 选项的服务器部分处理。你不需要proxy_pass,因为你可以使用uwsgi_pass,而不是在监听端口80的服务器部分,只需使用uwsgi_cache而不是proxy_cache
    • 不工作:我在第一个服务器块中从配置 domain_1 中删除了 default_server。现在server { listen 8000; server_name domain1; ... }server { listen 3000; server_name domain2; ... }server { listen 80; domain1; }server { listen 80; domain2; } 我也尝试了后者侦听端口8080 但没有成功:curl localhost:3000/api 继续在端口8000 上重定向。
    • 你的问题是 domain2 != domain2.com
    • 感谢 alex 的帮助,我在 server_names 上设置了正确的扩展名。我尝试让 domain1 和 domain2 的服务器块同时监听 80 和 8080,并按照指南中的描述为每个设置 default_server:It is possible to define servers listening on ports *:80 and *:8080, and direct that one will be the default server for port *:8080, while the other will be the default for port *:80,但没有成功。您可以通过聊天帮助解决此问题吗?
    【解决方案2】:

    我发现罪魁祸首是 Uwsgi:它是从 usr/bin 文件夹加载的,而不是从我的应用程序的虚拟环境文件夹加载的。

    可能 virtualenv 文件夹已损坏:当我尝试重新安装 uwsgi (pip install uwsgi) 时,它一直说满足要求,直到我注意到 which uwsgi 没有从 virtualenv 加载。

    我不得不删除并重新安装带有 uwsgi 和 python 模块的 virtualenv 文件夹。

    现在应用程序正在运行(问题的答案),但在我的情况下,proxy_server 配置必须进一步调整。

    以下内容可能对在烧瓶中使用url_for() 指令的人有用:url_for() 指令路由到绝对路径,这可能与 nginx 代理冲突。

    例子:nginx代理服务器监听domain2.com的80端口,proxy_pass/path位置到http://domain2.com:3000;如果flask 将路由重定向到/path(使用url_for()),则生成的url 将是http://domain2.com:3000/path(因为它遵循绝对路径中指定的端口)而不是http://domain2.com/path(代理的url)。

    proxy_set_header Host $http_host; 添加到/path 位置以允许 nginx 代理遵循您的烧瓶应用程序的正确重定向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2015-09-10
      • 1970-01-01
      相关资源
      最近更新 更多