使用Nginx和自定义服务提供静态文件。 Dotcloud

时间:2012-12-03 10:20:20

标签: django nginx dotcloud

我在Dotcloud上部署了我的Django应用程序。

我正在使用带有Gevent和django-socketio的websockets,所以我使用了自定义服务。现在,我仍在使用'runserver_socketio'以使其正常工作。

现在,我想使用Nginx来提供我的静态文件。我发现了这个:https://github.com/dotcloud/nginx-on-dotcloud

我试着用它。这是我的dotcloud.yml:

    www:
       type: custom
       buildscript: nginx/builder
       processes:
          app: /home/dotcloud/env/bin/python myproject/manage.py runserver_socketio 0.0.0.0:$PORT_WWW
          nginx: nginx
       ports:
          www: http
       systempackages:
          - libevent-dev
          - python-psycopg2
          - libpcre3-dev
     db:
         type: postgresql

我在我的应用程序的根目录中添加了文件夹'nginx'。

我还在postinstall结束时添加了:

         nginx_config_template="/home/dotcloud/current/nginx/nginx.conf.in"

         if [ -e "$nginx_config_template" ]; then
               sed > $HOME/nginx/conf/nginx.conf < $nginx_config_template    \
               -e "s/@PORT_WWW@/${PORT_WWW:-42800}/g"
         else
               echo "($nginx_config_template) isn't there!!! Make sure it is in the correct location or else nginx won't be setup correctly."
         fi

但是当我转到我的应用程序后,在推送它之后,我收到错误:

            403 Forbidden, nginx/1.0.14

Nginx确实提供错误页面404。

所以我不知道为什么,但我再也无法访问我的应用了。您对我如何使用Nginx设置应用程序有任何想法吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

我认为你的问题是你有两个不同的进程争夺http端口(80)。您一次只能在端口80上运行一个进程。大多数人通过在端口80上运行nginx来解决这个问题,然后将所有流量反向代理到另一个进程,该进程在不同的端口上运行。这对你不起作用,因为nginx不支持Web套接字。这意味着您需要在80以外的端口上运行nginx或django应用程序。这也不太理想。

此时你还有两个选择

  1. 使用CDN,将所有文件放在Amazon S3上,并从那里(或云端)提供服务。

  2. 使用dotCloud的静态服务,这将是一个仅提供静态文件的服务。这是dotcloud.yml的样子。

  3. dotcloud.yml

    www:
       type: custom
       processes:
          app: /home/dotcloud/env/bin/python myproject/manage.py runserver_socketio 0.0.0.0:$PORT_WWW
       ports:
          www: http
       systempackages:
          - libevent-dev
          - python-psycopg2
          - libpcre3-dev
     db:
         type: postgresql
     static:
         type: static
         approot: static_media
    

    基本上它添加了一个名为static的新服务,这个新服务正在项目目录中名为static_media的目录中查找静态文件。

    如果您使用静态服务,则需要从静态服务获取URL,并在django settings.py中正确设置STATIC_URL。

    使用此设置的另一个问题是,如果您使用的是django的static_files应用程序。 Django的静态文件应用程序将所有静态媒体复制到一个公共位置。这不适用于静态服务,因为静态服务是独立的,并且很可能与其他服务不同,因此您需要手动将文件复制到您自己的公共static_media目录中。

    有关dotCloud静态服务的详细信息,请参阅以下文档:http://docs.dotcloud.com/0.9/services/static/

    由于我在选项2中提到的问题,我建议使用选项1.如果您使用类似https://github.com/jezdez/django_compressor的内容,这样做非常简单。它可以为您发送文件到s3。