繁体   English   中英

如何将HTTP URL重定向到Apache服务的Django应用程序的https

[英]How to redirect http url to https for django application served by Apache

我想要实现的是当我浏览http://example.com:8080时将其重定向到https://example.com:8080

我的Web应用程序是用Django编写的,在我的设置中有以下一行:

SECURE_SSL_REDIRECT = True

example.com的httpd配置如下所示:

LISTEN 8080
<VirtualHost *:8080>
  ServerName example.com

  SSLEngine on
  SSLCertificateFile /path_to_cer
  SSLCertificateKeyFile /path_to_key
  SSLCertificateChainFile /path_to_iterm.cer

  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

  Alias /static /path_to_mysite/static
  <Directory /path_to_mysite/static>
      Require all granted
  </Directory>

  <Directory /path_to_mysite_wsgi_dir>
      <Files wsgi.py>
          Require all granted
      </Files>
  </Directory>

  WSGIDaemonProcess mysite python-path=/path_to_mysite:/path_to_mysite_python_packages display-name=%{GROUP}
  WSGIProcessGroup mysite
  WSGIApplicationGroup %{GLOBAL}
  WSGIScriptAlias / /path_to_mysite_wsgi.py
</VirtualHost>

使用这些配置,当我浏览http://example.com时 ,会出现以下错误:

Bad Request
Your browser sent a request that this server could not understand.
Reason: You're speaking plain HTTP to an SSL-enabled server port.
Instead use the HTTPS scheme to access this URL, please.

有什么想法吗?

您可以将流量从8080重定向到https:

<VirtualHost *:8080>
  ServerName example.com
  RewriteEngine on
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

然后在端口443上启用ssl,该端口是https请求的默认端口:

<VirtualHost *:443>
  ServerName example.com

  SSLEngine on
  SSLCertificateFile /path_to_cer
  SSLCertificateKeyFile /path_to_key
  SSLCertificateChainFile /path_to_iterm.cer

  RewriteEngine On
  RewriteCond %{HTTPS} off
  RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

  Alias /static /path_to_mysite/static
  <Directory /path_to_mysite/static>
      Require all granted
  </Directory>

  <Directory /path_to_mysite_wsgi_dir>
      <Files wsgi.py>
          Require all granted
      </Files>
  </Directory>

  WSGIDaemonProcess mysite python-path=/path_to_mysite:/path_to_mysite_python_packages display-name=%{GROUP}
  WSGIProcessGroup mysite
  WSGIApplicationGroup %{GLOBAL}
  WSGIScriptAlias / /path_to_mysite_wsgi.py
</VirtualHost>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM