7983

Serve static files with Nginx and custom service. Dotcloud

Question:

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 : <a href="https://github.com/dotcloud/nginx-on-dotcloud" rel="nofollow">https://github.com/dotcloud/nginx-on-dotcloud</a>

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

Answer1:

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

<ol><li>

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

</li> <li>

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.

</li> </ol>

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: <a href="http://docs.dotcloud.com/0.9/services/static/" rel="nofollow">http://docs.dotcloud.com/0.9/services/static/</a>

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 <a href="https://github.com/jezdez/django_compressor" rel="nofollow">https://github.com/jezdez/django_compressor</a> . It can send your files to s3 for you.

Recommend

  • npm install private git repository with .git folder and .gitignore file
  • Error in package.json deploying Angular2 project on Azure using bitbucket
  • cannot start angular-phonecat, postinstall script failing [closed]
  • Could not find the following Boost libraries: boost_asio
  • Objective-C : getting error on console while trying to display app on ipad device?
  • Einsum optimize fails for basic operation
  • Clion & CMake. How To Add Library (*.so)
  • Importing pyplot in a Jupyter Notebook
  • Unable to gem install nokogiri
  • Error while importing scikits.talkbox
  • What do I do with this error when I run tests in rails?
  • removing last 3 characters on a file (file extension)
  • Need code translation from VB to C#
  • PHP Listener Script for Paypal Webhooks
  • php script is parsing content from RTE (tt_news) but internal links are not appearing as speaking ur
  • Open Existing DB in MySQL WorkBench
  • How to get latest version of a artifact on Bintray using JSONP
  • During installation of Django, why do I keep getting ImportError: No module named django?
  • Adding elements to a huge XML file
  • error importing numpy
  • How can I extract results of aggregate queries in slick?
  • Needing to do .toArray() to get output of mongodb .find() on key name not value
  • htaccess add www if not subdomain, if subdomain remove www
  • How to define and use opencv mat of user type
  • Very simple C++ DLL that can be called from .net
  • Change Inet root folder for iis 7
  • MongoDB in PHP using aggregate to group by _id is null not working
  • Paperclip, set path outside of rails root folder
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Cassandra Data Model
  • Proper folder structure for lots of source files
  • Load html files in TinyMce
  • Acquiring multiple attributes from .xml file in c#
  • Getting Messege Twice Using IMvxMessenger
  • Turn off referential integrity in Derby? is it possible?
  • Append folder name and increment by 1 using batch script
  • reshape alternating columns in less time and using less memory
  • How can i traverse a binary tree from right to left in java?
  • Python/Django TangoWithDjango Models and Databases
  • How can I use threading to 'tick' a timer to be accessed by other threads?