关于Nginx主目录及配置文件相对路径解析的技术咨询
Hey there, solid question—let’s clear this up once and for all!
When you use a relative path in an include directive inside Nginx’s main configuration file, it gets resolved strictly relative to the directory where the main config file (typically nginx.conf) is stored.
Let’s use a real-world example to make this concrete:
- If your main
nginx.conflives at/etc/nginx/ - You add the line
include file.conf;to it - Nginx will automatically look for that file at
/etc/nginx/file.conf
If you use a parent-directory relative path like include ../conf.d/file.conf;, it’ll resolve to /etc/conf.d/file.conf (since /etc/nginx/../ translates to /etc/).
A quick important note: There’s no separate "declared main path" that overrides this behavior. Nginx doesn’t let you configure a custom base directory for include directives—every relative path is tied directly to the location of the config file that contains the include line.
One edge case to keep in mind: If you start the path with a slash (e.g., include /etc/nginx/conf.d/file.conf;), that’s an absolute path, and Nginx will use it exactly as written without any relative resolution.
备注:内容来源于stack exchange,提问作者palmasd1

