改变项目结构后的Nginx Bad Gateway

时间:2017-01-20 03:28:37

标签: django amazon-web-services nginx wsgi gunicorn

我在AWS上托管了一个django应用程序,并且一直在清理项目结构的某些方面。其中一个清理涉及删除根目录中的父文件夹,并将我的appspec.yml文件(用于CodeDeploy)放在带有docs的文件夹中。所以我要离开这个:

myproject/ appspec.yml myproject/ requirements/ docs/ myproject/ manage.py myproject/ __init__.py urls.py wsgi.py settings/ myproject/ __init__.py myproject.py

对此:

myproject/ appspec.yml requirements/ docs/ myproject/ manage.py myproject/ __init__.py urls.py wsgi.py settings/ myproject/ __init__.py myproject.py

我在重新启动实例之前做了以下事情:

  1. gunicorn.conf位置从root /var/www/myproject/myproject/myproject/myproject更改为root /var/www/myproject/myproject/myproject
  2. nginx.conf位置从root /var/www/myproject/myproject/myproject/myproject更改为root /var/www/myproject/myproject/myproject
  3. 当我尝试访问我的网站网址并在查看nginx failed (104: Connection reset by peer) while reading response header from upstream时看到error.log时,我现在看到 502 BAD GATEWAY

    这显然是因为我搞乱了项目目录。我是否遗漏了其他需要更改的内容,或者首先更改目录是一个坏主意?

    当前nginx.conf

    server {
        listen 80;
        server_name  www.myproject.co;
        return 301 http://myproject.co$request_uri;
    }
    
    server {
        listen 80;
        server_name myproject.co;
    
        ##Deny illegal Host headers
        if ($host !~* ^(myproject.co)$ ) {
            return 444;
        }
    
        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /var/www/myproject/myproject/myproject;
        }
        location /media/ {
            root /var/www/myproject/myproject/myproject;
        }
    
    
        location / {
            include proxy_params;
            proxy_pass http://127.0.0.1:8000;
        }
    }
    

    当前gunicorn.conf

    description "Gunicorn application server handling myproject"
    
    start on runlevel [2345]
    stop on runlevel [!2345]
    
    respawn
    setuid ubuntu
    setgid ubuntu
    chdir /var/www/myproject/myproject/myproject
    
    exec /usr/local/bin/gunicorn --workers 3 myproject.wsgi:application --env SECRET_KEY=""
    

    追踪错误

    当我运行exec /usr/local/bin/gunicorn --workers 3 myproject.wsgi:application --env SECRET_KEY=""时,我收到以下错误:

    exec /usr/local/bin/gunicorn --workers 3 myproject.wsgi:application --env SECRET_KEY="f15a4d7s4bec9cnlw24sw2lj4ften12nu3e4cx3z1utqgajp87"
    [2017-01-20 16:02:49 +0000] [9809] [INFO] Starting gunicorn 19.4.5
    [2017-01-20 16:02:49 +0000] [9809] [INFO] Listening at: http://127.0.0.1:8000 (9809)
    [2017-01-20 16:02:49 +0000] [9809] [INFO] Using worker: sync
    [2017-01-20 16:02:49 +0000] [10039] [INFO] Booting worker with pid: 10039
    [2017-01-20 16:02:49 +0000] [10039] [ERROR] Exception in worker process:
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 515, in spawn_worker
        worker.init_process()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 122, in init_process
        self.load_wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 130, in load_wsgi
        self.wsgi = self.app.wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
        self.callable = self.load()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
        return self.load_wsgiapp()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
        return util.import_app(self.app_uri)
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in import_app
        __import__(module)
      File "/var/www/myproject/myproject/myproject/myproject/__init__.py", line 5, in <module>
        from .myproject import app as celery_app
    ImportError: cannot import name app
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 515, in spawn_worker
        worker.init_process()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 122, in init_process
        self.load_wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 130, in load_wsgi
        self.wsgi = self.app.wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
        self.callable = self.load()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
        return self.load_wsgiapp()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
        return util.import_app(self.app_uri)
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in import_app
        __import__(module)
      File "/var/www/myproject/myproject/myproject/myproject/__init__.py", line 5, in <module>
        from .myproject import app as celery_app
    ImportError: cannot import name app
    [2017-01-20 16:02:49 +0000] [10039] [INFO] Worker exiting (pid: 10039)
    [2017-01-20 16:02:49 +0000] [9809] [INFO] Shutting down: Master
    [2017-01-20 16:02:49 +0000] [9809] [INFO] Reason: Worker failed to boot.
    

    /var/www/myproject/myproject/myproject/myproject/myproject.py

    from __future__ import absolute_import
    
    import os
    from os.path import abspath, dirname
    from sys import path
    SITE_ROOT = dirname(dirname(abspath(__file__)))
    path.append(SITE_ROOT)
    
    from celery import Celery
    
    # set the default Django settings module for the 'celery' program.
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', "settings.production")
    
    from django.conf import settings  
    
    app = Celery('myproject')
    
    path.append('/var/www/myproject/myproject/myproject')
    # Using a string here means the worker will not have to
    # pickle the object when using Windows.
    app.config_from_object('django.conf:settings')
    
    # load task modules from all registered Django app configs.
    app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
    
    
    @app.task(bind=True)
    def debug_task(self):
        print('Request: {0!r}'.format(self.request))
    

    的/ var / WWW / myproject的/ myproject的/ myproject的/ myproject的/ __ init.py __

    from __future__ import absolute_import
    
    # This will make sure the app is always imported when
    # Django starts so that shared_task will use this app.
    from .myproject import app as celery_app
    
    # __all__ = ['celery_app']
    

    将.myproject导入应用程序中的`作为celery_app注释掉后出错:

    [2017-01-20 18:13:49 +0000] [1411] [INFO] Starting gunicorn 19.4.5
    [2017-01-20 18:13:49 +0000] [1411] [INFO] Listening at: http://127.0.0.1:8000 (1411)
    [2017-01-20 18:13:49 +0000] [1411] [INFO] Using worker: sync
    [2017-01-20 18:13:49 +0000] [1439] [INFO] Booting worker with pid: 1439
    [2017-01-20 18:13:49 +0000] [1439] [ERROR] Exception in worker process:
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 515, in spawn_worker
        worker.init_process()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 122, in init_process
        self.load_wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 130, in load_wsgi
        self.wsgi = self.app.wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
        self.callable = self.load()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
        return self.load_wsgiapp()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
        return util.import_app(self.app_uri)
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in import_app
        __import__(module)
    ImportError: No module named myproject.wsgi
    Traceback (most recent call last):
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/arbiter.py", line 515, in spawn_worker
        worker.init_process()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 122, in init_process
        self.load_wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/base.py", line 130, in load_wsgi
        self.wsgi = self.app.wsgi()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/base.py", line 67, in wsgi
        self.callable = self.load()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 65, in load
        return self.load_wsgiapp()
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/app/wsgiapp.py", line 52, in load_wsgiapp
        return util.import_app(self.app_uri)
      File "/usr/local/lib/python2.7/dist-packages/gunicorn/util.py", line 357, in import_app
        __import__(module)
    ImportError: No module named myproject.wsgi
    [2017-01-20 18:13:49 +0000] [1439] [INFO] Worker exiting (pid: 1439)
    [2017-01-20 18:13:49 +0000] [1411] [INFO] Shutting down: Master
    [2017-01-20 18:13:49 +0000] [1411] [INFO] Reason: Worker failed to boot.
    

    现在枪手似乎很难找到任何模块。

1 个答案:

答案 0 :(得分:0)

事实证明,gunicorn.conf并未指向其中包含manage.py的目录。所以我把它改成了:

description "Gunicorn application server handling myproject"

start on runlevel [2345]
stop on runlevel [!2345]

respawn
setuid ubuntu
setgid ubuntu
chdir /var/www/myproject/myproject

exec /usr/local/bin/gunicorn --workers 3 myproject.wsgi:application --env SECRET_KEY=""

这解决了这个问题。请参阅Django Docs - How to use Django with Gunicorn,其中指出您的项目应位于Python路径上;确保这一点的最简单方法是从manage.py文件所在的目录运行此命令。