現在已經超過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塊會有效果嗎? –