3

I'm trying to run my django app with nginx, but when trying to connect to my EC2 IP im getting "connection timed out" error. It properly shows django "deafult" page on terminal when I run

curl 127.0.0.1

But no luck with EC2 IPs. Public IP gives me error 99 (cannot assign to requested address), private IP gives me "connection timed out" message in my browser.

So what's wrong with me and/or this nginx config:

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/*;
}

Here's my enabled site

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;

Do I need to change the last line for something else? Or this proxy_pass thingy?

Soviet
  • 119
  • 4
  • 11
  • 3
    It may be nothing at all is wrong with your nginx configuration. The errors you're seeing are ones I've experienced associated with the EC2 firewall defaults. Go into the EC2 control panel and open the ports (80, it looks like) to which you want to grant access. – Elf Sternberg Jul 19 '11 at 19:47
  • 1
    Oh God. Why I always have the most stupid problems on whole stackoverflow? xD. Thank you Elf. – Soviet Jul 19 '11 at 20:06
  • 2
    You're welcome! I have days where I can't rub two brain cells together. We all do. – Elf Sternberg Jul 19 '11 at 21:29
  • @Soviet FYI, you can't use private-ip on your browsers, private-ip's are meant to be used within Amazon services (like RDS, other EC2 instances, etc). – Rakesh Sankar Jul 20 '11 at 07:03
  • @Soviet: If this worked for you, please post the solution as an answer and accept it. You may help humanity this way ;) Thanks. – Tadeck Mar 01 '12 at 13:00

1 Answers1

3

I created my Django site on EC2 using Nginx and Gunicorn and These are the steps i followed

easy_install gunicorn
apt-get install nginx

nano /etc/init/site1.conf and added

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

and in 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

and in 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;
    }
}

finally

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

Detail description about Nginx+Django+Gunicorn here and Nginx+Django+Fcgi here

Rakesh
  • 81,458
  • 17
  • 76
  • 113