使用Nginx和定制服务提供静态文件。 Dotcloud [英] Serve static files with Nginx and custom service. Dotcloud

查看:224
本文介绍了使用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
进程:
app:/ home / dotcloud / env / bin / python myproject / manage.py runserver_socketio 0.0.0.0:$PORT_WWW
nginx:nginx
ports:
www:http
系统包:
- libevent-dev
- python-psycopg2
- libpcre3-dev
db:
类型:postgresql

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



我也在我的postinstall结尾添加了:

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

if [-e$ nginx_config_template];然后
sed> $ HOME / nginx / conf / nginx.conf< $ nginx_config_template \
-es / @ PORT_WWW @ / $ {PORT_WWW:-42800} / g
else
echo($ nginx_config_template)不存在!!! Make确定它在正确的位置,否则nginx将无法正确设置。
fi

但是当我去我的应用程序,我推后,我得到错误:

  403禁止,nginx / 1.0.14 

Nginx提供错误页面404。



所以我不知道为什么,但我不可以访问我的应用程序了。你有什么想法可以用Nginx来设置我的应用程序吗?



非常感谢你

解决方案

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



此时你还有另外两个选项


  1. 使用CDN,将所有文件放在Amazon S3上,并从那里(或cloudfront)提供。 p>


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


dotcloud.yml

  www:
类型:自定义
进程:
应用程序:/ home / dotcloud / env / bin / python myproject / manage.py runserver_socketio 0.0.0.0:$PORT_WWW
端口:
www:http
系统包:
- libevent-dev
- python-psycopg2
- libpcre3-dev
db:
类型:postgresql
static:
type:static
approot:static_media

基本上它增加了一个名为static的新服务,而这个新服务正在您的项目中的一个名为static_media的目录中找到静态文件,该文件位于项目的根目录。



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



此设置的另一个好处是,如果您使用django的static_files应用程序。 Django的静态文件应用程序将所有静态媒体复制到一个公共位置。这不适用于静态服务,因为静态服务是分开的,并且很可能生活在不同的主机上,然后您的其他服务,所以您需要手动将文件复制到常用的static_media目录中。 p>

有关dotCloud静态服务的更多信息,请参阅这些文档: http://docs.dotcloud.com/0.9/services/static/



由于我提到了选项2,我会推荐使用选项1.如果您使用 https://github.com/jezdez/ django_compressor 。它可以将您的文件发送给s3。


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

解决方案

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.

这篇关于使用Nginx和定制服务提供静态文件。 Dotcloud的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