使用UWSGI为Nginx提供多个Django站点

时间:2012-07-30 11:05:00

标签: nginx uwsgi

我正在尝试使用Nginx服务两个Django站点。

我可以提供任何一个没问题但是如果我同时激活它会将两个网址发送到一个网站。这是我第一次使用Nginx,我经常使用Apache,所以请耐心等待。

我在启用的网站中有两个网站,如下所示:

site1.com:

server{
    server_name www.site1.com;
    listen 69.164.211.85:80;
    access_log /var/www/site1.env/logs/access.log;
    error_log /var/www/site1.env/logs/error.log;

    location /static/ {
            # Point this wherever the static files for your django app are $
            autoindex on;
            alias /var/www/site1.env/Site1/static/;
    }

    location / {
        uwsgi_pass    127.0.0.1:3031;
        include       uwsgi_params;
        uwsgi_param   UWSGI_APPID site1;
        uwsgi_param UWSGI-FILE /var/www/site1.env/Site1/wsgi/site1_wsgi.py;
     }
}

site2.net

server{
        server_name www.site2.net;
        listen 69.164.211.85:80;
        access_log /var/www/site2.env/logs/access.log;
        error_log /var/www/site2.env/logs/error.log;

        location /static/ {
                # Point this wherever the static files for your django app are $
                autoindex on;
                alias /var/www/site2.env/Site2/static/;
        }

        location / {
            uwsgi_pass    127.0.0.1:3032;
            include       uwsgi_params;
            uwsgi_param   UWSGI_APPID site2;
            uwsgi_param UWSGI-FILE /var/www/site2.env/Site2/wsgi/site2.py;
         }

}

我还运行了两个UWSGI实例,它们开始使用这个脚本:

网站1:

description "uWSGI server"

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

respawn
exec /usr/local/bin/uwsgi \
--home /var/www/site1.env/Site1/ \
--socket 127.0.0.1:3031 \
--chmod-socket \
--module site1_wsgi \
--pythonpath /var/www/site1.env/Site1/wsgi \
-H /var/www/site1.env

网站2:

description "uWSGI server"

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

respawn
exec /usr/local/bin/uwsgi \
--home /var/www/site2.env/Site2/ \
--socket 127.0.0.1:3032 \
--chmod-socket \
--module site2 \
--pythonpath /var/www/site2.env/Site2/wsgi \
-H /var/www/sit2.env

这是我的nginx.conf文件的样子:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

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;

   ##
    # 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/x-javascript text/xml application/xml application/$

    ##
    # Virtual Host Configs
    ##

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

   ##
# 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/x-javascript text/xml application/xml application/$

    ##
    # Virtual Host Configs
    ##

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

我原本以为uwsgi_pass设置为不同的端口会阻止它们进入同一个端口,但显然我错过了其他的东西。我很感激任何帮助,谢谢!

2 个答案:

答案 0 :(得分:3)

啊,可能是因为我没有包含没有www的服务器名称。当我这样做它似乎工作。

答案 1 :(得分:1)

nginx配置对我来说似乎很好。我用相同的代码(指向不同端口的两个不同站点)进行了测试,并在端口上启动了一个监听器 - 每个站点都转到指定的端口。

我对uwsgi配置有一些疑问,尤其是环境/路径设置。 请尝试以下操作 - 创建一个uwsgi1.conf文件,并在其中粘贴以下内容:

[uwsgi]
socket = 127.0.0.1:3031
master = true
processes = 8
env = DJANGO_SETTINGS_MODULE=site1.settings
chdir = /var/www/site1.env/Site1/
pythonpath = /var/www/site1.env/
module = django.core.handlers.wsgi:WSGIHandler()
-H /var/www/site1.env

和第二个uwsgi2.conf,内容如下:

[uwsgi]
socket = 127.0.0.1:3032
master = true
processes = 8
env = DJANGO_SETTINGS_MODULE=site2.settings
chdir = /var/www/site2.env/Site2/
pythonpath = /var/www/site2.env/
module = django.core.handlers.wsgi:WSGIHandler()
-H /var/www/site2.env

并根据需要更改值env /,chdir,pythonpath。然后启动网站:

respawn
exec /usr/local/bin/uwsgi --ini /path/to/uwsgi1.conf

uwsgi2.conf为第二个。