-1

I'm having trouble configuring nginx to return the url I want. I'd like http://domain.com/blah?abc=e9800ecf8 to return http://domain.com/createpage.php?abc=e9800ecf8, i.e. it preserves the abc variable in $_GET

createpage.php is sitting in /www/webroot, and blah doesn't exist. How do I do this?

Here's my server block of the config file (obviously, not doing what I want):

server {
    root /www/webroot;
    index index.html;

    location / {
        index index.html;
    }
    location = /blah {
      return 301 /createpage.php;
    }
}

1 Answer 1

2
server {

    root /www/webroot;
    index index.html;

    location / {

        if ($request_uri ~ "^/blah\?abc=([a-z0-9]+)$") {
            return 301 /createpage.php?abc=$1;
        }

    }

}
6
  • I presume you meant [a-z0-9] ? It's still giving 404s after loading the configuration file, even if I just try to return 301 index.html.
    – Escher
    Mar 21, 2015 at 23:02
  • @Escher Yes. What does your error log says ? index.html is not a valid URI. Btw, is it your only server block ? Because if not you need to define the server_name directive according to your domain. Mar 21, 2015 at 23:13
  • Error log is empty. Access log is 127.0.0.1 - - [22/Mar/2015:00:10:26 +0100] "GET /blah?abc=a1723 HTTP/1.1" 404 177 "-" "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:36.0) Gecko/20100101 Firefox/36.0" i.e. you can see that I'm testing it on a small test server before it goes on the production one (so server_name is not needed for the moment; I'm trying to have the simplest config file possible to isolate the problem. You're right about the URI, I changed it to / and it loads the index page as intended.
    – Escher
    Mar 21, 2015 at 23:27
  • @Escher Well there's no point it would not work except if the server_name directive doesn't match because you have more than one server block loaded by your configuration or you did not reload nginx or you are have typos somewhere. Mar 21, 2015 at 23:30
  • Found the error as I was commenting out line by lin: something in /etc/nginx/sites-enabled/* was interfering. The 301 return works, however it just shows me my php code, rather than passing it through the PHP interpreter module. I guess I need to work on the PHP FPM block now. Thanks for the solution.
    – Escher
    Mar 21, 2015 at 23:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .