mod_wsgi icon indicating copy to clipboard operation
mod_wsgi copied to clipboard

Using WSGI in apache as a module: possible to stay on apache after a rewrite to a wsgi script?

Open TAlonglong opened this issue 3 years ago • 4 comments

OK

So I have a apache running fine with a rewrite to a WSGI script like this:

RewriteRule /call-some-wsgi-script /wsgi-script-alias

and

WSGIScriptAlias /wsgi-script-alias /scripts/wsgi-script.py

The wsgi script runs fine, getting something like this

[Wed May 04 06:16:10.692436 2022] [rewrite:trace1] [pid 9:tid 139791918434048] mod_rewrite.c(483): [client 172.16.0.170:47286] 172.16.0.170 - - [...] [perdir ^/call-some-wsgi-script$/] internal redirect with /wsgi-script-alias [INTERNAL REDIRECT]                                                                                           

And then some print logs from the wsgi script running fine.

And now my problem: after the WSGI script completes the script and apache return back to the client with what ever I give in the start_response

Is it possible to have the wsgi script do an internal redirect back to the apache, so that apache can do another rewrite?

Now I make my WSGI script return a external redirect 307 with the location set, and my client is handling this. But I want to avoid this external redirect.

I have tried looking around how to solve this, but no luck. But maybe I ask the wrong question(s).

Any hints or ideas?

TAlonglong avatar May 04 '22 06:05 TAlonglong

As I understand what you are asking, no. You need to tell the browser to send a new request via a redirect triggered using a redirect response and Location header, or by a refresh meta tag in the <head> of the HTML returned. It is not possible to have Apache somehow send two responses for a single request as part of multiple internal redirects.

GrahamDumpleton avatar May 04 '22 06:05 GrahamDumpleton

Perhaps you should explain the actual requirement rather than what you think is the solution, then I may have better ideas.

GrahamDumpleton avatar May 04 '22 06:05 GrahamDumpleton

Oh, wow, that was quick. Thanks.

Hm, requirement, I need to think about that.

TAlonglong avatar May 04 '22 07:05 TAlonglong

So I got stuck in other projects, but I still have this issue. It is working at the cost of an extra redirect.

Requirements vs what I think is the solution, now let see what I come up with.

I have a locationmatch in my apache config with some RewriteCond and RewriteRule:

<LocationMatch <mymatch>>
RewriteCond <config_file> -f
RewriteRule <cgi-bin rewrite with the config_file>

# But if this <config_file> does not exist I do a rewrite to a wsgi script that generate this <config_file>
RewriteRule ... /generate-config-file
# Now the process ends here (or I have made the wsgi script return a 307 redirect with a Location) but my requirement is that I also do the next rule instead of having the wsgi script return
RewriteRule <cgi-bin rewrite with the config_file>

</LocationMatch>

WSGIScriptAlias /generate-config-file /script/generate-config-file.py

Yeah, I don't know if this makes any sense but I give it a shot.

Thanks

TAlonglong avatar Jun 09 '22 15:06 TAlonglong