Search code examples
nginxgrav

try_files directive for multiple grav installations


At first I installed grav to a sub-level directory of a nginx server and it works fine: cadoankitovua.com/blog

The conf file:

server {
    listen 80;
    server_name cadoankitovua.com www.cadoankitovua.com *.cadoankitovua.com;
    root /home/grav/web/public/;

    location / {
        index index.php index.html index.htm;
        try_files $uri $uri/ /blog/index.php?_url=$uri&$query_string;
    }
    location ~ \.php$ {
        include /opt/local/etc/nginx/fastcgi.conf;
        fastcgi_pass 127.0.0.1:9000;
    }
}

I, then, installed an identical copy of grav to the /text directory and tried to modify the conf file in many different ways that I know of. None works.

I have 2 questions:

  1. How do I edit the conf file to make grav work on both /blog and /text directories?

  2. In general, how do I make many (>2) installations of grav work if I install them in many sub-level directories?

Thanks in advance.


Solution

  • I know nothing about grav, but you might start by adding a location /text block:

    server {
        listen 80;
        server_name cadoankitovua.com www.cadoankitovua.com *.cadoankitovua.com;
        root /home/grav/web/public;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /blog/index.php?_url=$uri&$query_string;
        }
        location /text {
            try_files $uri $uri/ /text/index.php?_url=$uri&$query_string;
        }
        location ~ \.php$ {
            include /opt/local/etc/nginx/fastcgi.conf;
            fastcgi_pass 127.0.0.1:9000;
        }
    }
    

    EDIT: A general solution would use a named location with a rewrite statement, for example:

    location / {
        try_files $uri $uri/ @rewrite;
    }
    location @rewrite {
        rewrite ^(/[^/]+) $1/index.php?_url=$uri&$query_string last;
        rewrite ^ /blog/index.php?_url=$uri&$query_string last;
    }