{"componentChunkName":"component---src-templates-post-template-js","path":"/nginx-per-site-conf","webpackCompilationHash":"9f9b08718447f3d4e396","result":{"data":{"markdownRemark":{"id":"b7f49102-068d-5ad9-8aae-153d34714813","html":"<p>Somehow I wasn’t able to find this old mechanic in the documentations and within the first page of google results while looking for it for a memory refresh, so here we go with a rustic explanation, mostly for myself</p>\n<p>There is a modular way to write nginx.conf blocks (http location config blocks for example), in a per-site fashion (similar approach as using  ‘a2ensite’ and ‘a2dissite’ in apache)</p>\n<p>This is helpful to keep an easy to maintain conf file when you have multiple sites/domains/subdomains configurations in the same server.</p>\n<h2 id=\"quick-how-to\"><a href=\"#quick-how-to\" aria-label=\"quick how to permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Quick how to</h2>\n<p>Change the site config at <code class=\"language-text\">/sites-available/</code> and then make sure that the symlink to that file exists at <code class=\"language-text\">/sites-enabled/</code>, if it doesn’t create it with <code class=\"language-text\">ln -s</code>, example:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">ln -s /etc/nginx/sites-available/mysite-production /etc/nginx/sites-enabled/</code></pre></div>\n<p>Then restart nginx</p>\n<h2 id=\"how-it-works\"><a href=\"#how-it-works\" aria-label=\"how it works permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How it works</h2>\n<p>Nginx consists of modules which are controlled by directives specified in the configuration file that you can usually find at <code class=\"language-text\">/etc/nginx/nginx.conf\\</code></p>\n<p>A way to load the per-site configuration is having this at the bottom in that file:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">location @mysite_production {\n  include /etc/nginx/conf.d/*.conf;\n  include /etc/nginx/sites-enabled/*;\n}</code></pre></div>\n<p>so it will include the configuration from the files found in that folder there, which are actually symlinks to the ones at /sites-available folder, more about that next.</p>\n<h2 id=\"how-to-update-them\"><a href=\"#how-to-update-them\" aria-label=\"how to update them permalink\" class=\"anchor\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to update them</h2>\n<p>The way that you can opt-in which one to include in the live configuration is adding or removing symlinks from <code class=\"language-text\">/sites-available/</code> to <code class=\"language-text\">/sites-enabled/</code></p>\n<p>If you update the config file located at <code class=\"language-text\">/sites-available/mysite-production</code> it will be included in the main nginx configuration only if the symlink exists in the <code class=\"language-text\">/sites-enabled/</code> folder. Changes will be reflected at sites-enabled since the symlink just links to the original file from sites-available</p>\n<p>That way you can serve multiple configurations, and enable/disable them to quickly test stuff or to scope per-site or even per-environment configurations.</p>\n<p>To create a symlink you can write something like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">ln -s /etc/nginx/sites-available/mysite-production /etc/nginx/sites-enabled/</code></pre></div>\n<p>And to remove it just go with the old rm or unlink command, pointing to the symlink path.</p>\n<p>After that make sure to restart nginx to pick up the config changes <code class=\"language-text\">sudo service nginx restart</code></p>","fields":{"slug":"nginx-per-site-conf","tagSlugs":["/tag/nginx/","/tag/per-site/","/tag/configuration/","/tag/sites-available/","/tag/sites-enabled/"]},"frontmatter":{"date":"2020-05-04T14:48:44.854Z","description":"A quick reminder on how sites-available and sites-enabled configurations work on Nginx and how to properly update them.","tags":["nginx","per-site","configuration","sites-available","sites-enabled"],"title":"Nginx per-site configuration","socialImage":null}}},"pageContext":{"isCreatedByStatefulCreatePages":false,"slug":"nginx-per-site-conf"}}}