TransWikia.com

Serve two locations under one domain

Server Fault Asked by F.P on November 27, 2021

I have two applications that I want to serve simultaneously under one domain in nginx.

Folder structure

/var/www/phm
    /client
        /static
            /css
              main.css
          index.html
    /server
        /public
           /img
               intro.png

nginx.conf

    server_name phm.example.com;
    
    root /var/www/phm;
    index index.html index.php;
    
    location / {
        try_files $uri /client/$uri @server;
    }
    
    location @server {
        try_files /server/public/$uri /server/public/index.php$is_args$args =404;
    }
    
    location ~ .php {
        fastcgi_split_path_info ^/(.+.php)(.*)$;
        include fastcgi.conf;
        internal;
    }

My expectation is, that the following requests are answered with the according files

  • phm.example.com —> /var/www/phm/client/index.html
  • phm.example.com/static/css/main.css —> /var/www/phm/client/static/css/main.css
  • phm.example.com/img/intro.png —> /var/www/phm/server/public/img/intro.png

One Answer

I would try a config like this:

server_name phm.example.com;

root /var/www/phm/client;
index index.html index.php;

location / {
    try_files $uri =404;
}

location /img {
    root /var/www/phm/server/public;

    try_files $uri /index.php$is_args$args;
}

location ~ .php {
    fastcgi_split_path_info ^/(.+.php)(.*)$;
    include fastcgi.conf;
}

In this config, the default is to serve files from /var/www/phm/client directory. For the location /img, the root directory is changed to /var/www/phm/server/public.

This configuration will serve http://phm.example.com/img/intro.png from /var/www/phm/server/public/img/intro.png.

Answered by Tero Kilkanen on November 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP