Serving a subdirectory

Francis Daly francis at daoine.org
Fri Dec 6 20:47:43 UTC 2019


On Fri, Dec 06, 2019 at 04:47:08PM +0100, Thomas Schweikle wrote:

Hi there,

> OK. Trying with a completely different question:
> 
> My server serves /var/www/html directory-content as static pages to http://
> <server>/
> 
> How do I have to set it up to additionally serve http://<server>/chrony
> from /var/www/chrony?

  location /chrony/ {
    root /var/www;
  }

Although I would probably really use "location ^~ /chrony/ {".

And that is all you need, in the "common" case.


Separate from that, since you want to do special handling of "index.sh"
within "/chrony/", I would suggest nesting the location, like so:

  location ^~ /chrony/ {
    root /var/www;
    location ~ index\.sh$ {
      fastcgi_pass unix:/var/run/fcgiwrap.socket;
    }
  }

You will want

  index index.sh index.html;

somewhere. You probably will not want the try_files line that you have.

And you can put the 

  include /etc/nginx/fastcgi_params;

and fastcgi_param lines together somewhere -- inside this location{},
or in somewhere that will inherit in to it. The "best" place depends on
what else the server config is doing -- do you, for example, want to
handle /xxx/index.sh as well as /chrony/index.sh?


> I've tried with:

>     root /var/www/chrony;

That will cause the request /chrony to look for /var/www/chrony/chrony.

> But this leads to error 404 - not found.

>     root /var/www;
>     index index.sh index.html;
> 
>     location /chrony {
>         try_files $uri $uri/ $uri/index.sh;
>     }
> 
>     location ~ "index\.sh$" {
>         gzip off;
>         fastcgi_pass unix:/var/run/fcgiwrap.socket;
>         include /etc/nginx/fastcgi_params;
>         fastcgi_param DOCUMENT_ROOT $document_root;
>         fastcgi_param SCRIPT_FILENAME $request_filename;
>     }

That looks to me like it probably should work. At least for the requests
/chrony and /chrony/ -- anything like /chrony/x will probably fail.

> But this also lead to error - 404 - not found.

>     root /var/www/chrony;

Again, that will cause the request /chrony to look for /var/www/chrony/chrony.

> lead to 404 - not found.

>     root /var/www/chrony;

Same.

> lead to 404 - not found.

> /var/www/chrony:
> insgesamt 16
> drwxr-xr-x 2 root root 4096 Dez  4 17:17 .
> drwxr-xr-x 4 root root 4096 Dez  2 16:25 ..
> -rw-r--r-- 1 root root   95 Dez  3 13:42 chrony.css
> -rwxr-xr-x 1 root root 1712 Dez  3 16:07 index.sh

Most of the above configs are looking for /var/www/chrony/chrony/index.sh.

>     root /var/www/html;
>     index index.html index.htm index.nginx-debian.html;
> 
>     location / {
>         # First attempt to serve request as file, then
>         # as directory, then fall back to displaying a 404.
>         try_files $uri $uri/ =404;

As it happens, that description (and configuration) is pretty much
what happens when try_files is not used. So you can probably just omit
that line.

>     location /chrony {
>         root /var/www/chrony;

  root /var/www;

>         try_files $uri $uri/ $uri/index.sh;

The final argument to try_files is a url that must exist; it is not
a file to try. So if you request /chrony/x, this will do an internal
redirect to /chrony/x/index.sh.

You are probably better off including "index.sh" in the "index" directive,
and omitting this try_files line too.

>     location ~ "index\.sh$" {
>         gzip off;
>         fastcgi_pass unix:/var/run/fcgiwrap.socket;
>         include /etc/nginx/fastcgi_params;
>         fastcgi_param DOCUMENT_ROOT $document_root;
>         fastcgi_param SCRIPT_FILENAME $request_filename;

If the request here is /chrony/index.sh, then $document_root is
/var/www/html and $request_filename is /var/www/html/chrony/index.sh.

In nginx, one request is handled in one location{}. Only the config in,
or inherited into, that location matters.

You do not set "root" in this location; therefore the inherited value
is /var/www/html.

If this location is only intended to handle things below /chrony,
set root explicitly; or set root implicitly by nesting it inside the
location that does set root appropriately.

Good luck with it,

	f
-- 
Francis Daly        francis at daoine.org


More information about the nginx mailing list