Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Mon Jul 01, 2013 12:12 pm 
Offline
Senior Member

Joined: Fri Feb 17, 2012 8:20 pm
Posts: 365
Hi,

I want to do the following:

If file/directory exists -> display
If not -> rewrite to redirect.php?shorturl=<request>

Now, the following config works almost the way I want it. It rewrites stuff, EXCEPT things that end in PHP. I want it to rewrite that as well.
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: http://p.ngx.cc/1e49459e88411e86
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!


Last edited by Nuvini on Wed Jul 03, 2013 3:22 pm, edited 1 time in total.

Top
   
PostPosted: Tue Jul 02, 2013 6:56 am 
Offline
Senior Member

Joined: Fri Feb 17, 2012 8:20 pm
Posts: 365
Got it - through a workaround though:

error_page 404 = /redirect.php?shorturl=$uri;


Top
   
PostPosted: Wed Jul 03, 2013 1:25 pm 
Offline
Senior Member
User avatar

Joined: Thu Jun 16, 2011 8:24 am
Posts: 412
Location: Cyberspace
Have you tried using an if statement? Generally, those should be avoided, however for a case like this I personally think it should be ok.

Something similar to this should work:

Code:
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.

_________________
Kris the Piki Geeker


Top
   
PostPosted: Wed Jul 03, 2013 2:39 pm 
Offline
Senior Member

Joined: Sun Mar 07, 2010 7:47 pm
Posts: 1970
Website: http://www.rwky.net
Location: Earth
try_files http://wiki.nginx.org/HttpCoreModule#try_files is preferred over if

_________________
Paid support
How to ask for help
1. Give details of your problem
2. Post any errors
3. Post relevant logs.
4. Don't hide details i.e. your domain, it just makes things harder
5. Be polite or you'll be eaten by a grue


Top
   
PostPosted: Wed Jul 03, 2013 3:23 pm 
Offline
Senior Member

Joined: Fri Feb 17, 2012 8:20 pm
Posts: 365
obs wrote:
try_files 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 :/

:mrgreen:


Top
   
PostPosted: Thu Jul 04, 2013 10:44 am 
Offline
Senior Member
User avatar

Joined: Thu Jun 16, 2011 8:24 am
Posts: 412
Location: Cyberspace
I'd expect placing the try_files outside a location block, as dwfreed suggested, should make it work. Unless you need rules specific to "location /" (rules that shouldn't apply to subdirectories), it shouldn't be used since anything outside a location block is normally global to the entire virtual host.

One thing I just noticed was the "=404" in your PHP block's try_files. 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 try_files 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, try_files should be used instead of an if statement, but if try_files 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 fastcgi_params, your PHP block should need only try_files, include, and fastcgi. If removing all else causes it to stop working, you can find a working fastcgi_params in this Nginx wiki article (it's the one I use, so I know it works).

Edit: removing square brackets in the above list (can't get phpBB's list working).

_________________
Kris the Piki Geeker


Top
   
PostPosted: Thu Jul 04, 2013 11:11 am 
Offline
Senior Member

Joined: Fri Feb 17, 2012 8:20 pm
Posts: 365
Thanks for your response Piki.

Interestingly enough - this seems to completely reverse the issue, heh. Using try_files outside the location_block, 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 try_files both in- and outside the location block seems to fix it o_O
EDIT2: for the =404 PHP security mitigation I changed cgi.fix_pathinfo=0 as suggested in the article you linked. Thanks!


Top
   
PostPosted: Thu Jul 04, 2013 11:20 am 
Offline
Senior Member
User avatar

Joined: Thu Jun 16, 2011 8:24 am
Posts: 412
Location: Cyberspace
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).

_________________
Kris the Piki Geeker


Top
   
PostPosted: Thu Jul 04, 2013 11:26 am 
Offline
Senior Member

Joined: Fri Feb 17, 2012 8:20 pm
Posts: 365
Piki wrote:
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 :mrgreen:


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group