【问题标题】:How to properly setup nginx as a reverse proxy for gunicorn and flask using subdomains, for both SSL and non-SSL configurations?对于 SSL 和非 SSL 配置,如何使用子域将 nginx 正确设置为 gunicorn 和 flask 的反向代理?
【发布时间】:2013-06-24 03:25:53
【问题描述】:

有人可以发布一个 nginx 配置文件,说明如何正确地将以下 URL 路由到 gunicorn:

  1. http://www.example.com
  2. https://www.example.com
  3. http://testing.example.com
  4. https://testing.example.com

一些问题:

  1. 为什么有些nginx配置文件中包含“上游命令”?
  2. 我正在运行 2N+1 个 gunicorn 工人。我还需要多个 nginx 工作人员吗?我的意思是,我什至应该使用“worker_processes”命令,因为 nginx 应该只提供静态文件?
  3. 如何设置缓冲/缓存?

【问题讨论】:

标签: python nginx flask gunicorn


【解决方案1】:
server {
    listen        80 default_server deferred;
    listen        443 default_server deferred ssl;
    listen        [::]:80 ipv6only=on default_server deferred;
    listen        [::]:443 ipv6only=on default_server deferred ssl;
    server_name   example.com www.example.com testing.example.com;
    root          /path/to/static/files

    # Include SSL stuff

    location / {

        location ~* \.(css|gif|ico|jpe?g|js[on]?p?|png|svg|txt|xml)$ {
            access_log                off;
            add_header                Cache-Control   "public";
            add_header                Pragma          "public";
            expires                   365d;
            log_not_found             off;
            tcp_nodelay               off;
            open_file_cache           max=16 inactive=600s; # 10 minutes
            open_file_cache_errors    on;
            open_file_cache_min_uses  2;
            open_file_cache_valid     300s; # 5 minutes
        }

        try_files $uri @gunicorn;
    }

    location @gunicorn {
        add_header                X-Proxy-Cache $upstream_cache_status;
        expires                   epoch;
        proxy_cache               proxy;
        proxy_cache_bypass        $nocache;
        proxy_cache_key           "$request_method@$scheme://$server_name:$server_port$uri$args";
        proxy_cache_lock          on;
        proxy_cache_lock_timeout  2000;
        proxy_cache_use_stale     error timeout invalid_header updating http_500;
        proxy_cache_valid         200 302 1m;
        proxy_cache_valid         301 1D;
        proxy_cache_valid         any 5s;
        proxy_http_version        1.1;
        proxy_ignore_headers      Cache-Control Expires;
        proxy_max_temp_file_size  1m;
        proxy_no_cache            $nocache;
        proxy_redirect            off;
        proxy_set_header          Host $host;
        proxy_set_header          X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header          X-Real-IP $remote_addr;
        proxy_pass                http://gunicorn;
    }
}

并回答您的其他问题:

  1. upstream 指令可用于简化 nginx 配置中的任何 *_pass 指令以及负载平衡情况。如果您有多个 gunicorn 服务器,您可以执行以下操作:
上游独角兽{ 服务器 http://gunicorn1; 服务器 http://gunicorn2; } 服务器 { 地点 { proxy_pass gunicorn; } }
  1. 如果您的 nginx 版本已经有 auto 选项,请将 nginx 的 worker_processes 设置为 auto。您的 nginx 的工作进程数量与您的 gunicorn 应用程序的工作进程无关。是的,即使您只提供静态文件,设置正确数量的工作进程也会增加您的 nginx 可以处理的请求总量,因此建议正确设置。如果您的 nginx 版本没有 auto 选项,只需将其设置为您的实际物理 CPU 计数或实际物理 CPU 核心计数。
  2. 我包含了一个示例配置,用于缓存来自您的 gunicorn 应用程序服务器的响应和基于 UNIX 的系统的打开文件缓存,用于静态文件。我认为如何设置很明显。如果您希望我详细解释任何特殊指令,只需发表评论,我将编辑我的答案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2021-03-05
    • 2020-04-26
    • 2014-09-06
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多