基于SSL的虚拟主机,带有django和mod_wsgi

时间:2022-06-28 23:05:29

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

我有一个django Web应用程序,其中允许通过http访问某些URL,而其他URL只能通过http-secure访问。我需要在我的apache conf文件中设置virtualHost / mod_wsgi配置,以便可以通过两者访问同一个Web应用程序。我跟着这2个帖子

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

并且具有以下配置,我认为应该这样做。

NameVirtualHost *:80
<VirtualHost *:80>
    ServerAdmin me@mail.com
    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 me@mail.com
    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.

但是,必须通过http访问的URL正在被访问而没有任何问题,而那些必须通过https的URL正在获得404 - 未找到错误。我有一个装饰器,可以看到是否通过http访问视图,然后重定向到URL,其中http替换为https。所以url-mapping是正确的,因为我看到重定向发生(这意味着视图被调用)但是通过https,它得到404错误。

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.

我在网络服务器文档root / var / www / html /中有第三方php应用程序,它可以在https上正常工作。不知何故,该应用程序不会通过http或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.
  • 尝试在两个虚拟主机中使用相同的WSGIProcessGroup名称进行守护进程模式方法,但这也没有用。

  • I tried adding Listen 443 command, and apache complained of a port already bound error.
  • 我尝试添加Listen 443命令,而apache抱怨端口已绑定错误。

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.

由于你的所有评论,我更多地探讨了配置,发现有一个子conf文件在运行时被拉入,它定义了*:443虚拟主机(并且没有wsgiscript指令)。这就是Listen 443抛出错误的原因。这也解释了https上的网址404(虚拟主机处理*:443无法解析该网址)。现在我修改它,一切正常。在我原始问题的2个堆栈溢出帖中建议的方法是完美的并且正常工作。

1 个解决方案

#1


4  

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

你有apache2.conf中提到的“Listen 80”和“Listen 443”吗?这可能会有所帮助。

#1


4  

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

你有apache2.conf中提到的“Listen 80”和“Listen 443”吗?这可能会有所帮助。