0

Nginx newbie here.

I control the domain eample.com and the goal is to have three distinct resources available at the following URLs:

  • example.com - static landing page
  • example.com/siteA - PHP application A
  • example.com/siteB - PHP application B

I have roughly the following file structure on disk:

/var/www
├── html
│   └── index.html
├── siteA
│   └── index.php
└── siteB
    └── public
        └── index.php

I have the following (simplified) Nginx configuration

server {
        root /var/www/html;
        index index.php index.html;

        server_name example.com;

        # Site A
        location /siteA {
                #root /var/www/siteA;
                alias /var/www/siteA/;
                include snippets/fastcgi-php.conf;
        }

        # Site B
        location /siteB/ {
                #root /var/www/siteB/public;
                alias /var/www/siteB/public/;
                include snippets/fastcgi-php.conf;
        }
}

The included file snippets/fastcgi-php.conf contains the following:

location ~ \.php$ {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        try_files $fastcgi_script_name =404;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;

        fastcgi_index index.php;
        include fastcgi.conf;

        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}

The static landing page at example.com works, but attempting to access example.com/siteA results in a Primary script unknown error.

The PHP-FPM pool access log contains lines like this:

- - 12/Nov/2019:15:57:03 +0200 "GET /siteA/index.php" 404

I believe that the actual file path that PHP-FPM is trying to open is /var/www/html/siteA/index.php, which is obviously wrong.

I'm pretty sure I'm messing up something related with the root and/or alias directives, but I can't quite put my finger on it.

How can I fix my configuration, without altering the disk file structure?

Any pointer would be appreciated.

Silviu G
  • 1,241
  • 10
  • 31
  • The naive way would be to set the real common `root /var/www;` and adjust it everywhere else. – Jared Farrish Nov 12 '19 at 14:38
  • Keep in mind that `root` appends the location while `alias` drops the location. – IVO GELOV Nov 12 '19 at 15:51
  • SiteA could use `root` instead of `alias`. SiteB needs to use `alias` unless you internally rewrite the URIs to include `public`. But your snippets file is designed to only work with root. – Richard Smith Nov 12 '19 at 16:32
  • @Richard: It is not mandatory to have a unified snippets file. – Silviu G Nov 12 '19 at 19:07
  • Where you must use an `alias`, take a look at the `/manage` block in [this answer](https://stackoverflow.com/questions/42443468/nginx-location-configuration-subfolders/42467562#42467562). – Richard Smith Nov 12 '19 at 19:16
  • I'll try that for `siteB`, but I still don't understand why `siteA` fails to work with the `alias` directive. It works with `root /var/www/` instead of `alias /var/www/siteA/`. – Silviu G Nov 12 '19 at 19:31

0 Answers0