Building Nginx with the Sticky Module in Ubuntu

To work correctly, the nginx balancer must be built together with the sticky module, which ensures the binding of a certain user session to a specific BI server. Consider build order in Ubuntu 18.04.

Nginx Build

  1. Download and extract the sticky module files at: https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng/src/master/.

  2. Download nginx:

wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xvf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
  1. Set dependencies required for nginx to work:

sudo apt-get install libxslt-dev
sudo apt install libgd-dev
sudo apt install openssl libssl-dev
sudo apt install libpcre3 libpcre3-dev
sudo apt install libgeoip-dev
  1. Generate and execute a command to configure a build. In the --add-module key specify the path to the sticky installation files obtained in the first step:

./configure --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --add-module=<path to sticky files>

  1. Run the nginx build and installation:

sudo make
sudo make install

Nginx Configuration

The nginx uses settings from the nginx.conf file located in the /etc/nginx/ folder. The example of the file structure required to work with BI servers is given in the Creating Cluster of BI Servers>Configure the Balancer to Work with BI Servers subsection. For details about directives used in the configuration file, see the developer documentation at https://nginx.org/en/docs/.

Nginx Startup

To run nginx, execute the command:

sudo /usr/share/nginx/sbin/nginx -h

If it is required to run nginx as a background service, execute the following operations:

  1. Create the file /lib/systemd/system/nginx.service.

  2. Add the service startup options to it:

[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/share/nginx/sbin/nginx -t
ExecStart=/usr/share/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
  1. Create the /var/lib/nginx directory:

sudo mkdir -p /var/lib/nginx

  1. Execute the commands to start the service:

systemctl unmask nginx.service
systemctl enable nginx
systemctl restart nginx

See also:

Creating BI Servers Cluster