我已经在 DigitalOcean 上部署了一个 Django 应用程序。 首先,当我尝试使用 https 和 ssl 保护它时,我收到了这个错误。
当我运行 nginx -t 时:
nginx: [emerg]/etc/nginx/sites-enabled/django:12 中的无效参数“server_name”
nginx: 配置文件/etc/nginx/nginx.conf 测试失败
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
listen 443 ssl
server_name domain.com
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias path/to/media;
}
# your Django project's static files - amend as required
location /static {
alias path/to/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias path/to/staticadmin;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
server {
listen 80;
server_name domain.com;
return 301 https://$host$request_uri;
}
此外,我可以使用ip地址访问该网站,但不能使用注册的域名。它导致400错误请求页面。 这可能是 settings.py 的问题吗?
引用 settings.pyALLOWED_HOSTS=['*']
。我在 ip_addresses() 函数中提供什么列表?
这两个问题有关联吗?
使用 Django v1.10.5
最佳答案
你在一堆行中缺少分号,这就是 nginx -t 失败的原因。
关于python -/etc/nginx/sites-enabled/django 中的无效参数 server_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42181879/