1

I have CMS running on domain.tld.

Now I would like to configure Nginx to server another app from domain.tld/app.

CMS in main domain shouldn't handle requests made to domain.tld/app.

Can this be achieved in the server block of main app:

server {
   server_name domain.tld;
   root /var/www/html/domain.tld;
   ...
   location /app {
       root /var/www/html/app;
       ...
       location .php {
           ...
       }
   }
}

And the question is about what to put in place of ...

Currently I've only achieved "No input file specified" error and not found since it tries to find index.php from /var/www/html/app/app/index.php.

0

In order to serve an app with a different document root from within a subdirectory, you should use the ^~ modifier on the prefix location so that other regular expression location blocks do not cause a conflict. See this document for details.

If the app uses PHP, you can use a nested location block to inherit the different root.

For example (and this is just a starting point):

location ^~ /app {
    root /var/www/html;

    index index.php;
    try_files $uri $uri/ /app/index.php;

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

        include       ...; # this is your system's fastcgi_params file
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass  ...; # this is a socket or IP:port for php-fpm
    }
}

I have left some ...s in as they depend on your specific installation. Notice that the root is appended to the URI to locate the physical file, so /app/index.php will be located at /var/www/html/app/index.php.

Finally, the app must know that it runs in a subfolder. When it requests resource files (such as .js and .css) it must prefix the URI with /app/, otherwise the main apps resources will be loaded.

  • Like magic! Made modifications according to your suggestion and it worked on the first reload. Big thanks! – user1271930 Sep 6 '16 at 18:49

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.