Django 无效HTTP_HOST标头:"/run/gunicorn.sock:"。根据 RFC 1034/1035,提供的域名无效



我需要一些帮助。我有一个Django网站,我添加了管理员通知,Django一直向我发送无效HTTP_HOST标头通知。

完整的错误消息是

[Django]错误(外部IP(:HTTP_HOST标头无效:'/run/gunicorn.sock:'。根据RFC 1034/1035,提供的域名无效。

这是我的Nginx配置

server {

if ($host !~ ^(XX.XX.XX.XX|example.com|www.example.com)$ ) {
return 444;
} # Deny illegal Host headers

if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80; 
server_name example.com www.example.com XX.XX.XX.XX;                             
access_log off; 
return 301 https://$host$request_uri; 
}
server {
server_name example.com www.example.com XX.XX.XX.XX;
if ($host !~ ^(XX.XX.XX.XX|example.com|www.example.com)$ ) {
return 444;
} # Deny illegal Host headers
location = /favicon.ico { access_log off; log_not_found off; }
location /assets/ {
root /home/joe/example;
}
location /media/ {
root /home/joe/example;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

问题是nginx按原样传递主机名。您可以更新主机标题:

location / {
proxy_set_header Host $host; // Add this
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}

proxy_params文件中的注释:

# proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

相关内容

最新更新