0

i'll need a bit of help for alias on folder with nginx

I have my folder www/ with the container of my site example.com and a lot of folder like client0, client1, client2... I should NOT modify www/example/ but i need that example.com/serveur0/ to be redirected to www/client0/

I made a nginx rule like this :

location /serveur0/ {
    alias /www/client0/;
    index index.php
    location ~ /serveur0/(.*\.php)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$1;
        include /etc/nginx/fastcgi_params;
    }
}

and it work perfectly. But i have some issues when i try to generalize it, using regex. I tried this

location /serveur([0-9]+)$/ {
    alias /www/client$1/;
    index index.php
    location ~ /serveur$1/(.*\.php)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$1;
        include /etc/nginx/fastcgi_params;
    }
}

And it doesn't work, and i fail to understand why. Could you help me?

Uwe Keim
  • 2,420
  • 5
  • 30
  • 47
Totobro
  • 1
  • 1
  • 2

2 Answers2

1

At the first location block:

  1. You forgot to specify a prefix ~ (or ~*) to use regular expressions.

  2. Remove the dollar sign ($) before the trailing slash.

location ~ /serveur([0-9]+)/ {
    alias /www/client$1/;
    index index.php
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        error_page 404 /error/404.php;
    }
}
quanta
  • 51,413
  • 19
  • 159
  • 217
  • Still doesn't work. My coworker want to hardcode hundred of locations in the file, and i don't want him to do that T_T – Totobro Aug 09 '12 at 07:45
  • What did you get? error_log? – quanta Aug 09 '12 at 07:48
  • You're talking about /var/log/nginx/error.log ? If so, there's nothing in. I emptied it, restarted the service and still doesn't work or display anything. – Totobro Aug 09 '12 at 08:15
  • What did you get when access to `/serveur1`? Show the full config? – quanta Aug 09 '12 at 08:25
  • I got a 404 not found error. The full config file is available on http://pastebin.com/V5uGNkBV – Totobro Aug 09 '12 at 08:39
  • Sorry. I forgot to specify a prefix `~`, too. Updated. – quanta Aug 09 '12 at 08:43
  • Still not working, but i juste saw that the last slash in alias is useless, so i tried alias /home/example/www/clients/client$1 and now i have a 403 error, and logs saying 'directory index of "/home/example/www/clients/client0" is forbidden' – Totobro Aug 09 '12 at 09:01
  • Does `/home/example/www/clients/client0/index.php` exist? – quanta Aug 09 '12 at 09:50
  • Yes it does exist, and it's not a mistake in the folders name. If only it was that > – Totobro Aug 09 '12 at 11:37
0

Here's a revised version of the configuration that includes a few improvements:

location ~* ^/serveur([0-9]+)/(.*)$ {
    alias /www/client$1/$2;
    try_files $uri $uri/ /index.php;

    location ~* \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        include /etc/nginx/fastcgi_params;
    }
}

Here are the changes that I made:

  • I changed the regular expression in the location directive to use the ~* modifier, which makes the matching case-insensitive. This allows you to handle requests for /serveurX/ and /serveurx/ in the same way.
  • I added the try_files directive to handle request that don't match a file in the directory. In this case, it will fall back to the /index.php file.
  • I changed the fastcgi_param directive to set the SCRIPT_FILENAME environment variable to $request_filename, which is the absolute file path of the requested file.
  • I changed the regular expression in the nested location block to match .php files using the ~* modifier, which makes the matching case-insensitive.

This configuration should work as expected, handling requests for /serveurX/ URLs, passing requests for PHP files to the FastCGI server for processing, and serving a default index.php file when a requested file is not found.

Salim Aljayousi
  • 341
  • 1
  • 3