gunicorn, nginx (v 1.3.14), django, and gevent-socket.io, on



我正在尝试在dotcloud上部署nginx (v 1.3.14)后面的gunicorn + gevent。我有几个问题要问。我的目标是改编python-on-dotcloud示例。到目前为止,我还没能让websockets部分与这个设置一起工作。换句话说,我必须手动刷新页面以获得更新,而不是通过socket.io。这一切对我来说都很新鲜,所以这可能是一个完全的新手错误。下面是一个相关的问题。

以下是我对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套接字,然后设置proxy_pass从nginx.conf发送流量上游到gunicorn与proxy_pass http://appserver;,我已经定义了appserver。

  3. 我正在运行一个django应用程序与getevent -socket。IO工作良好,没有nginx运行。(我只是绑定gunicorn 0.0.0.0:$PORT_WWW在dotcloud.yml)

我的问题是:

  1. 我是在试图解决一个无关紧要的问题吗?

    。我读过很多建议把gunicorn放在nginx后面的文章。在dotcloud的负载均衡器在前线的背景下,这仍然是正确的吗?

    b。如果我不希望自己受到DoS攻击,那么把gunicorn放在nginx后面还重要吗?

  2. 是否有可能通过unix套接字运行websockets,因为我试图设置?

  3. unix套接字会破坏dotcloud上的缩放吗?

  4. 如果我需要使用端口,如何设置?我不认为我可以在同一个应用程序中分配两个http端口。如果我把它分成两个应用程序,那么我不确定如何将PORT_WWW环境变量从gunicorn应用程序传递到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

nginx.conf。(与常规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;
    }
  }
}

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()

我在上面问了5个相关的问题,在这里我试着回答前3个。(我对dotcloud基础的了解还不够多,无法回答后两个问题)。我也希望大多数发现这个问题的人主要关注的是使用gunicorn和nginx的websockets(而不是dotcloud细节)。对于这些人,你可以跳到这里的参考设置:gunicorn部署方案与nginx

  1. 我是在试图解决一个无关紧要的问题吗?

    。我读了很多建议放在哪里的书nginx背后的Gunicorn。在dotcloud的负载平衡器上前线,这还是真的吗?

    b。如果我没有预料到我会受到DoS攻击,那它仍然是把谷歌放在nginx后面很重要吗?

从上面Ken Cochrane的评论来看,我相信dotcloud基础设施本身提供了nginx通常在DIY设置中提供的安全性。因此,在这种特殊情况下,这确实是一个"非问题"。但是,一般情况下,你确实需要nginx背后的gunicorn,并且你绝对可以使用该设置运行websockets。

  • 是否有可能通过unix套接字运行websockets,因为我试图设置?
  • 是的。这是一个很好的参考gunicorn部署方案与nginx。对于websockets,请务必阅读整个部分,并包括proxy_buffering off;

  • unix套接字将打破扩展点云?
  • 我的理解是套接字和端口都应该工作得很好。

    最新更新