0

I am trying to achieve the following:

The root of my domain http://www.paulix.local/ should server the file located at /var/www/html/main/webroot/index.php

The url http://www.paulix.local/malibushould serve the file located at /var/www/domain/malibu/webroot/index.php

The rewrite I am trying to achieve (url => script) :

  • / => /var/www/html/main/webroot/index.php
  • /path/whatever => /var/www/html/main/webroot/index.php?/path/whatever
  • /malibu => /var/www/domain/malibu/webroot/index.php
  • /malibu/path/whatever => /var/www/domain/malibu/webroot/index.php?/path/whatever

This is my current config

server {
    listen 80;
    listen [::]:80;

    rewrite_log on;
    access_log /var/log/nginx/paulix-access.log;
    error_log  /var/log/nginx/paulix-errors.log notice;

    server_name www.paulix.local;
    root /var/www/html/main/webroot;
    index index.php;

    # location 1
    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    # location 2
    location ~ /malibu {
        root /var/www/domain/malibu/webroot;
        try_files $uri $uri/ /index.php?$args;

        # location 3
        location ~ \.php$ {
            return 200 "Malibu is $document_root$fastcgi_script_name";
        }
    }

    # location 4
    location ~ \.php$ {
            return 200 "Global $document_root$fastcgi_script_name";
    }
}

If I hit the url http://www.paulix.local, it hits location 4 and I get the following output: Global /var/www/html/main/webroot/index.php which is what I'm expecting.

PROBLEMS

If I hit the url http://www.paulix.local/malibu it also hits location 4 with the same output as above, but I was expecting it to hit location 3, the expected output being: Malibu is /var/www/domain/malibu/webroot/index.php

Lastly, hitting http://www.paulix.local/malibu/index.php hits location 3, but not with the expected output. It outputs Malibu is /var/www/domain/malibu/webroot/malibu/index.php when I'd like it to be /var/www/domain/malibu/webroot/index.php

  • Note that the order of location stanzas is important. Try placing the malibu one as the first one. – wurtel May 8 at 11:45
1

Based on your needs, use location /malibu { or location ^~ /malibu {instead of location ~ /malibu {.

Here you can read an understandable overview how Nginx deals with location-blocks.

edit: Sorry, I haven't seen your last edit as I wrote my answer. I think it is alias you are searching for. Just replace root in location 2 with alias and the output should be fine. (Source)

  • Thanks for the suggestion, and the link, but it doesn't change anything – kaklon May 8 at 11:15
-1

Edit: The problem is due to a bug in Nginx

One solution is the one below, or even better this one

server {
listen 80 default_server;
listen [::]:80 default_server;
rewrite_log on;
access_log /var/log/nginx/paulix-access.log;
error_log  /var/log/nginx/paulix-errors.log notice;


server_name www.paulix.test;
root /var/www/html;

location / {
    root /var/www/html/main;
    if (-f $request_filename) {
        break;
    }

    # prevent recursion
    if ($request_uri ~ /webroot/index.php) {
        break;
    }

    #rewrite ^/$ / permanent; # do not uncomment, or face infinite rewrite
    rewrite ^/webroot/(.*) /webroot/index.php last;
    rewrite ^/(.*)$ /webroot/$1 last;

        location ~ index\.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

}

location /malibu {
        root /var/www/domain;

    if (-f $request_filename) {
        break;
    }

    # prevent recursion
    if ($request_uri ~ /webroot/index.php) {
        break;
    }

    rewrite ^/malibu$ /malibu/ permanent;
    rewrite ^/malibu/webroot/(.*) /malibu/webroot/index.php last;
    rewrite ^/malibu/(.*)$ /malibu/webroot/$1 last;

        location ~ index\.php$ {
            try_files $uri =404;
            include fastcgi_params;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
    deny all;
}

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.