zk_html/work/ctm-385.html

135 lines
15 KiB
HTML

<!doctype html>
<html>
<head>
<title>Zk | ctm-385</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Gentium+Plus&family=Lato&family=Ubuntu+Monodisplay=swap" />
<link rel="stylesheet" type="text/css" href="/style.css?2024-05-04" />
<meta name="viewport" content="width=device-width" />
<meta charset="utf-8" />
</head>
<body>
<main>
<header>
<h1>Zk | ctm-385</h1>
</header>
<article class="content">
<h3 id="introduction">Introduction</h3>
<p>By default, Jenkins comes with its own built in web server, which listens on port 8080. This is convenient if you run a private Jenkins instance, or if you just need to get something up quickly and don&rsquo;t care about security. Once you have real production data going to your host, though, it&rsquo;s a good idea to use a more secure web server such as Nginx handling the traffic.</p>
<p>This post will detail how to wrap your site with SSL using the Nginx web server as a reverse proxy for your Jenkins instance. <strong>This tutorial assumes some familiarity with Linux commands, a working Jenkins installation, and a Ubuntu 14.04 installation.</strong> </p>
<p>You can install Jenkins later in this tutorial, if you don&rsquo;t have it installed yet.</p>
<h2 id="prerequisites">Prerequisites</h2>
<p>This guide assumes that you are using Ubuntu 22.04. Before you begin, you should have a non-<strong>root</strong> user account with <code>sudo</code> privileges set up on your system. You can learn how to do this by following the <a href="https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-22-04">Ubuntu 22.04 initial server setup tutorial</a>. You will also need the Nginx server installed and hosting your domain. You can learn how to do this with the <a href="https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-22-04">How To Install Nginx on Ubuntu 22.04 tutorial</a>.</p>
<p>Additionally, having your Jenkins instance secured by SSL is very important. If is visible on the internet, you can secure it with Let&rsquo;s Encrypt. You can learn how to do this with the <a href="https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-22-04">How to Secure Nginx with Let&rsquo;s Encrypt on Ubuntu 22.04 tutorial</a>.
As stated previously, this tutorial assumes that Jenkins is already installed. <a href="https://www.digitalocean.com/community/tutorials/how-to-install-and-use-jenkins-on-ubuntu-12-04">This tutorial</a> will show you how to install Jenkins if necessary. You will probably need to switch to the root user for that article.</p>
<h2 id="step-1-configure-nginx">Step 1 — Configure Nginx</h2>
<p>Nginx has become a favorite web server for its speed and flexibility in recent years, which makes it an idea choice for our application.</p>
<h3 id="edit-the-configuration">Edit the Configuration</h3>
<p>Next you will need to edit the default Nginx configuration file. The following example uses <code>nano</code>.</p>
<div class="codehilite"><pre><span></span><code>sudo nano etc/nginx/sites-enabled/default
</code></pre></div>
<p>Here is what the final configuration might look like; the sections are broken down and briefly explained below. You can update or replace the existing config file, although you may want to make a backup copy first.</p>
<div class="codehilite"><pre><span></span><code><span class="k">server</span><span class="w"> </span><span class="p">{</span><span class="kn">23</span><span class="w"></span>
<span class="w"> </span><span class="s">listen</span><span class="w"> </span><span class="mi">80</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">return</span><span class="w"> </span><span class="mi">301</span><span class="w"> </span><span class="s">https://</span><span class="nv">$host$request_uri</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">server</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="kn">listen</span><span class="w"> </span><span class="mi">443</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">server_name</span><span class="w"> </span><span class="s">&lt;^&gt;jenkins.domain.com&lt;^&gt;</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">access_log</span><span class="w"> </span><span class="s">/var/log/nginx/jenkins.access.log</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">location</span><span class="w"> </span><span class="s">/</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">Host</span><span class="w"> </span><span class="nv">$host</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">X-Real-IP</span><span class="w"> </span><span class="nv">$remote_addr</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">X-Forwarded-For</span><span class="w"> </span><span class="nv">$proxy_add_x_forwarded_for</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">X-Forwarded-Proto</span><span class="w"> </span><span class="nv">$scheme</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_pass</span><span class="w"> </span><span class="s">http://localhost:8080</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_read_timeout</span><span class="w"> </span><span class="mi">90</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_redirect</span><span class="w"> </span><span class="s">http://localhost:8080</span><span class="w"> </span><span class="s">https://&lt;^&gt;jenkins.domain.com&lt;^&gt;</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="w"> </span><span class="kn">...</span><span class="w"></span>
<span class="err">}</span><span class="w"></span>
</code></pre></div>
<p>You will need to update the &lt;^&gt;server_name&lt;^&gt; and <code>proxy_redirect</code> lines with your own domain name. There is some additional Nginx magic going on as well that tells requests to be read by Nginx and rewritten on the response side to ensure the reverse proxy is working.</p>
<p>Save and close the file. If you used <code>nano</code>, you can do so by pressing <code>Ctrl + X</code>, <code>Y</code>, and then <code>Enter</code>.</p>
<p>The first section tells the Nginx server to listen to any requests that come in on port 80 (default HTTP) and redirect them to HTTPS.</p>
<div class="codehilite"><pre><span></span><code><span class="k">...</span><span class="w"></span>
<span class="s">server</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="kn">listen</span><span class="w"> </span><span class="mi">80</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">return</span><span class="w"> </span><span class="mi">301</span><span class="w"> </span><span class="s">https://</span><span class="nv">$host$request_uri</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">...</span><span class="w"></span>
</code></pre></div>
<p>After that, the proxying happens. It basically takes any incoming requests and proxies them to the Jenkins instance that is bound/listening to port 8080 on the local network interface. </p>
<div class="codehilite"><pre><span></span><code><span class="k">...</span><span class="w"></span>
<span class="s">location</span><span class="w"> </span><span class="s">/</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">Host</span><span class="w"> </span><span class="nv">$host</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">X-Real-IP</span><span class="w"> </span><span class="nv">$remote_addr</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">X-Forwarded-For</span><span class="w"> </span><span class="nv">$proxy_add_x_forwarded_for</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_set_header</span><span class="w"> </span><span class="s">X-Forwarded-Proto</span><span class="w"> </span><span class="nv">$scheme</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_pass</span><span class="w"> </span><span class="s">http://localhost:8080</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_read_timeout</span><span class="w"> </span><span class="mi">90</span><span class="p">;</span><span class="w"></span>
<span class="w"> </span><span class="kn">proxy_redirect</span><span class="w"> </span><span class="s">http://localhost:8080</span><span class="w"> </span><span class="s">https://&lt;^&gt;jenkins.domain.com&lt;^&gt;</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="k">...</span><span class="w"></span>
</code></pre></div>
<p>&lt;$&gt;[note]
<strong>Note:</strong> If you&rsquo;d like to learn more about proxying in Nginx, <a href="https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-front-end-proxy-for-apache">this tutorial</a> has some good information about the Nginx proxy settings.
&lt;$&gt;</p>
<p>A few quick things to point out here. If you don&rsquo;t have a domain name that resolves to your Jenkins server, then the &lt;^&gt;proxy_redirect&lt;^&gt; statement above won&rsquo;t function correctly without modification, so keep that in mind. Also, if you misconfigure the &lt;^&gt;proxy_pass&lt;^&gt; (by adding a trailing slash for example), you will get something similar to the following in your Jenkins Configuration page.</p>
<p><img alt="Jenkins error: Reverse proxy set up is broken" src="https://assets.digitalocean.com/articles/nginx_jenkins/1.jpg" /></p>
<p>So, if you see this error, double-check your &lt;^&gt;proxy_pass&lt;^&gt; and &lt;^&gt;proxy_redirect&lt;^&gt; settings in the Nginx configuration!</p>
<h2 id="step-2-configure-jenkins">Step 2 — Configure Jenkins</h2>
<p>For Jenkins to work with Nginx, we need to update the Jenkins config to listen only on the localhost address instead of all (0.0.0.0), to ensure traffic gets handled properly. This is an important security step because if Jenkins is still listening on all addresses, then it will still potentially be accessible via its original port (8080). We will modify the &lt;^&gt;/etc/default/jenkins&lt;^&gt; configuration file to make these adjustments.</p>
<div class="codehilite"><pre><span></span><code>sudo nano /etc/default/jenkins
</code></pre></div>
<p>Locate the <code>JENKINS\_ARGS</code> line and update it to look like the following:</p>
<div class="codehilite"><pre><span></span><code><span class="n">JENKINS_ARGS</span><span class="o">=</span><span class="s2">&quot;--webroot=/var/cache/jenkins/war --httpListenAddress=127.0.0.1 --httpPort=$HTTP_PORT -ajp13Port=$AJP_PORT&quot;</span><span class="w"></span>
</code></pre></div>
<p>Notice that the <strong>&ndash;httpListenAddress=127.0.0.1</strong> setting must be either added or modified.</p>
<p>Then go ahead and restart Jenkins and Nginx.</p>
<div class="codehilite"><pre><span></span><code>sudo service jenkins restart
sudo service nginx restart
</code></pre></div>
<p>You should now be able to visit your domain using HTTPS, and the Jenkins site will be served securely.</p>
<h2 id="optional-update-oauth-urls">Optional — Update OAuth URLs</h2>
<p>If you are using the GitHub or another OAuth plugin for authentication, it will probably be broken at this point. For example, when attempting to visit the URL, you will get a &ldquo;Failed to open page&rdquo; with a URL similar to <code>http://jenkins.domain.com:8080/securityRealm/finishLogin?code=random-string</code>.</p>
<p>To fix this you will need to update a few settings within Jenkins, including your OAuth plugin settings. First update the Jenkins URL in the Jenkins GUI; it can be found in the <strong>Jenkins -&gt; Manage Jenkins -&gt; Configure System -&gt; Jenkins Location</strong> menu.</p>
<p>Update the Jenkins URL to use HTTPS - <code>https://&lt;^&gt;jenkins.domain.com/&lt;^&gt;</code></p>
<p><img alt="Jenkins URL" src="https://assets.digitalocean.com/articles/nginx_jenkins/2.jpg" /></p>
<p>Next, update your OAuth settings with the external provider. This example is for GitHub. On GitHub, this can be found under <strong>Settings -&gt; Applications -&gt; Developer applications</strong>, on the GitHub site.</p>
<p>There should be an entry for Jenkins. Update the <strong>Homepage URL</strong> and <strong>Authorization callback URL</strong> to reflect the HTTPS settings. It might look similar to the following:</p>
<p><img alt="Jenkins settings on GitHub; https:// has been used with both URLs" src="https://assets.digitalocean.com/articles/nginx_jenkins/3.jpg" /></p>
<h3 id="conclusion">Conclusion</h3>
<p>The only thing left to do is verify that everything worked correctly. As mentioned above, you should now be able to browse to your newly configured URL - &lt;^&gt;jenkins.domain.com&lt;^&gt; - over either HTTP or HTTPS. You should be redirected to the secure site, and should see some site information, including your newly updated SSL settings. As noted previously, if you are not using hostnames via DNS, then your redirection may not work as desired. In that case, you will need to modify the &lt;^&gt;proxy_pass&lt;^&gt; section in the Nginx config file.</p>
</article>
<footer>
<p>Page generated on 2024-05-04</p>
</footer>
</main>
<script type="text/javascript">
document.querySelectorAll('.tag').forEach(tag => {
let text = tag.innerText;
tag.innerText = '';
tag.innerHTML = `<a href="/tags.html#${text}">${text}</a>`;
});
</script>
</body>
</html>