Installing nginx with websockets support

As of today NginX doesn’t support websockets. There are alternatives:

Environment prerequisites

Required packages as or today are:

  • OpenSSL – toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) protocols as well as a full-strength general purpose cryptography library
  • zlib (unobtrusive compression libraries)
  • PCRE Library ( Perl 5 Compatible Regular Expression Library
So lets start from getting root privileges

Now we should check if required libraries are installed.

Ubuntu:

CentOS:

If there is library missing (pcre in my case) apt-get install <package>  should be used to install it, ex.:

Ubuntu:

Centos:

Additional missing libraries can be installed by executing

Installation and system setup

All code shown in this example should be executed by root and everything will be downloaded into root directory. Lets navigate there and then we will export environment path first so we can easily reuse the script or newer version of nginx just by changing NGINX_VERSION value.

Build configuration

Below I listed Nginx configuration settings that will be apply by default during build process.

Lets tweak it a bit so files can get installed where they should be adding –add-module=/path/to/nginx_tcp_proxy_module option add the end

IMPORTANT: Before proceeding make sure above command didn’t return any errors

Installation and testing

Installation is very simple process. Just make sure you are in nginx directory and then run:

If you followed all the steps installation should end successfully. Confirm nginx version typing:

System setup

“www-data” userIn our configuration we pointed nginx to use nginx user and group. Lets create them now

Ubuntu:

CentOS:

If you want to confirm user has been created look at /etc/passwd  file with grep option

All configured directories and files have to be created. We pointed nginx to /var/tmp/nginx  folder as location for temporary files so lets create it along with sites-available, sites-enabled and conf.d.

Important*: As we didn’t set tcp access log path during configuration (it is not possible) to avoid error while starting nginx usually we would have to create  /usr/log/tcp_access.log file as shown below. DON’T !!!

This is default location required by nginx_tcp module. In a moment we will set it up manually in our nginx.conf  file using access_log option.

“nginx.conf” config file

Edit nginx.conf file using your favourite editor:

And replace its content with bellow settings:

Test with first websocket application

We will have to create applications for both the client and the server.

Using node.js lets create server application that will (1) serve index.html as a response to HTTP request on localhost port 8080 (as  configured above) and (2) provide websocket support with socket.io. Create app.js file

and place in the same paste below code:

Now lets create public folder and index.html file which will demo client application.

Paste below code to html file:

Notice that in line 6 we connect to port 8080 which is port defined in /etc/nginx/nginx.conf configuration file.

Now run server in regular way by executing below line in your console

You should see output

And if you navigate to http://localhost:8080/ you should see “Websockets Proxy Test” in browser window

Open new tab and navigate to http://localhost:9000/ and you should see websocket monitor.

Other resources

Freelance developer, IT enthusiast, blogger with entrepreneurial spirit and passion for making games

Tagged with:
, , , , , , , ,
Posted in
Technical
Categories