簡體   English   中英

如何在以下環境中設置子域:nginx,主管,django,gunicorn?

[英]how to setup subdomain in following environment: nginx, supervisor, django, gunicorn?

我有一個使用nginx + gunicorn + supervisor的django應用程序安裝程序,並且工作正常。 但我需要為登台或開發創建一個子域,例如“ dev.domain.com”。 我在nginx.conf中為我的子域添加了另一個服務器塊。 但是我的子域網址始終指向主域站點。 所以我在其他帖子中建議在proxy_pass中更改端口號。 但是由於gunicorn和supervisor,我需要在“ /etc/supervisord/conf.d/subdomain.conf”中為此子域添加另一個conf文件,但是當我重新加載supervisor時,它無法啟動我的子域程序。 以下是我的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

好的,這些是我的配置。 請檢查我在哪里做錯了。 我只想將開發服務器作為一個不同的項目但在與子域相同的域下運行。 畢竟,無論我在做什么更改,主域在相同的過程中都可以正常工作。 如果您需要有關此錯誤的更多信息,請告訴我。

編輯

我正在重新閱讀您的文章,並且...您是否不應該在您的gunicorn腳本中設置ADDRESS? gunicorn默認情況下使用端口8000,也許您的子域嘗試使用同一端口?

結束編輯

我有兩個想要運行的Django應用程序,分別使用nginx,gunicorn和supervisor運行(嗯,不一樣,但是非常相似,我有兩個域和一個子域)。 我看不出您的錯誤在哪里,我認為一定是在nginx配置中。 也許是“根”線?

當您嘗試使用“ supervisorctl”命令啟動supervisor時,是否看到supervisor返回了錯誤?

我可以向您展示我的配置,您可以對其進行比較:

我為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腳本是相同的,只是其中之一更改了路徑和地址:

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

希望對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM