nginx variable for path of config file

2019-07-08 03:01发布

问题:

Is there a way to specify, for example, that the root should be relative to the directory where the config file is living? Something like

root $conf_path/www

回答1:

In general, I don't believe this is possible. But, you might be able to hack something together based on this article.

Quoting relevant parts:

2ed version is here: How to reference OS Environment Variables in nginx.conf

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,215269,215278#msg-215278

and further

You can read system environment variables with ngx_lua enabled in your nginx build: http://wiki.nginx.org/HttpLuaModule

env PATH;
http {
    ...
    server {
        location /path {
            set_by_lua $path 'return os.getenv("PATH")';
            ...
        }
    }

BTW, to use the set_by_lua directive, you also need to enable the ngx_devel_kit module here: https://github.com/simpl/ngx_devel_kit (it'll be easier if you use the ngx_openresty bundle).



回答2:

You can actually do that using the -p option.

If you have your config file in the same directory as your app you can run:

/your/folder $ sudo nginx -c `pwd`/nginx.conf -p "`pwd`"

from your app folder.
Your nginx.conf file changes from:

http {
    include mime.types;
    root /your/folder;
    server {
        listen 8000;
    }
}

to

http {
    include /etc/nginx/mime.types;
    root .;
    server {
        listen 8000;
    }
}

just make sure that you check relative links that pointed to /etc/nginx/



标签: nginx config