2013-03-18 38 views
0

我有一個django應用程序設置與nginx + gunicorn +主管和它的工作正常。但我需要爲「dev.domain.com」創建一個用於分段或開發的子域。我在nginx.conf中爲我的子域添加了另一個服務器塊。但我的子域網址始終指向主域名站點。所以我改變了其他職位上建議的proxy_pass中的端口號。但由於gunicorn和supervisord我需要在「/etc/supervisord/conf.d/subdomain.conf」中爲此子域添加另一個conf文件,但是當我重新加載supervisord時,它無法啓動我的子域程序。下面是我的nginx.conf,subdomain.conf,script.sh: nginx.conf如何在以下環境中設置子域名:nginx,主管,django,gunicorn?

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

編輯

我又讀您的文章,並...你不應該必須在gunicorn腳本設定的地址? gunicorn默認使用端口8000,也許你的子域試圖使用相同的端口?

編輯完

我有nginx的,gunicorn和監督者運行兩個Django應用程序,你想做的事(當然,不一樣的,但非常相似,我有兩個域和子域)。我看不出你的錯誤在哪裏,我認爲必須在nginx配置中。也許「根」線?

您是否看到過,當您嘗試使用「supervisorctl」命令啓動它時,supervisord是否會返回錯誤?

我可以告訴你我的配置,你可以比較一下:

我有nginx的 2個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腳本是其中之一相同,只是改變路徑和地址:

#!/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 

我希望你覺得這有幫助。

+0

感謝您的回覆,我曾嘗試在gunicorn腳本中添加「ADDRESS = 127.0.0.1:PORT」並重新載入supervisord,但它沒有奏效。我檢查了超級用戶日誌,有兩條消息與我的新子域應用1相關。EXIT STATUS 1:NOT EXPECTED,並在下一行2.「進入FATAL狀態,太多開始重試太快」(我不知道爲什麼它說的太多重啓我只重載了一次,請檢查是否可以得到是什麼原因造成的問題,謝謝 – 2013-03-20 08:41:00

+0

如果你手工運行腳本(沒有supervisord),它的工作是否正確? – jjimenez 2013-03-21 11:54:05

+0

哦,我們可以做那麼測試?我會測試同樣,並讓你知道它是否工作與否,謝謝你的回覆。 – 2013-03-22 11:58:26