20

Is it possible to specify root path relative to the containing conf file?

We are developing a web app and keep app-nginx.conf file with app-specific configuration in the project's directory. I want to be able to include this file into main nginx.conf and have root path properly referencing required project's sub-directory. Example:

# /etc/nginx/nginx.conf
http {
    include /absolute/path/to/app/app-nginx.conf
}


# /absolute/path/to/app/app-nginx.conf
server {
    server_name localhost;
    listen 9090;

    root ./app;
}

After starting nginx with similar configuration nginx tries to resolve ./app path relative to nginx installation directory (/usr/local/Cellar/nginx/1.4.3/ in my case), not to the app's directory.

11

Is it possible to specify root path relative to the containing conf file?

Short answer: No.

Long answer: Nginx, by default, only knows about the path such as --prefix and --conf-path that are set at the compile time (ref: https://nginx.org/en/docs/configure.html). We can find various paths used by Nginx by running the command nginx -V (it's capital "V"). So, --conf-path is where the main nginx.conf file resides and that path is used for any relative paths.

According to the official documentation (https://nginx.org/r/root), a root directive can contain other variables, though.

Thanks for @cyker for clarifying after 5 years of posting the initial answer!

5
  • Thank you, Pothi. Unfortunately neither of the options works for me. --prefix can affect other nginx servers and from my personal perspective a little bit harder to manage comparing to configuration file. And nginx doesn't allow to create custom variables with set directive outside server context, so I can't move absolute path from my app-nginx.conf file. – Anton Moiseev Oct 24 '13 at 13:57
  • So, that path is used for any relative paths. I think this is inaccurate. It seems nginx will use config dir for options include and ssl_certificate, for example, but will use prefix dir for options such as root. I don't seem to come up with a full list about which one each option uses. – Cyker Jul 27 '18 at 12:21
  • @Cyker You are right. Updated the answer now. Of course, it isn't complete. But, better than the original answer (that was indeed wrong / inaccurate). – Pothi Kalimuthu Jul 28 '18 at 4:22
  • I'm reading Nginx From Beginner to Pro, it's noted that: "You have already seen the usage of root directive inside a location block. The root directive tells Nginx to return /image/myimage.jpg instead of fully qualified path /etc/nginx/html/app1/image/myimage.jpg." so IMHO it means that Nginx config support relative pathes ? – Webwoman Sep 18 '18 at 17:47
  • Here, the question is specific to "root" directive and whether it could support "relative path to conf file". – Pothi Kalimuthu Sep 19 '18 at 3:06
0

I haven't found a way to use relative paths or define global variables, but this little trick solves my problem.

2
  • 3
    What little trick? – Michael Hampton Oct 24 '13 at 16:40
  • I can't define variable, but I can use map directive with single default value that will play role of the variable for me. – Anton Moiseev Oct 25 '13 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.