Serve static files with Nginx and custom service. Dotcloud
Asked Answered
S

1

0

I deployed my Django app on Dotcloud.

I'm using websockets with Gevent and django-socketio, so I used a custom service. For now, I'm still using 'runserver_socketio' in order to make it works.

Now, I would like to use Nginx to serve my static files. I found this : https://github.com/dotcloud/nginx-on-dotcloud

I tried to use it. Here is my 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

And I added the folder 'nginx' at the root of my app.

I also added at the end of my 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

But when I go to my app, after I push it, I get the error:

            403 Forbidden, nginx/1.0.14

And Nginx does serve the error pages 404.

So I don't know why, but I don't have access to my app anymore. Do you have any idea on how I can set my app with Nginx?

Thank you very much

Sacrosanct answered 3/12, 2012 at 10:20 Comment(7)
Do you want to put your app behind nginx, or in front of it? - If you put your app behind nginx, you won't be able to use websockets anymore. - If you put your app in front of nginx, I'm not sure that you'll gain anything from nginx. So I'm trying to understand the rationale behind that!Taffrail
Is the nginx folder at the root of your dotcloud app or django app? nginx: nginx makes me believe that the nginx process will be run under the nginx user. Does the nginx user have permission to read the files in the nginx folder?Amusing
@Taffrail I wan't to use nginx to serve my static files, and put my app in front of nginx. I'm still running my app with runserver_socketio, so I can't put my DEBUG mode to False, because Django will stop serving my static files. So I need to use a server to do it. I'm new with programming so I don't know what is the best way to do it. I'll take any advice you could give me! Thank you for your answer.Sacrosanct
@Taffrail At the end, what I would like to do is serving my static files with nginx and use Gunicorn instead of 'runserver_socketio'Sacrosanct
If you just want to serve static files then you can use the nginx service to do that. It is a seperate service, but very easy to setup. It is cleaner then mixing two services together. Another option is to serve static files from a CDNMutazilite
@KenCochrane I can use it with the custom service? I tried to use a simple nginx.conf following the tutorial (docs.dotcloud.com/0.9/guides/nginx), but it didn't work.Sacrosanct
@Sacrosanct see my answer below, hopefully that clears things up.Mutazilite
M
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.

Mutazilite answered 3/12, 2012 at 16:18 Comment(3)
Thank you for your answer. I tried the second solution, it works find. I'll try the first one. But if I don't use static_files app, is there any advantage on using S3?Sacrosanct
The advantage to using S3, is that your content will load quicker for your visitors since it will be coming from a CDN (if you use cloudfront with s3), and it will remove the need for static service on dotcloud which will save you a little bit of money.Mutazilite
Ok, great. I'll have a look on how I could serve my static with cloudfront and S3. If you have any good tutorial about it, it would help me a lot. Thank you again for your answer.Sacrosanct

© 2022 - 2024 — McMap. All rights reserved.