Mobile app version of vmapp.org
Login or Join
Megan663

: How to configure URI in Nginx to serve different web app than in the main domain? I have CMS running on domain.tld. Now I would like to configure Nginx to server another app from domain.tld/app.

@Megan663

Posted in: #Configuration #Domains #Nginx #Subdirectory #WebApplications

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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Megan663

1 Comments

Sorted by latest first Latest Oldest Best

 

@Cugini213

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme