0

I have a server serving multiple Django sites using nginx (1.1.19) and uWSGI (0.9.8). uWSGI is running in VirtualHosting mode and loading apps dynamically as documented here: http://projects.unbit.it/uwsgi/wiki/VirtualHosting

There are currently 5 Django sites running on the server with nginx configurations like this:

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

The 4 other sites are configured in exactly the same way, with example.com substituted for the real domains.

uWSGI is run with the following options:

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

This setup has worked great for 2 years now (which is why the version os uWSGI is so old).

However yesterday I tried to add a 5th site to this server, configured as follows:

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

When I attempt to load this site within a browser nginx gives me a 502 Bad Gateway error, and the logs say:

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"

This tells me 2 things; firstly the correct nginx virtual host is being loaded, and secondly uWSGI is dying before it can server the request. So next I check the uWSGI logs:

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)

The fact that these are errors are caused by uWSGi attempting to load the Django settings files of other projects (seemingly at random) suggests that uWSGI is ignoring the UWSGI_CHDIR and UWSGI_SCRIPT parameters in the nginx configuration and is randomly attempting to load another WSGI app.

Furthermore it appears that the UWSGI_SCRIPT file is never loaded as no test.pyc file is created within the /var/www/test.domain.com/wsgi. To further test this I hav placed assert False, 'test' at the start of the file, which is never shown in the uWSGI log file.

Does anyone have any ideas as to why this is happening when the existing sites are all still functioning correctly?

1 Answer 1

1

Not sure why existing sites are working but you need to name your wsgi files with unique names. For some reason, if you have bunch of wsgi files with the same name, uwsgi loads any of them.

2
  • Maybe unique django settings files as well. I have both, but not 100% sure if you need that.
    – Vargur
    Aug 22, 2013 at 14:28
  • Hi @vargur, I have tried giving both my WSGI file and the Django settings file unique names and I am still getting the same errors in the uWSGI log. Aug 22, 2013 at 14:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .