Django-Gunicorn-Nginx部署并没有超越Nginx

时间:2017-08-19 12:47:15

标签: python django nginx gunicorn

我试图在EC2实例上部署项目。

当我转到我的EC2实例网址时,我可以看到Nginx欢迎消息,但我似乎无法将其转换为django。

我有一个Gunicorn服务和Nginx服务,这些是他们的service status电话的输出:

Gunicorn:

  

●gunicorn_mynew_website.service - 我的新网站的Gunicorn守护进程   已加载(/etc/systemd/system/gunicorn_mynew_website.service;   启用;供应商预设:已启用)活动:自周六起激活(正在运行)   2017-08-19 12:29:05 UTC; 2s ago Main PID:1760(gunicorn)       任务:2内存:33.6M         CPU:368ms CGroup:/system.slice/gunicorn_mynew_website.service              ├─1760/opt/mynew/venv/website-venv/bin/python3.5 / opt / mynew / venv / website-venv / bin / gunicorn website.wsgi:application   --name website --workers 1 --user ubuntu --bind = unix:/ opt / mynew / run / gunicor              └─1769/opt/mynew/venv/website-venv/bin/python3.5 / opt / mynew / venv / website-venv / bin / gunicorn website.wsgi:application   --name website --workers 1 --user ubuntu --bind = unix:/ opt / mynew / run / gunicor

     

8月19日12:29:05 ip-172-31-26-24 systemd [1]:启动了Gunicorn守护进程   对于我的新网站。 8月19日12:29:05 ip-172-31-26-24   gunicorn_start.sh [1760]:以ubuntu 8月19日12:29:05开始网站   ip-172-31-26-24 gunicorn_start.sh [1760]:[2017-08-19 12:29:05 +0000]   [1760] [INFO]启动gunicorn 19.7.1 Aug 19 12:29:05 ip-172-31-26-24   gunicorn_start.sh [1760]:[2017-08-19 12:29:05 +0000] [1760] [INFO]   听:unix:/opt/mynew/run/gunicorn.sock(1760)898 19:29:05   ip-172-31-26-24 gunicorn_start.sh [1760]:[2017-08-19 12:29:05 +0000]   [1760] [INFO]使用worker:sync 8月19日12:29:05 ip-172-31-26-24   gunicorn_start.sh [1760]:[2017-08-19 12:29:05 +0000] [1769] [INFO]   使用pid启动工作人员:1769

Nginx的

  

●nginx.service - 高性能Web服务器和反向代理   server Loaded:loaded(/lib/systemd/system/nginx.service; enabled;   供应商预设:已启用)活动:自周六起激活(正在运行)   2017-08-19 11:34:14 UTC; 56分钟前过程:1151   ExecStart = / usr / sbin / nginx -g daemon on; master_process on;   (code = exited,status = 0 / SUCCESS)流程:1090   ExecStartPre = / usr / sbin / nginx -t -q -g daemon on; master_process on;   (code = exited,status = 0 / SUCCESS)主PID:1165(nginx)       任务:2记忆:9.6M         CPU:24ms CGroup:/system.slice/nginx.service              ├─1165nginx:主进程/ usr / sbin / nginx -g守护进程; master_process on              └─1166nginx:工人流程

     

8月19日11:34:14 ip-172-31-26-24 systemd [1]:开始高   性能Web服务器和反向代理服务器... 8月19日11:34:14   ip-172-31-26-24 systemd [1]:启动高性能Web服务器和   反向代理服务器。

我的`/etc/nginx/nginx.conf(Http部分)是:

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;



        upstream test_server {
          server unix:/opt/mynew/run/gunicorn.sock fail_timeout=10s;
        }

        server {
            listen   80;

            client_max_body_size 4G;

            access_log /opt/mynew/logs/nginx-access.log;
            error_log /opt/mynew/logs/nginx-error.log warn;

            location /static/ {
                autoindex on;
                alias   /opt/mynew/website/static/;
            }

            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://test_server;
                    break;
                }
            }

}

我的gunicorn_start.sh文件(服务):

#!/bin/bash

NAME="website"                              
DJANGODIR=/opt/mynew/website             
SOCKFILE=/opt/mynew/run/gunicorn.sock        
USER=ubuntu
GROUP=www-data
NUM_WORKERS=1
DJANGO_SETTINGS_MODULE=website.settings
DJANGO_WSGI_MODULE=website.wsgi
echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
source /opt/mynew/venv/website-venv/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
export PYTHONPATH=$DJANGODIR:$PYTHONPATH

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)
exec /opt/mynew/venv/website-venv/bin/gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user $USER \
  --bind=unix:$SOCKFILE

我的django文件夹位于/opt/mynew/website/

为了理解发生了什么,我故意向/website/website/settings.py添加例外,只是为了看看gunicorn是否与它连接,当我开始使用gunicorn服务时它确实失败了

所以我猜测Nginx和gunicorn之间的错误......

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

删除这两个

include /etc/nginx/conf.d/*.conf; 
include /etc/nginx/sites-enabled/*; 

看看它是否有效。默认情况下,nginx附带一个default.conf,它将侦听端口80并使用静态nginx页面为/var/www/html目录提供服务。因此要么知道你包含的每个文件,要么不包括它们