如何在以下环境中设置子域名:nginx,supervisor,django,gunicorn?

时间:2013-03-18 12:58:28

标签: django nginx ubuntu-12.04 gunicorn supervisord

我有一个django应用程序设置与nginx + gunicorn +主管及其工作正常。但我需要为“dev.domain.com”创建一个用于登台或开发的子域。我在nginx.conf中为我的子域添加了另一个服务器块。但我的子域名url始终指向主域名网站。所以我按照其他帖子的建议更改了proxy_pass中的端口号。但是由于gunicorn和supervisord我需要在“/etc/supervisord/conf.d/subdomain.conf”中为这个子域添加另一个conf文件,但是当我重新加载supervisord时它无法启动我的子域程序。下面是我的nginx.conf,subdomain.conf,script.sh: 的 nginx.conf

http {
include       mime.types;
default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

gzip  on;
gzip_static  on;
gzip_types application/x-javascript text/css text/html application/json text/css text/json;

server {
listen   80;
server_name domain_name
# no security problem here, since / is alway passed to upstream
root /home/path/to/project/base
# serve directly - analogous for static/staticfiles
location /static/ {
    # if asset versioning is used
    if ($query_string) {
        expires max;
    }
    autoindex off;
    root /home/path/to/static/;
}
location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
proxy_pass http://localhost:8000/;
    proxy_set_header X-Forwarded-For $remote_addr;
}
}

server {
listen   80;
server_name subdomain_name
# no security problem here, since / is alway passed to upstream
root /home/path/to/subdomain_directory(which is different, you can say it is fully differnt project which i want to run as development project);
# serve directly - analogous for static/staticfiles


location / {
    proxy_pass_header Server;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_connect_timeout 10;
    proxy_read_timeout 10;
    proxy_pass http://localhost:9000/;
    proxy_set_header X-Forwarded-For $remote_addr;
}
}
}

script.sh

set -e
NUM_WORKERS=4
# user/group to run as
USER=user_name
#GROUP=your_unix_group
cd /home/path/to/subdomain_base
source subdomain_virtualenv_activation
LOGFILE=log_file_path
LOGDIR=$(dirname $LOGFILE)
test -d $LOGDIR || mkdir -p $LOGDIR
exec virtualenvironment/bin/gunicorn_django -w $NUM_WORKERS \
--user=$USER --log-level=debug \
--log-file=$LOGFILE 2>>$LOGFILE

subdomain.conf

[program:programname]
directory = /home/path/to/subdomainbase/
user = user_name
command = /home/path/to/script.sh
stdout_logfile = /home/path/to/log
stderr_logfile = /home/path/to/log

我也有一个procfile,就像gunicorn中的建议一样,它位于基本目录中 的 Procfile

./manage.py runserver_plus 0.0.0.0:$PORT

好的,这些是我的配置。请检查我做错了什么。我只想将我的开发服务器作为一个不同的项目运行,但在与子域相同的域下运行。在所有这些我正在做的改变之后,主域正在以同样的过程正常工作。如果您需要有关此错误的更多信息,请与我们联系。

1 个答案:

答案 0 :(得分:1)

编辑

我正在再次阅读你的帖子,并且......你不应该在你的枪炮剧本中设置ADDRESS吗? gunicorn默认使用端口8000,也许您的子域尝试使用相同的端口?

结束编辑

我有两个运行nginx,gunicorn和supervisor的Django应用程序(嗯,不一样,但非常相似,我有两个域和一个子域)。我不知道你的错误在哪里,我认为必须在nginx配置中。也许是“根”线?

您是否看到当您尝试使用“supervisorctl”命令启动时,supervisord是否会返回错误?

我可以告诉你我的配置,你可以比较一下:

我有 nginx 的两个.conf文件:

domain1.conf:

server {
    listen  80;
    server_name  domain1.net;
    return 301 $scheme://www.domain1.net$request_uri;    
}

server {
    listen 80;
    server_name www.domain1.net;
    access_log /var/log/nginx/domain1.log;

    location /static {
        alias /var/www/domain1/media/;
        autoindex on;
        access_log off;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For  $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://127.0.0.1:8000/;
    }

}

和domain2.conf:

server {
    listen 80;
    server_name subdomain.domain2.es;
    access_log /var/log/nginx/domain2.log;

    location /static {
        alias /var/www/dev/domain2/domain2/static/;
        autoindex on;
        access_log off;
    }

    location / {
        proxy_pass_header Server;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-For  $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout 10;
        proxy_read_timeout 10;
        proxy_pass http://127.0.0.1:8005/;
    }

}

我的两个 gunicor脚本是相同的,只是改变其中一个路径和ADDRESS:

#!/bin/bash
set -e
LOGFILE=/var/log/gunicorn/domain1.log
LOGDIR=$(dirname $LOGFILE)
NUM_WORKERS=1
# user/group to run as
USER=user
GROUP=user
ADDRESS=127.0.0.1:8005
cd /var/www/dev/domain1
source /path/to/venv/domain1/bin/activate
test -d $LOGDIR || mkdir -p $LOGDIR
exec gunicorn_django -w $NUM_WORKERS --bind=$ADDRESS \
  --user=$USER --group=$GROUP --log-level=debug \
  --log-file=$LOGFILE 2>>$LOGFILE

我的两个主管脚本也是一样的:

[program:domain1]
directory = /var/www/dev/domain1/
user = user
command = /path/to/bin/gunicorn_domain1.sh
stdout_logfile = /var/log/nginx/domain1.log
stderr_logfile = /var/log/nginx/domain1.log

我希望你发现这有用。