Python社区  »  Django

django同主机多应用程序Nginx Gunicorn

help • 1 周前 • 33 次点击  

在一台服务器上运行多个django应用程序

environment python3 nginx django2

我不能双双跑动。总是 Bad Request (400)

django app1设置

nginx设置 /etc/nginx/conf.d/abc.conf

server {
    listen 80;
    server_name .abc.co;
    rewrite ^ https: // $ server_name $ request_uri? permanent;
}

server {
    listen 443 ssl;
    server_name .abc.co;

    ssl_certificate /etc/letsencrypt/live/abc.co/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/abc.co/privkey.pem;
    ssl_protocols TLSv1;
    ssl_ciphers HIGH:! ADH:! MD5;
    ssl_prefer_server_ciphers on;

  location / static {
        alias /home/abc.co/static;
    }

    location / media {
        alias /home/abc.co/media;
    }

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
        proxy_set_header Host $ http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $ scheme;
    }
}

gunicorn设置 /etc/systemd/system/abc.服务

[Unit]
Description = gunicorn
After = network.target

[Service]
WorkingDirectory = / home / abc.co
ExecStart = / usr / bin / gunicorn --bind 127.0.0.1:8000 config.wsgi: application

[Install]
WantedBy = multi-user.target

django设置 /主页/123.co/config/settings.py

ALLOWED_HOSTS = ['abc.co']

使用上述设置检查对主域的访问,并确认代码工作正常。

接下来,尝试在相同的服务器和IP地址上构建另一个django应用程序。然后它失败了。

django app2设置

nginx设置 /etc/nginx/conf.d/123.conf

server {
    listen 80;
    server_name .123.jp;
    rewrite ^ https: // $ server_name $ request_uri? permanent;
}

server {
    listen 443 ssl;
    server_name. 123.jp;

    ssl_certificate /etc/letsencrypt/live/123.jp/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/123.jp/privkey.pem;
    ssl_protocols TLSv1;
    ssl_ciphers HIGH:! ADH:! MD5;
    ssl_prefer_server_ciphers on;

  location / static {
        alias /home/123.jp/static;
    }

    location / media {
        alias /home/123.jp/media;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
        proxy_set_header Host $ http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $ scheme;
    }
}

gunicorn设置 /etc/systemd/system/123.service

[Unit]
Description = gunicorn
After = network.target

[Service]
WorkingDirectory = / home / 123.jp
ExecStart = / usr / bin / gunicorn --bind 127.0.0.1:8080 config.wsgi: application

[Install]
WantedBy = multi-user.target

django设置 /home/123.jp/config/settings.py

ALLOWED_HOSTS = ['123.jp']

第一个域运行正常,但每次访问第二个域时 错误请求(400) 它会出来的。 Bad Request (400) is due to access from a server that is not specified in ALLOWED_HOSTS

我试过的:

  1. gunicorn配置和nginx配置 127.0.0.1:8080至 127.0.0.2:8000更改为。测试
    • reboot
    • systemctl restart nginx
    • systemctl daemon-reload
    • 通过任何方法重新启动
    • ALLOWED_HOSTS = ['*'] #设置为全部接受通配符
    • ALLOWED_HOSTS = ['abc.co', '123.34.1.123'] #设置为接受域和服务器IP

即使我做了所有的事情,它也不会工作,即使我在 ALLOWED_HOSTS ,如果我启动两个django,“abc.co”不在 允许的主机 无法在django调试屏幕上显示。呕吐错误。

--附言--

嗨,谢谢你的回答。 我做的

django app2设置

nginx设置/etc/nginx/conf.d/123.conf

server_2 {
    listen 80;
    server_name .123.jp;
    rewrite ^ https: // $ server_name $ request_uri? permanent;
}

server_2 {
    listen 443 ssl;
    server_name. 123.jp;

    ssl_certificate /etc/letsencrypt/live/123.jp/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/123.jp/privkey.pem;
    ssl_protocols TLSv1;
    ssl_ciphers HIGH:! ADH:! MD5;
    ssl_prefer_server_ciphers on;

  location / static {
        alias /home/123.jp/static;
    }

    location / media {
        alias /home/123.jp/media;
    }

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
        proxy_set_header Host $ http_host;
        proxy_redirect off;
        proxy_set_header X-Forwarded-Proto $ scheme;
    }
}

然后

nginx -t
[emerg] 806#806: unknown directive "server_2" in /etc/nginx/conf.d/123.jp.conf:1
nginx: configuration file /etc/nginx/nginx.conf test failed
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/50711
 
33 次点击  
分享到微博