LoginSignup
2
2

More than 5 years have passed since last update.

nginScriptを試す(2):js_set,js_runディレクティブ

Last updated at Posted at 2015-10-13

関連記事:

今度はスクリプトの実行

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 余談はここから ↓-------------------
前回の記事では非常に長くなったがnginxのインストールとnginScriptのインストールはできた。
次はnginScriptを実際に使ってみよう。
とはいえ、Launching nginScript and Looking Aheadページにある以上のことは何もなさそうだ。

これはプログラミング言語というレベルまではいかないまでも、
ApacheのConfでゴリゴリ書くようなものをScript上でやってしまおうという位置づけに思う。

------------------- ↑ 余談はここまで ↑ -------------------

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 本題はここから ↓-------------------

ディレクティブ

js_setディレクティブ

変数を定義するディレクティブ
""でくくった内容の結果文字列を定義する変数に代入

/usr/local/nginx/conf/nginx.conf
http {
    ・・・
    js_set $msg "
       var m = 'Hello ';
       m += 'world!';
       m;
    ";

    server{
        ・・・
        location /hello {
            add_header Content-Type text/plain;
            return 200 $msg;
        }
    }
}

ブラウザからアクセス

nginx04.PNG

js_runディレクティブ

responseオブジェクトを制御するディレクティブ

/usr/local/nginx/conf/nginx.conf
http {
    ・・・
    server{
        ・・・
        location /run {
            js_run "
                var res;
                res = $r.response;

                res.contentType = 'text/plain';
                res.status = 200;
                res.sendHeader();

                res.send( 'Hello nginScript!' );
                res.finish();
            ";
        }
    }
}

ブラウザからアクセス

nginx05.PNG

------------------- ↑ 本題はここまで ↑-------------------

ヾ(・ω<)ノ" 三三三● ⅱⅲ コロコロ♪


------------------- ↓ 補足はここから ↓-------------------

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2