[Answer]-Serve static files with Nginx and custom service. Dotcloud

1👍

I think Your problem is that you have two different processes fighting for the http port (80). You can only have one process running on port 80 at a time. Most people work around this by having nginx running on port 80, and then reverse proxying all traffic to the other process, which runs on a different port. This wouldn’t work for you, because nginx doesn’t support web sockets. So that means you would need to run nginx or the django app on a port other then 80. Which also isn’t ideal.

At this point you have two other options

  1. Use a CDN, put all of your files on Amazon S3, and serve them from there (or cloudfront).

  2. Use dotCloud’s static service, this will be a separate service that just serves the static files. Here is what your dotcloud.yml would look like.

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

Basically it adds a new service called static, and this new service, is looking for your static files in a directory in your project called static_media, located at the root of your project.

If you use the static service, you will need to get the URL from the static service, and set your STATIC_URL appropriately in your django settings.py.

Another gotcha with this setup, is if you are using django’s static_files app. Django’s static files app will copy all the static media into one common location. This doesn’t work with the static service, because the static service is separate, and will most likely live on a different host then your other service, so you will need to manually copy the files into the common static_media directory your self.

For more information about the dotCloud static service, see these docs: http://docs.dotcloud.com/0.9/services/static/

Because of the gotcha I mentioned for option 2, I would recommend using option 1. Doing this is a pretty easy if you use something like https://github.com/jezdez/django_compressor . It can send your files to s3 for you.

Leave a comment