NGINX location rewrite

774 views Asked by At

Given the URL below, how can I have NGINX automatically pass the last part of the URL (not the GET Params but the last chunk of the base URL - myMap) to fastcgi_param SCRIPT_FILENAME?

The URL:

http://localhost/mapserver/myMap?&LAYERS=base....

NGINX config:

  location /mapserver/ {
        fastcgi_pass   unix:/tmp/mapserver.sock;
        fastcgi_index  mapserv?*;
        fastcgi_param  SCRIPT_FILENAME  /usr/lib/cgi-bin/mapserv?map=/mapfiles/myMap.map;
        include fastcgi_params; 
    }

Thanks.

1

There are 1 answers

0
Xavier Lucas On BEST ANSWER

The documentation states that you can use variables in fastcgi_param values :

Sets a parameter that should be passed to the FastCGI server. The value can contain text, variables, and their combination. These directives are inherited from the previous level if and only if there are no fastcgi_param directives defined on the current level.

So you can use a location with a regular expression and a capture group :

location ~ /mapserver/(.*)$ {
    fastcgi_pass   unix:/tmp/mapserver.sock;
    fastcgi_index  mapserv?*;
    fastcgi_param  SCRIPT_FILENAME  /usr/lib/cgi-bin/mapserv?map=/mapfiles/$1.map;
    include fastcgi_params; 
}

Be aware that this kind of location has a particular priority during request processing.