正如标题所说,我想在具有不同子域的同一个 Droplet(NGINX、Gunicorn、ubuntu)中托管两个不同的 django 项目。其中一个是我们的主站点 example.com。它已启动并运行并完美运行。我们希望在同一个 Droplet 中托管临时站点 staging.example.com。
我们已经为暂存站点创建了新的套接字和服务文件并激活并启用了它们,但问题是 nginx 仍然指向主域目录中的文件而不是暂存目录,因此即使已添加这些域,我们也会在下面收到此错误在暂存站点的 settings.py 允许的主机中
DisallowedHost at / 无效的 HTTP_HOST 标头:“staging.example.com”。您可能需要将 >'staging.example.com' 添加到 ALLOWED_HOSTS
这是我们的 staging.guinicorn.service 文件
[Unit]
Description=staging.gunicorn daemon
Requires=staging.gunicorn.socket
After=network.target
[Service]
User=admin
Group=www-data
WorkingDirectory=/home/admin/example1staging
ExecStart=/home/admin/example1staging/venv/bin/gunicorn --access-logfile - --workers 3 --bind unix:/run/staging.gunicorn.sock djangoproject.wsgi:application
[Install]
WantedBy=multi-user.target
这是我们的 staging.guicorn.socket 文件
[Unit]
Description=staging.gunicorn socket
[Socket]
ListenStream=/run/staging.gunicorn.sock
[Install]
WantedBy=sockets.target
最后是我们的 nginx 配置
server {
listen 80;
listen [::]:80;
server_name example.com www.example.com;
return 302 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
ssl_client_certificate /etc/ssl/cloudflare.crt;
ssl_verify_client on;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/admin/example1;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
server {
listen 80;
listen [::]:80;
server_name staging.example.com www.staging.example.com;
return 302 https://$server_name$request_uri;
}
server {
# SSL configuration
listen 443 ssl http2;
listen [::]:443 ssl http2;
ssl_certificate /etc/ssl/cert.pem;
ssl_certificate_key /etc/ssl/key.pem;
ssl_client_certificate /etc/ssl/cloudflare.crt;
ssl_verify_client on;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/admin/example1staging;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/staging.gunicorn.sock;
}
}
任何形式的帮助将不胜感激!
您的两个 SSL 配置块都缺少
server_name
指令。因此 nginx 对所有请求使用默认的虚拟主机,在这种情况下,它是第一个侦听端口 443 的主机。您需要添加适当的
server_name
指令来解决问题。听 443 ssl http2;
server_name example.com www.example.com;
听 443 ssl http2;
server_name staging.example.com www.staging.example.com;
请在 https 部分更改您的 nginx 文件