为什么 uWSGI 没有加载 nginx 配置中指定的应用程序?

为什么 uWSGI 没有加载 nginx 配置中指定的应用程序?

我有一个使用 nginx (1.1.19) 和 uWSGI (0.9.8) 为多个 Django 站点提供服务的服务器。uWSGI 在 VirtualHosting 模式下运行,并动态加载应用程序,如下所述:http://projects.unbit.it/uwsgi/wiki/VirtualHosting

当前服务器上运行着 5 个 Django 站点,其 nginx 配置如下:

# /etc/nginx/sites-enabled/www.example.com
server {
    listen xxx.xxx.xxx.xxx:80;

    server_name www.example.com example.com;

    if ($host != 'www.example.com' ) {
        rewrite  ^/(.*)$  http://www.example.com/$1  permanent;
    }

    location / {
        uwsgi_pass  127.0.0.1:8000;
        include uwsgi_params;
        uwsgi_param UWSGI_CHDIR     /var/www/www.example.com/wsgi;
        uwsgi_param UWSGI_SCRIPT    production;
        uwsgi_param UWSGI_PYHOME    /var/envs/www.example.com;
    }
}

其他 4 个站点的配置方式完全相同,用 example.com 代替真实域名。

uWSGI 使用以下选项运行:

/usr/local/bin/uwsgi --socket $SOCKET --chmod-socket --master --harakiri $HARAKIRI --sharedarea $SHAREDAREA --processes $PROCESSES --daemonize $LOG --max-requests $MAX_REQUESTS --vhost --no-site"

这个设置已经运行了2年了(这就是为什么 uWSGI 版本这么旧)。

但是昨天我尝试向该服务器添加第 5 个站点,配置如下:

# /etc/nginx/sites-enabled/test.domain.com
server {
    listen  xxx.xxx.xxx.xxx:80;

    server_name test.domain.com;

    location / {
        uwsgi_pass  127.0.0.1:8000;
        include uwsgi_params;
        uwsgi_param UWSGI_CHDIR     /var/www/test.domain.com/wsgi;
        uwsgi_param UWSGI_SCRIPT    test;
        uwsgi_param UWSGI_PYHOME    /var/envs/test.domain.com;
    }
}

当我尝试在浏览器中加载此站点时,nginx 给出了 502 Bad Gateway 错误,并且日志显示:

2013/08/22 12:17:03 [error] 26397#0: *9171 upstream prematurely closed connection while reading response header from upstream, client: 77.98.87.69, server: test.domain.com, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8000", host: "test.domain.com"

这告诉我 2 件事;首先,正确的 nginx 虚拟主机正在加载,其次,uWSGI 在能够处理请求之前就挂了。因此,接下来我检查 uWSGI 日志:

Traceback (most recent call last):
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'site3.settings.production' (Is it on sys.path?): No module named site3.settings.production
test.domain.com [pid: 23255|app: 4|req: 93/22584] 77.98.87.69 () {52 vars in 1112 bytes} [Thu Aug 22 12:42:01 2013] GET / => generated 0 bytes in 1 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'site3.settings.production' (Is it on sys.path?): No module named site3.settings.production
test.domain.com [pid: 23255|app: 4|req: 94/22585] 77.98.87.69 () {52 vars in 1112 bytes} [Thu Aug 22 12:42:02 2013] GET / => generated 0 bytes in 1 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'site3.settings.production' (Is it on sys.path?): No module named site3.settings.production
test.domain.com [pid: 23255|app: 4|req: 95/22586] 77.98.87.69 () {52 vars in 1112 bytes} [Thu Aug 22 12:42:02 2013] GET / => generated 0 bytes in 1 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'site4.settings.production' (Is it on sys.path?): No module named site4.settings.production
test.domain.com [pid: 14025|app: 5|req: 80/22587] 77.98.87.69 () {52 vars in 1112 bytes} [Thu Aug 22 12:42:03 2013] GET / => generated 0 bytes in 1 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'site3.settings.production' (Is it on sys.path?): No module named site3.settings.production
test.domain.com [pid: 23255|app: 4|req: 96/22589] 77.98.87.69 () {52 vars in 1112 bytes} [Thu Aug 22 12:42:03 2013] GET / => generated 0 bytes in 1 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)
Traceback (most recent call last):
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
    self.load_middleware()
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/core/handlers/base.py", line 45, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 53, in __getattr__
    self._setup(name)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 48, in _setup
    self._wrapped = Settings(settings_module)
  File "/var/envs/test.domain.com/lib/python2.7/site-packages/django/conf/__init__.py", line 134, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'site2.settings.test' (Is it on sys.path?): No module named site2.settings.test
test.domain.com [pid: 11217|app: 4|req: 46/23046] 77.98.87.69 () {52 vars in 1112 bytes} [Thu Aug 22 12:47:56 2013] GET / => generated 0 bytes in 1 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

这些错误是由于 uWSGi 尝试加载其他项目的 Django 设置文件(似乎是随机的)而导致的,这表明 uWSGI 忽略了nginx 配置中的UWSGI_CHDIRUWSGI_SCRIPT参数,并随机尝试加载另一个 WSGI 应用程序。

此外,似乎该UWSGI_SCRIPT文件从未被加载,因为没有在 中创建 test.pyc 文件。为了进一步测试这一点,我在文件的开头/var/www/test.domain.com/wsgi放置了,但它从未显示在 uWSGI 日志文件中。assert False, 'test'

当现有站点仍正常运行时,有谁知道为什么会发生这种情况?

答案1

不确定现有网站为何能正常工作,但您需要使用唯一名称来命名 wsgi 文件。出于某种原因,如果您有一堆同名的 wsgi 文件,uwsgi 会加载其中任何一个。

相关内容