nginx variable for path of config file

2019-07-08 02:52发布

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

标签: nginx config
2条回答
放我归山
2楼-- · 2019-07-08 03:23

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/

查看更多
看我几分像从前
3楼-- · 2019-07-08 03:44

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).

查看更多
登录 后发表回答