DEV Community

Aruzhan Abduvali
Aruzhan Abduvali

Posted on

Setting up Nginx as a Reverse Proxy for Jenkins with SSL certificates on multiple subdomains using Certbot on Ubuntu 20.04

Step 1: Install the Nginx Web Server
Ubuntu
sudo apt update
sudo apt install nginx vim

Note: Ensure that Apache2 is not running simultaneously with Nginx. If Apache2 is running, stop and disable it.

Step 2: Configure Nginx for the Jenkins Server subdirectory
After installing Nginx web server, create a VirtualHost configuration file:
sudo vim /etc/nginx/conf.d/jenkins.conf

Paste and modify the following configurations in the created file:

upstream jenkins {
  server 127.0.0.1:8080 fail_timeout=0;
}

server {
  listen 80;
  server_name jenkins.example.com;

  location / {
    proxy_set_header        Host $host:$server_port;
    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_pass              http://jenkins;
    proxy_http_version 1.1;
    proxy_request_buffering off;
    proxy_buffering off;
  }
}

server {
  listen 80;
  server_name www.jenkins.example.com;

  location / {
    proxy_set_header        Host $host:$server_port;
    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_pass              http://jenkins;
    proxy_http_version 1.1;
    proxy_request_buffering off;
    proxy_buffering off;
  }
}

Enter fullscreen mode Exit fullscreen mode

Replace:
jenkins.example.com and www.jenkins.example.com with your Jenkins server domain name as configured in the DNS server.

Step 3: Validate Nginx configuration
sudo nginx -t

Output should confirm that the syntax is OK and the test is successful.

Step 4: Start Nginx service
sudo systemctl enable --now nginx
sudo systemctl restart nginx

Nginx is now configured to proxy requests to the Jenkins server. Access Jenkins using your specified domain name (e.g., jenkins.example.com or www.jenkins.example.com).

Certbot instructions - Nginx on Ubuntu 20
Step 1: SSH into the Server

SSH into the server running your HTTP website as a user with sudo privileges.
ssh your_username@your_server_ip

Step 2: Install Snapd
Install Snapd on the server.
sudo apt install snapd

Step 3: Remove Existing Certbot Packages
Remove any Certbot packages installed using the OS package manager.
sudo apt remove certbot

Step 4: Install Certbot Snap
Install Certbot using Snap.
sudo snap install --classic certbot

Step 5: Obtain and Install SSL Certificate
Run Certbot to get a certificate and automatically edit the Nginx configuration to enable HTTPS.
sudo certbot --nginx

Follow the prompts to enter your email address and answer other questions.

Choose which domain you want to activate HTTPS or press Enter to choose everything.
You can check your SSL certificate's expiration date and other information on the SSL Server Test (Powered by Qualys SSL Labs) website.

Top comments (0)