Zuriar
Zuriar

Reputation: 11734

Use nginx Rewrite root url to remove the url directory

I have this config which is trying to send all request to /login/ to be served from the php directory /var/www/saml_provider/www/ :

  location ^~ /login/ {
    root /var/www/saml_provider/www/;

    index moo.php;
    try_files $uri $uri/ /login$is_args$args =404;

    location ~ \.php$ {

        fastcgi_pass presentation-php-fpm:9000;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }
}

unfortunately it then expects the files to be at /var/www/saml_provider/www/login/ as root appends the url to the path. How can I rewrite this to remove the /login/ so that the files are found in the correct place? (NB moving files down into login directory is not an option). Thanks

Upvotes: 0

Views: 1460

Answers (1)

Richard Smith
Richard Smith

Reputation: 49722

You could rewrite /login to /saml_provider/www internally, and hide the latter from external access:

location ^~ /login {
    rewrite ^/login(.*)$ /saml_provider/www$1 last;
}
location ^~ /saml_provider/www {
    internal;

    root /var/www;
    index moo.php;
    try_files $uri $uri/ /saml_provider/www/index.php$is_args$args;

    location ~ \.php$ {
        try_files $uri =404;

        fastcgi_pass presentation-php-fpm:9000;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php_errors.log";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
    }
}

It simplifies the rewrite for root problem and avoids the use of an alias. But it does mean that the URI /saml_provider/www is removed from external access, but hopefully it is unlikely to collide with another application on the same server.

Upvotes: 1

Related Questions