<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="howdid.xsl"?> 

<articles>
  <article date="17 Jul 2023">
    <pagetitle>Nginx N-Gage</pagetitle>
    <articleheader>Setting up server-side XSLT on NGINX and Ubuntu</articleheader>
    <articleabstract>Moving to server-side XSLT is easier and harder than I expected</articleabstract>
    <articlebody>

<p>This site is built with <span class="danger">XML</span> and <span class="danger">XSLT</span> For the last couple of years, I used client-side processing because it literally took no additional configuration on my end other than telling nginx <a href="https://wyrm.org/howdid/nginx-xml.xml">what an <span class="danger">XML</span> file is</a>. I decided that it was time to change this.</p>

<p>Note: I'm using Ubuntu for this for reasons that don't warrant going into here, but the same steps should also work for Debian</p>

<h2>Install the server module</h2>

<p>Install the <code>libnginx-mod-http-xslt-filter</code> module by typing <code>apt install libnginx-mod-http-xslt-filter</code> as root or via <code>su</code>. It might already be installed (it was on mine).</p>

<p>Near the top of your nginx.conf file, add the following line to activate the module</p>

<pre>/etc/nginx.conf
<code>include /etc/nginx/modules-enabled/50-mod-http-xslt-filter.conf;</code></pre>

<p>Restart nginx and, if there are no errors, it's time to configure your <code>/etc/nginx/sites-available/sitename.example</code>.

<h2>Configuring your site</h2>

<p>If your site is like mine, then this part gets tedious.</p>

<p>In <code>/etc/nginx/sites-available/sitename.example</code> (replace <code>sitename.example</code> with whatever your site name is), find the <code>location</code> directive for the root of your site. Add the following:</p>

<code>
xslt_stylesheet /var/somefolder/path-to.xsl;
</code>

<p>Where <code>/var/somefolder</code> is the real full path to the <code>XSLT</code> file on your server. Restart nginx and marvel at how nothing looks right because nginx is now applying one <span class="danger">XSL</span> to everything. That's probably not what we want (it's definitly not what <em>I</em> want.</p> To fix this, we need to add lots more <code>location</code> directives for every different <span class="danger">XSLT</span> file that we want to use. And since I sprinkled them everywhere, I need <em>lots</em> of <code>location</code> directives. They all look something like this.</p>

<pre>sites-available/example.com<code>

	location /cookiefaq.xml {
                xslt_stylesheet /path/to/webroot/cookiefaq.xsl;
        }
        location /books {
                xslt_stylesheet /path/to/webroot/books/books.xsl;
        }
        location /inventory/amiibo.xml {
                xslt_stylesheet /path/to/webroot/inventory/amiibo.xsl;
        }
</code></pre>

<p>Obviously <code>/path/to/webroot</code> should reflect your actual system path the <span class="danger">XSLT</span> files live in.</p>

<p>Restart nginx again. If you have no errors, then, congratulations! Everything looks identical! Which may seem like a huge waste of time and energy, but now people who are using web browsers with scripting turned off (or if scripting isn't available) can see the site (more-or-less) as intended.</p>


</articlebody>
    <footnotes>
      <footnote><span class="danger">XML and XSLT</span></footnote>
      <footnote>Server-side</footnote>
      <footnote>howdid</footnote>
    </footnotes>
  </article>
</articles>
