在dotcloud上的gunicorn,nginx(v 1.3.14),django和gevent-socket.io

t1m*_*1m0 13 django nginx gunicorn dotcloud gevent-socketio

我试图在dotcloud上在nginx(v 1.3.14)后面部署gunicorn + gevent.我有几个问题.我的目标是改编python-on-dotcloud示例.到目前为止,我还没有能够使用此设置使用websockets部分.换句话说,我必须手动刷新页面以获取更新,而不是通过socket.io.这对我来说都很新鲜,所以它可能是一个总的noob错误.这是一个相关的问题.

以下是我对python-on-dotcloud示例所做的更改.

  1. 显然nginx(从版本1.3.13开始)支持Web套接字.我从python-on-dotcloud示例更新了构建器脚本以指向此开发版本:nginx_download_url="http://nginx.org/download/nginx-1.3.14.tar.gz"

  2. 我设置gunicorn绑定到unix套接字,然后从nginx.conf设置proxy_pass,将流量上行发送到gunicorn proxy_pass http://appserver;,我已经定义了appserver.

  3. 我正在使用gevent-socket.io运行一个django应用程序,在没有运行nginx的情况下工作正常.(我只是将gunicorn绑定到dotcloud.yml中的0.0.0.0:$PORT_WWW)

我的问题是这些.

  1. 我想解决一个没问题吗?

    一个.我已经做了相当多的阅读,建议将枪支放在nginx后面.在dotcloud的前线负载平衡器的背景下,这仍然是真的吗?

    湾 如果我不期望我会受到DoS攻击,那么将gunicorn置于nginx后面仍然很重要吗?

  2. 我试图设置是否可以通过unix套接字运行websockets?

  3. unix套接字是否会在dotcloud上打破缩放?

  4. 如果我需要使用端口,那么如何设置呢?我不认为我可以在同一个应用程序中分配两个http端口.如果我将它拆分为两个应用程序,那么我不确定如何将来自gunicorn应用程序的PORT_WWW环境变量传递到nginx应用程序,以便最终可用于nginx postinstall脚本,从而生成nginx.conf.

  5. 关于为什么这不起作用的任何想法?

我在下面包含了三个配置文件.让我知道其他人是否会有所帮助.谢谢!

dotcloud.yml

www:
    type: custom
    buildscript: python/builder
    systempackages:
        # needed for the Nginx rewrite module
        - libpcre3-dev
        # needed to support python versions 2.7, 3.1, 3.2.
        - python3-all
    ports:
        www: http
    processes:
        nginx: nginx
        app: /home/dotcloud/env/bin/gunicorn -c /home/dotcloud/gunicorn.conf -b unix:/tmp/gunicorn.sock wsgi:application
        #app: /home/dotcloud/env/bin/gunicorn -c /home/dotcloud/gunicorn.conf -b 0.0.0.0:$PORT_WWW wsgi:application
    config:
        # python_version can have one of the following values (2.6, 2.7, 3.1, 3.2). 2.6 is the default if no value is entered.
        python_version: 2.7
data:
  type: redis
db:
  type: postgresql
Run Code Online (Sandbox Code Playgroud)

nginx.conf.in(与常规nginx.conf相同,只有PORT_WWW等待交换实际端口号)

# template for nginx.conf file.
# the file will be processed by the postinstall script
# it will insert the correct value for PORT_WWW and then put this
# file in /home/dotcloud/nginx/conf/nginx.conf when done.

# nginx will be managed by supervisord when it starts so we don't need daemon mode
daemon off;

worker_processes    1;

events {
    worker_connections  1024;
}


http {

 include       mime.types;
 default_type  application/octet-stream;

 sendfile        on;
 #tcp_nopush     on;

 keepalive_timeout  65;

 log_format combined-realip '$remote_addr ($http_x_real_ip) - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
 access_log  /var/log/supervisor/nginx_access.log combined-realip;
 error_log  /var/log/supervisor/nginx_error.log error;

 gzip  on;


    upstream appserver {
        server unix:/tmp/gunicorn.sock;
        # For a TCP configuration:
        # server 192.168.0.7:8000 fail_timeout=0;
    }


  server {
    # PORT_WWW value is added via postinstall script.
    listen  @PORT_WWW@ default;

    server_name localhost;

    root    /home/dotcloud/current/;

    location / {
        if ( -f /home/dotcloud/current/maintenance) {
            return 503;
        }
        proxy_pass http://appserver;
        proxy_http_version 1.1;
        proxy_set_header upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

    }

    error_page 404 @404;
    error_page 500 @500;
    error_page 502 @502;
    error_page 503 @503;
    error_page 504 @504;

    location @404 {
        rewrite ^ /static/404.html;
    }
    location @500 {
        rewrite ^ /static/500.html;
    }
    location @502 {
        rewrite ^ /static/502.html;
    }
    location @503 {
        rewrite ^ /static/503.html;
    }
    location @504 {
        rewrite ^ /static/504.html;
    }

    location /static {
        alias /home/dotcloud/current/static;
    }

    location /robots.txt {
        alias /home/dotcloud/current/static/robots.txt;
    }

    location /favicon.ico {
        alias /home/dotcloud/current/static/favicon.ico;
    }

  }
}
Run Code Online (Sandbox Code Playgroud)

gunicorn.conf

workers = 1
worker_class = 'socketio.sgunicorn.GeventSocketIOWorker'
pidfile = '/tmp/gunicorn.pid'
debug = True
loglevel = 'debug'
errorlog = '/var/log/supervisor/gunicorn.log'
django_settings='/home/dotcloud/settings.py'

def post_fork(server, worker):
    from psycogreen.gevent import patch_psycopg
    patch_psycopg()

# MySQL
#def post_fork(server, worker):
#    import pymysql
#    pymysql.install_as_MySQLdb()
Run Code Online (Sandbox Code Playgroud)

t1m*_*1m0 1

我在上面问了 5 个相关问题,我将在这里尝试回答前 3 个。(我对 dotcloud 的基础了解不够,无法回答最后两个问题)。我还预计大多数人发现这个问题主要集中在带有gunicorn和nginx的websocket上(较少关注dotcloud细节)。对于这些人,您可以跳转到此处的参考设置:gunicorn 部署方案与 nginx

  1. 我是否在尝试解决一个非问题?

    A。我已经阅读了大量建议将 Gunicorn 放在 nginx 后面的文章。在前线的 dotcloud 负载均衡器的背景下,这仍然是真的吗?

    b. 如果我预计不会受到 DoS 攻击,那么将 Gunicorn 放在 nginx 后面还重要吗?

从 Ken Cochrane 上面的评论来看,我相信 dotcloud 基础设施本身提供了 nginx 通常在 DIY 设置中提供的安全性。因此,在这种特殊情况下,这确实是“没问题的”。然而,一般来说,您确实希望在 nginx 后面使用 Gunicorn,并且您绝对可以使用该设置运行 websockets。

  1. 是否可以像我尝试设置的那样通过 unix 套接字运行 websockets?

是的。这里有一个关于gunicorn与nginx的部署方案的很好的参考。对于 Websocket,请务必阅读整个部分并包括proxy_buffering off;

  1. unix 套接字会破坏 dotcloud 上的扩展吗?

我的理解是套接字和端口应该同样工作。