Redirection: mod_wsgi and django

1.3k views Asked by At

following this guide, I have setup Mailman 3 with Apache and mod_wsgi on a Debian server.

The .conf-file of my virtualhost:

ErrorLog /var/log/mailman-web/mailman-web.log
CustomLog /var/log/mailman-web/mailman-web_access.log combined
WSGISocketPrefix run/wsgi
<VirtualHost *:80>
    ServerName XXXXXXX
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # Here, use the value of the STATIC_ROOT variable in your Django configuration file (production.py)
    Alias /robots.txt  /usr/local/mailman/mailman-bundler/var/mailman-web/static/hyperkitty/robots.txt
    Alias /favicon.ico /usr/local/mailman/mailman-bundler/var/mailman-web/static/hyperkitty/favicon.ico
    Alias /static      /usr/local/mailman/mailman-bundler/var/mailman-web/static

    <Directory "/usr/local/mailman/mailman-bundler/var/mailman-web/static">
        Order deny,allow
        Allow from all
        Require all granted
    </Directory>

    WSGIScriptAlias / /usr/local/mailman/mailman-bundler/bin/mailman-web.wsgi
    WSGIDaemonProcess mailman-web display-name=mailman-web maximum-requests=1000 processes=1 threads=1 python-path=/usr/local/mailman/venv/lib/python2.7/site-packages

        <Directory "/usr/local/mailman/mailman-bundler/bin">
            <Files mailman-web.wsgi>
                    Order deny,allow
                    Allow from all
                    Require all granted
            </Files>
            WSGIProcessGroup mailman-web
        </Directory>
</VirtualHost>

The problem I have with this setup is, when I go to http://myhost/ the wsgi script redirects my browser to http://myhost/archives. I would like to have http://myhost/ redirected to http://myhost/mailman3 and not http://myhost/archives.

Trying to find out where mailman decides to return the archives subdirectory, I looked into the wsgi script defined in the apache .conf-file but there is not much more happening then importing some classes and calling another script. Following this script I landed in the file ยด./eggs/Django-1.10.4-py2.7.egg/django/core/handlers/wsgi.py`, specifically this part:

148 class WSGIHandler(base.BaseHandler):
149     request_class = WSGIRequest
150 
151     def __init__(self, *args, **kwargs):
152         super(WSGIHandler, self).__init__(*args, **kwargs)
153         self.load_middleware()
154 
155     def __call__(self, environ, start_response):
156         set_script_prefix(get_script_name(environ))
157         signals.request_started.send(sender=self.__class__, environ=environ)
158         try:
159             request = self.request_class(environ)
160         except UnicodeDecodeError:
161             logger.warning(
162                 'Bad Request (UnicodeDecodeError)',
163                 exc_info=sys.exc_info(),
164                 extra={
165                     'status_code': 400,
166                 }
167             )
168             response = http.HttpResponseBadRequest()
169         else:
170             response = self.get_response(request)
171 
172         response._handler_class = self.__class__
173 
174         status = '%d %s' % (response.status_code, response.reason_phrase)
175         response_headers = [(str(k), str(v)) for k, v in response.items()]
176         for c in response.cookies.values():
177             response_headers.append((str('Set-Cookie'), str(c.output(header=''))))
178         start_response(force_str(status), response_headers)
179         if getattr(response, 'file_to_stream', None) is not None and environ.get('wsgi.file_wrapper'):
180             response = environ['wsgi.file_wrapper'](response.file_to_stream)
181         return response

I guess that the decision of the subdirectory returned happens somewhere here but I could be wrong, not sure.

What I tried so far: In the apache conf file I added a line Redirect permanent / http://myhost/mailman3 but this made apache go into a redirection loop ending in a url like http://myhost/mailman3mailman3mailman3....

I hope someone can help me with this problem.

Thanks in advance!

1

There are 1 answers

1
bruno desthuilliers On BEST ANSWER

I guess that the decision of the subdirectory returned

This is a Django project, so urls are not about subdirectories, they are matched to "view" functions in Django. In this case, the "decision" happens here:

https://gitlab.com/mailman/mailman-bundler/blob/master/mailman_web/urls.py

urlpatterns = patterns('',
    url(r'^$',RedirectView.as_view(url=reverse_lazy('hyperkitty.views.index.index'))),
    url(r'^mailman3/', include('postorius.urls')),
    url(r'^archives/', include('hyperkitty.urls')),
    url(r'', include('social.apps.django_app.urls', namespace='social'), {"SSL": True}),
    url(r'', include('django_browserid.urls'), {"SSL": True}), 
)

and you can see that the "root" url ("r'^$'") is set to redirect to hyperkitty.views.index.index (hyperkitty being the archiver)

If you want to have the root url redirecting to "/mailman3/" instead, you'll have to change the first url() line to

url(r'^$',RedirectView.as_view(url="/mailman3/")),

Note that you'll then have to maintain this fork each time you update the package.

NB : there are "better" (safer / cleaner / easier to maintain) ways to do the same thing (by using your own django settings module instead of the default and making it's ROOT_URL_CONF point to your owner copy/pasted/edited version of this urls.py file, and finally by using a proper reverse_lazy instead of hardcoding the url), but if you don't know much about Django the above (somewhat Q&D) solution is by far the simplest.