NGINX is part of most package management repositories, including yum and apt. Installing NGINX on apt-based distributions such as Debian and Ubuntu would involve the following commands:
sudo apt-get update
sudo apt-get install nginx
On macOS, we can use brew for a fast one-line command:
brew install nginx
Of course, it is always possible to install from the source. Remember to install the dependencies first. These include PCRE, zlib, and OpenSSL. You can find the source code at the following website: http://nginx.org/en/download.html.
Before you start NGINX, you need to provide a configuration file for it to connect to the Unicorn web component. The interface between the two servers is a local Unix domain socket.
Please create a file called nginx.config and add the following:
events{}
The preceding section is mandatory and you can specify how NGINX will handle connections. For this example, we just accept the default; that is why the section is empty.
The next section is an HTTP block. You can define several and let settings be inherited, but here, we define that HTTP requests should be redirected to upstream gitlab-app, which is Unicorn. You can also see that the interface is the Unix socket:
http {
# Tell nginx there is a unicorn waiting
upstream gitlab-app {
server unix:/var/www/gitlab-app/tmp/sockets/unicorn.sock fail_timeout=0;
}
So, we have defined how NGINX connects to the backend, which is GitLab. On the frontend, we want to accept requests from HTTP clients. This is taken care of via a server block:
server {
listen 8080;
server_name localhost;
The next directive inside this block handles a path that doesn't exist on disk. It forwards the request to the app:
try_files $uri/index.html $uri @gitlab-app;
The following is the definition of gitlab-app, and it modifies request headers to proxy the request to the upstream Unicorn server via the Unix socket:
location @gitlab-app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://gitlab-app;
}
Don't forget to close the server and HTTP block:
}
}
Now that we have a configuration, you can run this NGINX in the following way:
nginx -c /path/to/nginx.config
The command should return no output and the NGINX server runs in the background. You can verify this yourself by checking the process list:
$ ps -ax|grep nginx |grep -v grep
33310 ?? 0:00.00 nginx: master process nginx -c /Users/joostevertse/nginx.config
33312 ?? 0:00.00 nginx: worker process
Now that you have a running NGINX server, if you point your browser to http://localhost:8080, you should receive a 502 error. This is because there is no Unicorn server listening on a Unix socket yet. We will demonstrate how to run Unicorn in the next section.