NGINX 背后的 FastCGI 应用程序无法检测到使用了 HTTPS 安全连接
我在 Nginx 后面运行 FastCGI,需要检测何时通过 HTTPS 访问 url。但是,我的 Django Web 应用程序始终报告连接是 HTTP (request.is_secure() == False)。但是,SSL 设置正确,并且我已使用 SSL 检查器验证了我的 https:// url 的安全性。
如何让 Django 正确检测请求何时来自 HTTPS url?
我的 Nginx 设置是:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
listen 443 default ssl;
ssl_certificate /home/webapp/ssl.crt
ssl_certificate_key /home/webapp/ssl.key
server_name myapp.com;
access_log /home/webapp/access.log
error_log /home/webapp/error.log
root /home/mywebapp;
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8801;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
}
我使用以下命令启动 Django FastCGI 进程:
python /home/webapp/manage.py runfcgi method=threaded host=127.0.0.1 port=8801 pidfile=/home/webapp/fastcgi.pid
I'm running FastCGI behind Nginx, and need to detect when the url is accessed via HTTPS. However, my Django web application always reports that the connection is HTTP (request.is_secure() == False). However, SSL is setup correctly, and I've verified my https:// urls are secure with an SSL checker.
How can I get Django to correctly detect when the request is from an HTTPS url?
My Nginx settings are:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
server {
listen 80;
listen 443 default ssl;
ssl_certificate /home/webapp/ssl.crt
ssl_certificate_key /home/webapp/ssl.key
server_name myapp.com;
access_log /home/webapp/access.log
error_log /home/webapp/error.log
root /home/mywebapp;
location / {
# host and port to fastcgi server
fastcgi_pass 127.0.0.1:8801;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_pass_header Authorization;
fastcgi_intercept_errors off;
}
}
}
I start the Django FastCGI process with:
python /home/webapp/manage.py runfcgi method=threaded host=127.0.0.1 port=8801 pidfile=/home/webapp/fastcgi.pid
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢Yuji的回答。我已经更新了我的服务器块,以有条件地注入 HTTPS 打开或关闭 HTTPS,具体取决于 $server_port:
{
}
Thanks to Yuji for the answer. I've updated my server block to conditionally inject HTTPS on or HTTPS off, depending on $server_port:
{
}
确保 nginx 正在为 443 上的连接发送
fastcgi_param HTTPS on
。Make sure nginx is sending
fastcgi_param HTTPS on
for connections on 443.