0

What I want to achieve is when I browse for http://example.com:8080 it gets redirected to https://example.com:8080.

My web application is written in Django, and I have the following line in my settings:

SECURE_SSL_REDIRECT = True 

The httpd configuration for example.com looks like this:

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:

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?

3
  • 1
    You can't have both HTTP and HTTPS served from the one port. Port 8080 will only be able to accept HTTPS requests. Commented Aug 16, 2018 at 3:17
  • BTW, don't use python-path to point at your virtual environment site packages. Read modwsgi.readthedocs.io/en/develop/user-guides/… to see how to configure virtual environment correctly. Commented Aug 16, 2018 at 3:19
  • I'm delighted to have your comment under my post :) I remember that I had always problems when I used python-home to point at my virtual environment, and setting python-path resolved the issues (unfortunately I don't remember what was the problem). But now because of your comment, I removed python-path, and used python-home instead and it seems everything is working fine! I have no clue why :D Commented Aug 16, 2018 at 16:27

1 Answer 1

2

You can redirect traffic from 8080 to 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:

<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> 
Sign up to request clarification or add additional context in comments.

2 Comments

My first intention was to use both http and https on port 8080. But as Graham pointed out it's not possible. So I cannot mark this as correct answer. But your answer helped a lot to resolve my problem. thank you.
Np, I thought I'd expand on his comment a bit, glad it helped.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.