Nginx rewrites - need some help
I want to do the following:
If file/directory exists -> display
If not -> rewrite to redirect.php?shorturl=
e.g. the request /bfsld/fds/sfd goes to redirect.php?shorturl=/bfsld/fds/sfd - Just like I want.
I want the following as well: /bfsld/fds/sfd.php –> redirect.php?shorturl=/bfsld/fds/sfd.php
But right now, it simply shows a 404. I tried in the #nginx channel on IRC and the #linode channel where dwfreed tried to help me (Thanks!) but I haven't been able to get a working solution.
This is my config:
dwfreed suggested to uncomment line 17, and comment line 23 - but after doing this the following request: /bfsld/fds/sfd no longer gets rewritten, and things ending in .php don't work either, heh.
Hopefully someone knows a way to do this. Thanks!
8 Replies
error_page 404 = /redirect.php?shorturl=$uri;
Something similar to this should work:
if (!-f $request_filename) {
return 301 $scheme://yoursite.com//redirect.php?shorturl=$request_uri;
#rewrite ^/(.*)$ /redirect.php?shorturl=$1 last;
else
return 404;
}
After putting that into your virtual host configuration, execute 'nginx -t' without quotes to make sure you entered it okay. If so, restart nginx and test.
@obs:
tryfiles
http://wiki.nginx.org/HttpCoreModule#try files is preferred over if
If you know of a proper way to do this with try_files I'd love to know, since when we tried this as seen in my config it didn't seem to work :/
One thing I just noticed was the "=404" in your PHP block's tryfiles. Normally that would be good to keep for security (hence why I missed it – I'm used to seeing it in my own config), but could interfere with your rewrite. It could be interfering with the rewrite by sending a 404 before the rewriting the URL. Try using the same exact tryfiles you use earlier in your configuration; if it doesn't work, you can always re-add the "=404".
If removing the "=404" makes you nervous, some other tips to mitigate the risk can be found on Nginx's wiki in this article
If that doesn't make your rewrite work, and nobody can figure out why, than my suggested if statement may be your best option. obs is right, tryfiles should be used instead of an if statement, but if tryfiles won't work, you'd be best using an if statement instead.
Two suggestions to clean up your configuration:
*Place gzip_static and index in nginx.conf within the http block. That will make both apply to ALL your virtual hosts.
*Unless you have a specific need to override the settings in fastcgiparams, your PHP block should need only tryfiles, include, and fastcgi. If removing all else causes it to stop working, you can find a working fastcgi_params in this Nginx wiki article
Edit: removing square brackets in the above list (can't get phpBB's list working).
Interestingly enough - this seems to completely reverse the issue, heh. Using tryfiles outside the locationblock, and removing =404 from PHP now causes everything ending in .php rewriting as it it supposed to.
But if I try /bfsld/fds/sfd/ (without ending in .php) it just gives a normal 404 and does not rewrite.
So some progress in a sense, but not completely there yet
EDIT: Using tryfiles both in- and outside the location block seems to fix it oO
EDIT2: for the =404 PHP security mitigation I changed cgi.fix_pathinfo=0 as suggested in the article you linked. Thanks!
Although including the "location /" block could be interfering with the try_files that's outside the block. I've never tried this (nor seen anyone else try it), so I don't know the full effects of doing so. If you don't really need it, try removing it (or commenting it out).
@Piki:
Needing to include both try_files seems a bit odd…
Although including the "location /" block could be interfering with the try_files that's outside the block. I've never tried this (nor seen anyone else try it), so I don't know the full effects of doing so. If you don't really need it, try removing it (or commenting it out).
Seems like that was the cause. Commenting out the location / fixed that. Only one try_files required now