Skip to content

Deploy ngrest under Nginx

Dmitry edited this page Dec 20, 2016 · 6 revisions

Deploying ngrest under Nginx Web Server is intended to use ngrest in production. Simple http server shipped with ngrest is suitable for development purposes and for very small deployment only.

To deploy ngrest under Nginx you need to complete few steps.

Install nginx

Nginx 1.9.11 is required. If you already have Nginx version >= 1.9.11 installed, skip this step.

sudo add-apt-repository ppa:nginx/stable
# to enable source package
sudo sed -i 's/# //' /etc/apt/sources.list.d/nginx*-stable*.list
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install nginx

Obtain Nginx source code

Due to no header files installed upon Nginx installation, you must get Nginx source code and configure it to create auto-generated files (no need to build it). Also must configure with the same flags as Nginx from PPA, else module signature will not match and Nginx will refuse to load the module.

mkdir ~/nginx-build && cd ~/nginx-build
apt-get source nginx
sudo apt-get build-dep nginx
cd nginx-*
nginx -V 2>&1 | tr ' '  '\n' | grep -- --with | egrep -v -- '--with-(cc|ld)' | xargs ./configure

Make sure ngrest module is compiled

touch ~/.ngrest/ngrest/CMakeLists.txt
NGINX_PATH=$PWD make -C ~/.ngrest/ngrest-build
ls ~/.ngrest/ngrest-build/deploy/share/ngrest/modules/nginx/mod_ngrest.so

You should see full path to ngrest module, not the "No such file or directory" error:

/home/user/.ngrest/ngrest-build/deploy/share/ngrest/modules/mod_ngrest.so

Configure Nginx

Edit /etc/nginx/nginx.conf and add a line before any sections, replacing path after load_module to the full path to ngrest module above:

load_module /home/YOUR_USERNAME/.ngrest/ngrest-build/deploy/share/ngrest/modules/nginx/mod_ngrest.so;

Edit /etc/nginx/sites-enabled/default adding these lines inside server section:

location ~ /(ngrest|echo) {
    ngrest /home/user/.ngrest/ngrest-build/deploy/share/ngrest/services;
}

Adjust path after ngrest handler name to actual path to services.

Also you need to edit location directive setting regex to all your bound ngrest service resource paths. Please read explanation here.

Make sure config and module are ok

sudo nginx -t

You should see something like:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx Web Server

sudo service nginx restart

Test if ngrest module loaded and work

To test if module loaded and work you must access one of your services. If your ngrest.conf as in example you should be able to access ngrest services tester with this URL: http://localhost/ngrest/services.