简体   繁体   English

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

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

What I want to achieve is when I browse for http://example.com:8080 it gets redirected to https://example.com:8080 . 我想要实现的是当我浏览http://example.com:8080时将其重定向到https://example.com:8080

My web application is written in Django, and I have the following line in my settings: 我的Web应用程序是用Django编写的,在我的设置中有以下一行:

SECURE_SSL_REDIRECT = True

The httpd configuration for example.com looks like this: 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>

with these configurations when I browse http://example.com , I will get the following error: 使用这些配置,当我浏览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.

any thoughts? 有什么想法吗?

You can redirect traffic from 8080 to https: 您可以将流量从8080重定向到https:

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

Then enable ssl on port 443, which is the default port for https requests: 然后在端口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.

相关问题 静态文件未通过Apache为Django应用程序提供 - Static Files not served via Apache for Django Application 如何在apache上配置django并在apache中创建https,以便django url应该以https://打开 <ip> :80端口 - how to configure django on apache and make https in the apache so that django url should open with https://<ip>:80 port 如何将任何 api 请求从 http 重定向到 Z2B9AFB89A6ACC1575B159BFCA38D1 中的 https? - how to redirect any api request from http to https in django? 如何在Apache服务的Django网站中将站点从http:// SERVER-IP / APP更改为http:// SERVER-IP - How to change site from http://SERVER-IP/APP to http://SERVER-IP in Apache served Django website 指定 HTTP 或 HTTPS 时 Django 重定向不起作用 - Django redirect not working when specifying HTTP or HTTPS 在Python Django中从HTTP重定向到HTTPS - Redirect from HTTP to HTTPS in Python Django 将http重定向到Django中的https(使用sslserver) - Redirect http to https in Django (using sslserver) Django:为 Heroku 应用安全地将 http 重定向到 https - Django: redirect http to https safely for Heroku app 如何使用 Django Nginx 应用程序与 HTTP 和 Z0E8433F9A404F26F3BA601C14B1 请求? - How to use Django Nginx Application with both HTTP and HTTPS requests? 从Django应用程序重定向URL - Redirect url from django application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM