现在已经超过4个小时了,我无法让我的nginx服务器为我的Django应用程序使用我的SSL证书。通过https的Django Cookiecutter
这里是我的nginx.conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream app {
server django:5000;
}
server {
listen 80;
server_name www.example.com;
rewrite ^/(.*) https://www.example.com/$1 permanent;
}
server {
listen 443 ssl;
server_name www.example.com;
charset utf-8;
ssl on;
ssl_certificate /etc/nginx/ssl/1_www.example.com_bundle.crt;
ssl_certificate_key /etc/nginx/ssl/example_rsa.key;
location/{
try_files $uri @proxy_to_app;
}
location @proxy_to_app {
proxy_pass http://app;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
}
}
}
我还是设法所有http
流量重定向到https
,但是当我访问https://www.example.com
我得到一个漂亮ERR_CONNECTION_REFUSED
。
我能想到的在我的Django应用程序的唯一相关的部分是:
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
,但我认为这个问题是在nginx的水平。我通过pydanny使用Django Cookiecutter。未修改的nginx.conf文件可以找到here。我确实将Dockerfile编辑为ADD
我的证书。
谢谢!
我没有看到它允许443连接。检查https://docs.djangoproject.com/en/1.9/topics/security/#ssl-https,以确保你在正确的路径(在cookiecutter旁边) – chachan
@chachan谢谢!我与文档非常一致,但我仍然怀疑问题出在Nginx上。使用django项目的日志记录非常广泛,我似乎没有通过Nginx提出的请求。你认为Nginx配置中的http块会有效果吗? –