Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSL based virtual host with django and mod_wsgi

I have a django web application in which some URLs are allowed to be accessed over http while others have to be over http-secure only. I need to set up the virtualHost/ mod_wsgi configuration in my apache conf file, so that the same web application can be accessed over both. I followed these 2 posts

  • How to force the use of SSL for some URL of my Django Application?
  • Django and SSL question

and have the following configuration which I think should do the trick.

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ServerName www.mydomain.com

    ErrorLog server-logs/error_log
    CustomLog server-logs/access_log common

    WSGIScriptAlias /test /var/www/my_app_root/apache/django.wsgi

    <Directory /var/www/my_app_root>
      Order allow,deny
      Allow from all
    </Directory>    
</VirtualHost>

NameVirtualHost *:443
<VirtualHost *:443>
    SSLEngine On
    SSLCertificateFile /etc/httpd/conf/ssl/mykey.crt
    SSLCertificateKeyFile /etc/httpd/conf/ssl/mykey.key

    ServerAdmin [email protected]
    DocumentRoot /var/www/html
    ServerName www.mydomain.com

    ErrorLog server-logs/error_log
    CustomLog server-logs/access_log common

    WSGIScriptAlias /test /var/www/my_app_root/apache/django.wsgi

    <Directory /var/www/my_app_root>
      Order allow,deny
      Allow from all
    </Directory>
</VirtualHost>

However, the urls which have to be accessed over http are being accessed without any issues, while those which have to be over https are getting a 404 - not found error. I have a decorator which sees if a view is being accessed over http and then redirects to a url with the http replaced by https. So the url-mapping is correct since I see the redirect happening (that means the view gets called) but over https, it gets 404 error.

I have a 3rd party php application in the webserver document root /var/www/html/ which works fine over https. Somehow, that application is not facing any issue over http or https.

I tried the following

  • Tried the Daemon mode approach with the same WSGIProcessGroup name in both virtual hosts, but that did not work out either.
  • I tried adding Listen 443 command, and apache complained of a port already bound error.

Edit:

Due to all your comments, I explored the configurations a bit more and found that there was a sub conf file which was getting pulled in at run time which was defining the *:443 virtual host (and did not have the wsgiscript directive). That's why the Listen 443 was throwing an error. And that also explains the 404 for the url over https (the virtualhost handling *:443 was unable to resolve that url). Now I modified it and everything is working fine. The approaches suggested in the 2 stack overflow posts in my original question are perfect and work correctly.

like image 659
AjP Avatar asked Oct 09 '22 01:10

AjP


1 Answers

Do you have both "Listen 80" and "Listen 443" mentioned in your apache2.conf? This might be of some help.

like image 106
Parthan Avatar answered Oct 13 '22 09:10

Parthan