nginxurl-rewritingwildcardforward

nginx forward wildcard urls


Can someone help me with how to set up an nginx forward that does the following:

This is what I have so far. I was going to adapt it but to be honest I can't get off the ground

location ~* ^/pdf/(.*)$ {
      rewrite ^/pdf/(\d+)*$ /pdf.php;
 }

    

I would like it to :

I know this is asking a lot, but I think this sort of thing is really useful and I can't see anyone doing it on the internet.


Solution

  • Here is one of the possible solutions:

    location ^~ /pdf/ {
        location ~ ^/pdf/(?<name>[^/]+)/?$ {
            rewrite ^ /pdf.php?book=$name;
        }
        return 403;
    }
    location = /pdf.php {
        internal;
        # your php handler here, e.g.:
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$uri;
        fastcgi_pass unix:/path/to/php.sock;
    }
    

    Requested name should be available as a book query argument inside your PHP script.