Now, if you go to http://<server-ip>:8080 on your browser, you'll see the Jenkins setup screen. But ideally, we want to use a human-friendly hostname. So, just as we did with our API server, let's install NGINX to reverse proxy requests from jenkins.hobnob.social to http://localhost:8080:
jenkins@ci:$ echo "deb http://nginx.org/packages/ubuntu/ bionic nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list
jenkins@ci:$ echo "deb-src http://nginx.org/packages/ubuntu/ bionic nginx" | sudo tee -a /etc/apt/sources.list.d/nginx.list
jenkins@ci:$ sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys ABF5BD827BD9BF62
jenkins@ci:$ sudo apt update
jenkins@ci:$ sudo apt install nginx
jenkins@ci:$ sudo mkdir /etc/nginx/sites-available /etc/nginx/sites-enabled
Then, in the /etc/nginx/nginx.conf file, add a line after include /etc/nginx/conf.d/*.conf;:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
Next, create a configuration file for Jenkins at /etc/nginx/sites-available/jenkins.hobnob.social and paste in the following content:
server {
listen 80 default_server;
server_name jenkins.hobnob.social;
root /var/cache/jenkins/war;
access_log /var/log/nginx/jenkins/access.log;
error_log /var/log/nginx/jenkins/error.log;
ignore_invalid_headers off;
location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}
location /userContent {
root /var/lib/jenkins/;
if (!-f $request_filename){
rewrite (.*) /$1 last;
break;
}
sendfile on;
}
location @jenkins {
sendfile off;
proxy_pass http://localhost:8080;
proxy_redirect default;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_max_temp_file_size 0;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_request_buffering off;
proxy_set_header Connection "";
}
location / {
try_files $uri @jenkins;
}
}
This configuration is taken from wiki.jenkins.io/display/JENKINS/Running+Jenkins+behind+Nginx. The most pertinent parts are highlighted in preceding bold.
When a request comes in for jenkins.hobnob.social, it will match the location / block, which then proxies the request to the service running at the proxy_pass directive (http://localhost:8080). Likewise, when the internal service returns with a response, the proxy_redirect directive will rewrite the Location header of the response and replace http://localhost:8080 with http://jenkins.hobnob.social.
Now that our server block is ready, add it to the /etc/nginx/sites-enabled/ directory using a symbolic link:
jenkins@ci:$ sudo ln -s /etc/nginx/sites-available/jenkins.hobnob.social /etc/nginx/sites-enabled/
Lastly, make sure our NGINX configuration does not contain any syntax errors, and start it:
jenkins@ci:$ sudo nginx -t
jenkins@ci:$ sudo systemctl start nginx.service