Secure Jenkins with SSL Using Nginx Reverse Proxy
In this article
This guide walks you through setting up an Nginx reverse proxy to terminate SSL/TLS for your Jenkins instance. By the end, users will access Jenkins via HTTPS (e.g., https://jenkins.example.com) while Nginx handles encryption and forwards traffic to Jenkins over HTTP locally. This improves security without modifying Jenkins' internal configuration.
What You'll Need
- A server running Linux (Ubuntu/Debian or CentOS/RHEL) with Jenkins already installed and listening on localhost (default port 8080).
- Root or sudo access to the server.
- A domain name pointing to your server's public IP (e.g., jenkins.example.com).
- An SSL certificate. You can use a free Let's Encrypt certificate (via certbot) or a purchased certificate.
- Basic familiarity with the command line and text editors (vi, nano).
Step-by-Step
- Install Nginx
On Ubuntu/Debian:sudo apt update && sudo apt install nginx -y
On CentOS/RHEL:sudo yum install nginx -y - Start and enable Nginx
sudo systemctl start nginxsudo systemctl enable nginx - Obtain an SSL certificate
If using Let's Encrypt, install certbot:sudo apt install certbot python3-certbot-nginx -y(orsudo yum install certbot python3-certbot-nginx). Then runsudo certbot --nginx -d jenkins.example.comand follow prompts. For a purchased certificate, place the certificate and key files in a secure location like/etc/ssl/. - Create an Nginx configuration file
Create a new file:sudo nano /etc/nginx/sites-available/jenkins(Ubuntu/Debian) orsudo nano /etc/nginx/conf.d/jenkins.conf(CentOS/RHEL). Add the following configuration (adjust paths if using your own certificate):server { listen 80; server_name jenkins.example.com; return 301 https://$host$request_uri; } server { listen 443 ssl http2; server_name jenkins.example.com; ssl_certificate /etc/letsencrypt/live/jenkins.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/jenkins.example.com/privkey.pem; ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; location / { proxy_pass http://127.0.0.1:8080; 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; # WebSocket support for Jenkins (if needed) proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; } } - Enable the configuration
On Ubuntu/Debian:sudo ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/then remove the default site if it conflicts:sudo rm /etc/nginx/sites-enabled/default. On CentOS/RHEL, the file is already active if placed in conf.d. - Test the Nginx configuration
sudo nginx -t– ensure no syntax errors. - Reload Nginx
sudo systemctl reload nginx - Configure Jenkins to trust the proxy
Edit/etc/default/jenkins(Debian/Ubuntu) or the systemd service file (CentOS) to add JVM arguments:JAVA_ARGS="-Djava.awt.headless=true -Djenkins.model.Jenkins.crumbIssuerProxyCompatible=true"(the crumb setting helps with CSRF when behind a proxy). Also, set the Jenkins URL to the HTTPS address: In Jenkins UI, go to Manage Jenkins > Configure System and set Jenkins URL tohttps://jenkins.example.com. - Restart Jenkins
sudo systemctl restart jenkins - Verify HTTPS access
Open a browser and navigate tohttps://jenkins.example.com. You should see the Jenkins login page with a valid SSL padlock.
Common Mistakes
- Forgetting to set X-Forwarded-Proto – Jenkins may generate HTTP links or reject requests if it doesn't know the original protocol. Always include the header.
- Not enabling WebSocket headers – Jenkins uses WebSockets for some features (e.g., console output). Without the Upgrade/Connection headers, those features may fail.
- Using the wrong certificate path – Double-check that the paths in the Nginx config match the actual certificate location.
- Blocking port 443 in firewall – Ensure your firewall allows HTTPS traffic (e.g.,
sudo ufw allow 443/tcp). - Not updating Jenkins URL – If Jenkins still thinks it's on HTTP, it may generate incorrect links in emails and notifications.
Tips
- Use Let's Encrypt with auto-renewal: certbot adds a cron job or systemd timer automatically. Test renewal with
sudo certbot renew --dry-run. - Add a firewall rule to restrict port 8080 to localhost only, so Jenkins is not exposed directly to the internet.
- Consider enabling HTTP/2 for performance (already included in the config with
http2). - If you have a large team, set up a dedicated DNS record for Jenkins and keep the URL consistent.
- Regularly update Nginx and Jenkins to get security patches.
Troubleshooting
- 502 Bad Gateway – Nginx cannot reach Jenkins. Check that Jenkins is running:
sudo systemctl status jenkins. Also verify the proxy_pass address matches Jenkins' listening address (usually 127.0.0.1:8080). - SSL certificate not trusted – Ensure your certificate is valid and not self-signed (unless you've added it to client trust stores). For Let's Encrypt, check that the domain resolves to the server.
- Redirect loop – If Jenkins is configured to force HTTPS, it may conflict with Nginx. Remove any Jenkins-level HTTPS redirect or ensure the X-Forwarded-Proto header is correct.
- WebSocket errors – If console output fails to update in real-time, check that the Upgrade headers are present in the Nginx config and that Nginx version supports WebSocket proxying (1.3+).
- Permission denied on certificate files – Nginx runs as user
nginxorwww-data. Ensure the certificate and key files are readable by that user:sudo chmod 644 /etc/letsencrypt/live/.../fullchain.pemandsudo chmod 600 .../privkey.pem. - Jenkins shows HTTP links – After setting the Jenkins URL, restart Jenkins and clear your browser cache.
Frequently Asked Questions
Do I need to change Jenkins' port?
No. Jenkins can continue listening on port 8080. Nginx will listen on port 443 and forward to 8080 internally.
Can I use a self-signed certificate?
Yes, but browsers will show warnings. For production, use a trusted certificate from Let's Encrypt or a commercial CA.
Will this affect Jenkins plugins or CLI?
No, as long as you set the correct Jenkins URL and proxy headers. CLI tool should work over HTTPS.
How do I renew my Let's Encrypt certificate?
Certbot handles renewal automatically. You can test with sudo certbot renew --dry-run.
What if I already have a reverse proxy like Apache?
You can adapt the same steps to Apache, but this guide focuses on Nginx. The key headers (X-Forwarded-*) are similar.
Should I disable HTTP access to Jenkins?
Yes, ideally. The Nginx config above redirects all HTTP to HTTPS, and you can block external access to port 8080 with a firewall.