Tuesday, 28 July 2015

Find server Machine IP with NGINX LUA

Step 1 install

wget http://openresty.org/download/ngx_openresty-1.7.10.2.tar.gz
tar xvzf ngx_openresty-1.7.10.2.tar.gz
cd ngx_openresty-1.7.10.2/
sudo apt-get update
sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl make build-essential
./configure --with-luajit
make
sudo make install
mkdir ~/work
cd ~/work/
mkdir conf/ logs/
vim conf/nginx.conf
PATH=/usr/local/openresty/nginx/sbin:$PATH
export PATH
nginx -p `pwd`/ -c conf/nginx.conf
curl localhost:8080

Step 2
Note - use step 2 only if you want to use content by lua and skip step 3,4
## content by lua
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua '
                ngx.say("<p>hello, world</p>")
            ';
        }
    }
}

Step 3
Note - use this if you want to use content by lua file in this skip step 2
## content by lua file
worker_processes  1;
error_log logs/error.log;
events {
    worker_connections 1024;
}
http {
    server {
        listen 8080;
        location / {
            default_type text/html;
            content_by_lua_file '/home/mr/cache.lua';
        }
    }
}

Step 4
Note - lua file that you want to pass from nginx
## content by lua file
vim /home/mr/cache.lua                
ngx.print(ngx.var.remote_addr)

Step 5
Note - if any isssue to start the nginx
Trobleshoot -
ps ax| grep nginx
sudo kill -9 PID

Step 6
Note - how to start nginx and open on browser IP:8080
Start ->
nginx -p `pwd`/ -c conf/nginx.conf
curl localhost:8080