can mod_wsgi-express be used with RewriteRule in the included config?

190 views
Skip to first unread message

Gavin Cannizzaro

unread,
Jan 12, 2016, 12:50:53 PM1/12/16
to modwsgi
Hi all,

I love mod_wsgi-express.  As someone relatively new to Linux, Apache, Python, and WSGI, "express" works as advertised by getting me up and running.  I'm already using it in production.

When I use a rewrite rule in the custom configuration, though, it is completely ignored.

My test case is just a few lines, calling mod_wsgi-express with a standard test script, a port number, a document-root, and an included config.

The included config has just

RewriteEngine On
RewriteRule ^ /blah [R=301]

(I don't actually want redirects, but it's a stronger test.)

In any case, I don't see the rule being picked up.  I know that I have mod_rewrite enabled, and it works in a regular Apache site.  And even with express, I'm sure that the module is loaded, based on an IfModule rule that sets a header.

Is this supposed to be supported with express?  Does it need to be used in conjunction with some other configuration?  I've seen Graham suggest rewrite rules in reference to various issues, but not specifically where "express" is involved.

Thanks!
Gavin

Graham Dumpleton

unread,
Jan 12, 2016, 5:24:00 PM1/12/16
to mod...@googlegroups.com
Unfortunately it isn’t that simple. Because mod_wsgi-express uses rewrite rules to allow the WSGI application to overlay a static document root, adding rewrite rules at the end of the configuration file at top level will not be recognised, or if put inside of a Location directive will usually screw things up. Any rewrite rules likely need to be supplied separately with an option used to point at the rewrite file. The rules would then need to be included in a particular location within the generated configuration just after the rewrite rules mod_wsgi uses. Even then there may be complications or rules would need to be written a certain way.

The alternative for simple redirect rules is to use the redirect directives from mod_alias.


You can thus quite happily use something like:

    Redirect /dog /cat
    RedirectMatch ^/?$ /index

For more complicated rewrite rules, would need to see what exactly you are trying to do.

I will though have a quick try at adding a —rewrite-rules option and include it at where I think they need to be and see what is possible.

BTW, if using Apache 2.4, it is very easy to enable rewrite trace logs by doing:

    mod_wsgi-express start-server tests/environ.wsgi --include-file redirect.conf --log-level 'info rewrite:trace8' --log-to-terminal

That is, use —log-level with quoted option ‘info rewrite:trace8’.

That way you can see whether rewrite rules are applied at all, what they are being given and what it generates.

Graham

Graham Dumpleton

unread,
Jan 12, 2016, 5:42:44 PM1/12/16
to mod...@googlegroups.com
A quick test of hacking into the generated configuration file:

<Directory '/tmp/mod_wsgi-localhost:8000:502/htdocs/'>
<IfDefine MOD_WSGI_DIRECTORY_INDEX>
    DirectoryIndex None
</IfDefine>
<IfDefine MOD_WSGI_DIRECTORY_LISTING>
    Options +Indexes
</IfDefine>
<IfDefine MOD_WSGI_CGI_SCRIPT>
    Options +ExecCGI
</IfDefine>
<IfDefine MOD_WSGI_CGID_SCRIPT>
    Options +ExecCGI
</IfDefine>
<IfDefine !MOD_WSGI_STATIC_ONLY>
    RewriteEngine On
    Include /Users/graham/Projects/mod_wsgi/rewrite.conf
    RewriteCond %{REQUEST_FILENAME} !-f
<IfDefine MOD_WSGI_DIRECTORY_INDEX>
    RewriteCond %{REQUEST_FILENAME} !-d
</IfDefine>
<IfDefine MOD_WSGI_SERVER_STATUS>
    RewriteCond %{REQUEST_URI} !/server-status
</IfDefine>
    RewriteRule .* - [H=wsgi-handler]
</IfDefine>
    Order allow,deny
    Allow from all
</Directory>

and then having in rewrite.conf:

RewriteRule ^/?$ /blah [R=301]

does appear to work.

BTW, another way might have been to use in the include configuration file:

<Directory /some/path/to/document/root>
AllowOverride FileInfo
</Directory>

then add a .htaccess file to the document directory and add rewrite rules in there.

Rewrite rules in .htaccess files are fiddly to get right though and does look a bit like they trying to add them there also stuffs up the mod_wsgi rewrite rules as no longer falls through to WSGI application and get not found instead.

Graham

