开发者

Setting up django with nginx on EC2

开发者 https://www.devze.com 2023-03-21 05:31 出处:网络
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

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;

  开发者_Python百科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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消