5

最初に環境変数 HOSTNAME をキャプチャし、次に MySQL クエリを使用して、vhosts のドキュメント ルートを取得して Nginx conf に戻す方法を見つけようとしています。現在 Apache で動的ドキュメント ルートに使用していますが、Nginx に移行中です。

例 nginx.conf (次のようになります):

server {
    listen   80;
     # grab Environment variable HOSTNAME
     $hostname= ENV(HOSTNAME);
     # execute mysql query
    $doc_root = mysql(select docroot from table where host = '$hostname' );
     # set document root
    root           /var/www/$doc_root;

..... Lua とhttps://github.com/openresty/lua-resty-mysqlを使用して調査し ていましたが、HOSTNAME と mysql クエリを変数としてキャプチャし、結果が返されます。

4

2 に答える 2

1

First of all, using a database for doing basic routing does not sound like a very good idea - I would recommend having the results cached in memory and maybe refreshing them periodically from the database.

Second, the basic Nginx config file will get you only so far - in order to get more advanced functionality you will need to use a scripting language (like Lua) on top of it. One of the things this allows you is reading environment variables. I wrote about how to do it here:

https://docs.apitools.com/blog/2014/07/02/using-environment-variables-in-nginx-conf.html

The usual way to get Lua working on Nginx is using Openresty, a version of Nginx which comes with several modules pre-installed, including the Lua one. You can add lua-resty-mysql to the mix, and then do everything you want directly from Lua. I have never used lua-resty-mysql so I can't code that for you. If you are set on using Mysql, you will have to study its docs. Give a look at Redis while you are at it, it might be a better fit than Mysql.

于 2014-09-21T11:33:12.620 に答える