Graham Dumpleton

unread,
Jan 12, 2016, 6:40:45 PM1/12/16
to mod...@googlegroups.com
If you want to try it out, I have pushed up new feature to allow rewrite rules.

Currently on develop branch of mod_wsgi Git repo.


The rewrite rules can be placed in the rewrite.conf file in the server root directory.

Better still, use the —rewrite-rules option to specify the location of your own file containing the rewrite rules.

For example:

$ mod_wsgi-express start-server tests/envirowsgi --rewrite-rules rewrite.conf --log-level 'info rewrite:trace8' --log-to-terminal
Server URL         : http://localhost:8000/
Server Root        : /tmp/mod_wsgi-localhost:8000:502
Server Conf        : /tmp/mod_wsgi-localhost:8000:502/httpd.conf
Error Log File     : /dev/stderr (info rewrite:trace8)
Rewrite Rules      : /Users/graham/Projects/mod_wsgi/rewrite.conf
Request Capacity   : 5 (1 process * 5 threads)
Request Timeout    : 60 (seconds)
Queue Backlog      : 100 (connections)
Queue Timeout      : 45 (seconds)
Server Capacity    : 20 (event/worker), 20 (prefork)
Server Backlog     : 500 (connections)
Locale Setting     : en_AU.UTF-8

You don’t need a RewriteEngine On directive.

Just include your RewriteCond/RewriteRule directives.

Avoid using RewriteBase as may well screw up what mod_wsgi rules do and not sure it is needed anyway in that context.

The rules should be applied before static file matching, so can also be used to remap static files as well.

A really simple example of a rewrite.conf file would be:

RewriteRule ^/?$ /blah [R=301]

This would cause access to root of site using just ‘/‘ to be redirected to ‘/blah’. Everything else should flow through as normal.

If you find any issues or have problems constructing the rules you want then let me know.

Graham

Graham Dumpleton

unread,
Jan 12, 2016, 6:44:38 PM1/12/16
to mod...@googlegroups.com
BTW, the rewrite.conf file can also be used as a back door for more easily enabling .htaccess files in the document root direct. Just include:

AllowOverride FileInfo

Need to think why I don’t have an option to for enabling them anyway.

Graham

Gavin Cannizzaro

unread,
Jan 12, 2016, 6:50:49 PM1/12/16
to modwsgi
Many thanks for the reply.

The rewrites are part of a file versioning scheme, where the "version" part of some paths is simply ignored.  So redirects won't apply.  But since it only involves static files under a definite location, it sounds like the Directory option will work for me.

That said, I did try an express-free setup---both from scratch and with an express-generated config.  I did not get it working either way.  I was hoping to host multiple sites on the same VM, and it looks like with express the only way to do that is via something like ProxyPass.  That works, except that (at least on my local) I get intermittent 502 errors with that setup.  It also messed with keep-alive connections.  So I abandoned that.  But I understand I could accomplish the same thing with Virtual hosts if I understood the generated config (which I don't yet).

I know I've changed subjects here, but... are there any requirements/tips for using moving a generated setup to a virtual host config?  When I tried it, I wasn't sure how to define all of the MOD_WSGI variables.

Thanks again,
Gavin

Gavin Cannizzaro

unread,
Jan 12, 2016, 6:54:07 PM1/12/16
to modwsgi
Thanks!  You're faster than me, so my other reply is out-of-date :)  I'll check out the update and let you know.

I appreciate you taking the time to consider this.


Gavin

Graham Dumpleton

unread,
Jan 12, 2016, 7:15:48 PM1/12/16
to mod...@googlegroups.com
Trying to take what it generates and repurpose it for a VirtualHost may be more trouble than its worth. Maybe easier to understand what you need and just highlight what the key parts of the configuration are that you need.

In part mod_wsgi-express is targeting a Docker based deployment environment where different hosts would be in different containers for haproxy or some other router in front. That is the reason why mod_wsgi had all this magic stuff added to help in fixing up host information in WSGI environment based on proxy headers. There are some WSGI middleware which do this, but generally don’t do it right and only support a subset of what headers are often used out there.

Graham

Graham Dumpleton

unread,
Jan 21, 2016, 6:54:01 PM1/21/16
to mod...@googlegroups.com
Did you every play with the rewrite rules feature?

Extra confirmation that it is working and useful before I release it would be appreciated.

Graham
Reply all
Reply to author
Forward
0 new messages