在EC2上使用nginx设置django

时间:2011-07-19 19:35:33

标签: django configuration amazon-ec2 nginx

我正在尝试使用nginx运行我的django应用程序,但在尝试连接到我的EC2 IP时遇到“连接超时”错误。当我运行

时,它在终端上正确显示了django“deafult”页面
curl 127.0.0.1

但EC2 IP没有运气。公共IP给我错误99(无法分配到请求的地址),私人IP在我的浏览器中给我“连接超时”消息。

那么我和/或这个nginx配置有什么问题:

user nobody nogroup;
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;

events {
    worker_connections  1024;
    accept_mutex: on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    access_log /tmp/nginx.access.log combined;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay         on;
    gzip  on;
    gzip_http_version 1.0;
    gzip_proxied any;
    gzip_min_lenght 500;
    gzip_disable "MSIE [1-6]\.";
    gzip_types text/plain text/xml text/css
         text/comma-separated-values
         text/javascript application/x-javascript
         application/atom+xml;
    include /etc/nginx/sites-enabled/*;
}

这是我启用的网站

upstream app_server {
  server 127.0.0.1:8000 fail_timeout=0;
}

server {
    listen my_IP:80 default;
    client_max_body_size 4G;
    server_name www.my_domain.pl my_domain.pl;
    keepalive_timeout 5;
    root /home/ubuntu/webapps/my_app;

  location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      if (!-f $request_filename) {
        proxy_pass http://app_server;
        break;
      }
  }
  error_page 500 502 503 504 /500.html;
  location = /500.html {
    root /path/to/app/current/public;

我是否需要更改其他内容的最后一行?或者这个proxy_pass东西?

1 个答案:

答案 0 :(得分:3)

我使用Nginx和Gunicorn在EC2上创建了我的Django站点,这些是我遵循的步骤

easy_install gunicorn
apt-get install nginx

nano /etc/init/site1.conf并添加了

description "site1 web server"
start on runlevel [2345]
stop on runlevel [06]
respawn
respawn limit 10 5
exec /home/scripts/gunicorn_runserver.sh

并在gunicorn_runserver.sh

#!/bin/bash
  set -e
  LOGFILE=/var/log/nginx/site1.log
  NUM_WORKERS=10
  # user/group to run as
  USER=www-data
  GROUP=adm
  cd /home/projects/project_name/
#  source ../../bin/activate
  exec gunicorn_django -w $NUM_WORKERS \
    --user=$USER --group=$GROUP --log-level=error \
    --log-file=$LOGFILE 2>>$LOGFILE

并在Nginx conf中

upstream app_server_site1 {
    server localhost:8000 fail_timeout=0;
}

location  / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (!-f $request_filename) {
            proxy_pass http://app_server_site1;
            break;
    }
}

最后

/etc/init.d/nginx restart
service site1 start

详细说明 Nginx + Django + Gunicorn here Nginx + Django + Fcgi here