1

I have a directory that sometimes exists and sometimes does not. If requested and existent, all files in it should be made available, e.g. like this

location /my_dir {
  root /other/www/root
}

Thus if a user asks for http://myhost.example.com/my_dir/file and /my_dir exists, it should be served. However, if the directory /other/www/root/my_dir does not exist, nginx should act as if the location block was never defined and use the normal rules to handle the request. Specifically, the client should not be aware of this special rule if the directory currently does not exist, i.e. there should be no user-visible indicator for it.

One idea was to put the default configuration in a named location and change the above lines to the following (cf. How to write a DRY, modular nginx conf (reverse proxy) with named locations):

location /my_dir {
  root /other/www/root
  try_files $uri $uri/ @default;
}

location / {
  error_page 418 = @default;
  return 418;
}

location @default {
  # all default stuff like the default root, index etc. goes here
}

Does that make sense or is there a better way?

Update

Here are two example configurations for nginx:

# Example 1
server {
  listen 80;
  listen [::]:80;
  server_name www.example.com example.com
  root /var/www/www.example.com
  index index.html index.htm
}

# Example 2
server {
  listen 80;
  listen [::]:80;
  server_name www.example.com example.com
  return 301 https://$server_name/$request_uri
}

In each case, I would like to add the special rules for the /my_dir location but only if the special root directory /other/www/root currently exists. If it doesn't, the client should not notice the special treatment for the /my_dir location.

  • Maybe I'm missing something, but it seems like the server shouldn't need anything special. If the requested location does not exist, nginx should serve it as any other location that does not exist. I'm not clear on why some additional configuration would be required. – Paul Jan 01 '16 at 19:32
  • @Paul, e.g. in case where the default action is to do a `return 301 http://...` to forward to another site or using https-by-default. – Michael Schlottke-Lakemper Jan 01 '16 at 19:45
  • Your question currently states "...nginx should act as if the location block was never defined and use the normal rules to handle the request." This would mean that you do not want anything other than the default action, but your comment suggests that you want something other than the default action. I'm not clear what you're asking. – Paul Jan 01 '16 at 19:56
  • Sorry, I realize that my question is not as clear as it could be. It boils down to the following: if the resource in question exists, it should be served. If it doesn't, the client should not even be aware of the fact, that there is a special provision in place for that resource, ie no temporary redirect or anything like that. – Michael Schlottke-Lakemper Jan 01 '16 at 22:22
  • Please post an `nginx.conf` file. – Paul Jan 01 '16 at 22:31
  • @Paul, I just did that. I hope this makes things clearer. If not, I'd be happy to provide more information. Thanks for the patience! – Michael Schlottke-Lakemper Jan 02 '16 at 07:43

0 Answers0