Changes for page How to setup an Nginx reverse proxy and also provide a global X.509 certificate for it
Last modified by Alexandru Pentilescu on 2023/06/25 18:53
From version 12.1
edited by Alexandru Pentilescu
on 2022/11/13 22:18
on 2022/11/13 22:18
Change comment:
There is no comment for this version
To version 13.1
edited by Alexandru Pentilescu
on 2023/06/25 18:53
on 2023/06/25 18:53
Change comment:
There is no comment for this version
Summary
-
Page properties (1 modified, 0 added, 0 removed)
Details
- Page properties
-
- Content
-
... ... @@ -2,11 +2,11 @@ 2 2 3 3 Using Nginx in such a way helps define subdomains for different services. 4 4 5 -For example, suppose you are in control of a domain name called " pentilescu.com". You wish to establish subdomains for different services, such as: having a Bitwarden HTTP server accessible at "passwords.pentilescu.com", a gitea HTTP server accessible at "git.pentilescu.com" and a personal webpage at "alexandru.pentilescu.com".5 +For example, suppose you are in control of a domain name called "transistor.one". You wish to establish subdomains for different services, such as: having a Bitwarden HTTP server accessible at "passwords.transistor.one", a gitea HTTP server accessible at "git.transistor.one" and a personal webpage at "alex.transistor.one". 6 6 7 -" pentilescu.com" is the master domain, an umbrella under which all the other services reside. Each of these services can run on a different machine, if you wish for it, or it can run on the same machine.7 +"transistor.one" is the master domain, an umbrella under which all the other services reside. Each of these services can run on a different machine, if you wish for it, or it can run on the same machine. 8 8 9 -All you have to do is configure an Nginx server on the machine that's being pointed to, via DNS, as " pentilescu.com". Then, Nginx can redirect all HTTP requests destined to various pre-configured subdomains to other machines or to different ports on the same machine, effectively orchestrating all requests according to well defined matching rules. It can, say, redirect all HTTP requests destined for "passwords.pentilescu.com" to localhost port 187, all HTTPS requests destined for "git.pentilescu.com" to localhost port 200 and all "alexandru.pentilescu" requests to local device 192.168.1.3 port 9030.9 +All you have to do is configure an Nginx server on the machine that's being pointed to, via DNS, as "transistor.one". Then, Nginx can redirect all HTTP requests destined to various pre-configured subdomains to other machines or to different ports on the same machine, effectively orchestrating all requests according to well defined matching rules. It can, say, redirect all HTTP requests destined for "passwords.transistor.one" to localhost port 187, all HTTPS requests destined for "git.transistor.one" to localhost port 200 and all "alex.transistor.one" requests to local device 192.168.1.3 port 9030. 10 10 11 11 This gives administrators flexibility to configure all services on various machines and ports and then, at a single gateway endpoint, configure Nginx to redirect all requests to each of them, accordingly. 12 12 ... ... @@ -89,7 +89,7 @@ 89 89 {{code language="nginx"}} 90 90 server { 91 91 92 - server_name passwords. pentilescu.com;92 + server_name passwords.transistor.one; 93 93 root /var/www/; 94 94 95 95 listen [::]:443 ssl http2; # managed by Certbot ... ... @@ -120,7 +120,7 @@ 120 120 I called this file "bitwarden.conf" both in the "sites-enabled" and "sites-available" directories, the former is a symbolic link towards the latter. It's imperative that the symbolic link's name end in the ".conf" extension, otherwise Nginx will ignore it, per our own configuration in "nginx.conf". 121 121 Now, let's break that configuration down into the important parts that you should remember: 122 122 123 -* The "server_name" directive tells Nginx the name of the subdomain whose requests need to be redirected to a different endpoint. In our case, we wanted all HTTP requests destined to "passwords. pentilescu.com" to be redirected to our localhost port 5178123 +* The "server_name" directive tells Nginx the name of the subdomain whose requests need to be redirected to a different endpoint. In our case, we wanted all HTTP requests destined to "passwords.transistor.one" to be redirected to our localhost port 5178 124 124 * The "listen 443 ssl http2" and "listen [::]:443 ssl http2" directives tell Nginx to open port 443 for both IPv4 and IPv6 incoming traffic. Port 443 is usually used by web browsers attempting to connect to our website using the TLS protocol, in order to secure the connection against network eavesdroppers. Keep in mind that, in order to effectively use TLS, we must have a generated X.509 certificate to use, signed by a certificate authority that's, hopefully, trusted by most web browser vendors 125 125 * The "location" directive is a bit interesting. It defines many other subdirectives for the destination endpoint of our service. The "proxy_pass" subdirective, specifically, tells Nginx where the destination of the redirected requests needs to be. In our case, all requests have to be redirected to our own machine, port 5178. The "add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload";" directive tells the connection to always be encrypted, at all times, otherwise it should fail in an error. This should include subdomains as well. And the "error_log" and "access_log" subdirectives tell Nginx where and under which names to store the error log and the access log for this specific service, respectively 126 126 ... ... @@ -128,9 +128,9 @@ 128 128 129 129 Finally, the "include /etc/nginx/snippets/ssl.conf;" is the most interesting directive, by far. This "snippets" directory, is meant to contain configuration snippets that are meant to be included in other configuration files, all the time. The "ssl.conf", specifically, contains configuration data for pointing towards X.509 certificate files, so that Nginx wil know where the public key file is contained, where the private key file is contained, and where the full chain certificate is contained. All these three files need to be configured, so that Nginx will know how to find them and deliver the public ones to any visitors to our site. If you don't wish to support TLS at all on your server, don't include this directive in your configuration file and change the 443 port references here to port 80, instead, so that only HTTP connections are allowed. 130 130 131 -Since the same certificate files will be reused by all of our different endpoints, such as "passwords. pentilescu.com", "git.pentilescu.com", "wiki.pentilescu.com" etc, it doesn't make sense to just copy-paste their configurations in every one of their Nginx ".conf" files, instead, we configure all of them in one snippet file, in our case, "ssl.conf" and then, in all of those other services' configuration files, we just add 1 line that references the "ssl.conf" file, instead. Functionally, they are the same.131 +Since the same certificate files will be reused by all of our different endpoints, such as "passwords.transistor.one", "git.transistor.one", "wiki.transistor.one" etc, it doesn't make sense to just copy-paste their configurations in every one of their Nginx ".conf" files, instead, we configure all of them in one snippet file, in our case, "ssl.conf" and then, in all of those other services' configuration files, we just add 1 line that references the "ssl.conf" file, instead. Functionally, they are the same. 132 132 133 -This is possible because of the existence of wildcard X.509 certificates, where you can basically say "This certificate is meant to protect traffic for all subdomains under pentilescu.comas well as forpentilescu.comitself!". This allows us to have a single X.509 certificate for all of our subdomains, which makes administration easier as only this certificate will have to be signed, afterwards, by a certificate authority!133 +This is possible because of the existence of wildcard X.509 certificates, where you can basically say "This certificate is meant to protect traffic for all subdomains under transistor.one as well as for transistor.one itself!". This allows us to have a single X.509 certificate for all of our subdomains, which makes administration easier as only this certificate will have to be signed, afterwards, by a certificate authority! 134 134 135 135 Now, you might look at this and say to yourself: "Well, that's all fine and good but where are you opening port 80 for remote connections? Isn't that the standard HTTP port? In the above configuration, you're only listening on port 443, which is specifically only HTTPS! Wouldn't this cause connecting clients that only use bare HTTP to fail?". 136 136 Well, yes, this is an issue. But that's why there's more to show you, in the next section! ... ... @@ -140,7 +140,7 @@ 140 140 141 141 Time to get into the groove of things! 142 142 First, we shall create a new configuration file in "/etc/nginx/sites-available" and then a symbolic link towards it in "/etc/nginx/sites-enabled" called "fallback.conf". Why? 143 -Well, basically, we want for any HTTP request to our website, either to " pentilescu.com" or to any subdomain of "pentilescu.com" to redirect that request to the exact same destination but using the HTTPS protocol, instead.143 +Well, basically, we want for any HTTP request to our website, either to "transistor.one" or to any subdomain of "transistor.one" to redirect that request to the exact same destination but using the HTTPS protocol, instead. 144 144 This is considered good practice, because, you're eseentially redirecting the browser to use a more secured connection protocol. Not only will this ensure that any login credentials are protected from being stolen, but also that the exact resources being retrieved from our server are obfuscated, so that our visitors can browse our server privately. 145 145 146 146 The contents of this "fallback.conf" file are as follows: ... ... @@ -155,8 +155,8 @@ 155 155 } 156 156 157 157 server { 158 - server_name pentilescu.com;159 - return 301 https://alexandru. pentilescu.com$request_uri;158 + server_name transistor.one; 159 + return 301 https://alexandru.transistor.onem$request_uri; 160 160 161 161 listen 80; 162 162 listen [::]:80; ... ... @@ -188,16 +188,17 @@ 188 188 189 189 To put it as simply as possible, suppose the aforementioned bitwarden.conf file is created and there's also the fallback.conf file as with the previous contents, as well. 190 190 191 -When a new request arrives to our server, destined to passwords. pentilescu.comport 443, our server will look at both of these ".conf" files. Let's suppose it starts looking with the fallback.conf file.191 +When a new request arrives to our server, destined to passwords.transistor.one port 443, our server will look at both of these ".conf" files. Let's suppose it starts looking with the fallback.conf file. 192 192 It sees 3 "server" blocks in fallback.conf, the first of which matches for any incoming connections for port 443 (which matches our request) and matches for literally any server name (that's what the "_" after the "server" directive means). Since it matches for any server name, this is not a very specific match. Nginx will remember this as the closest match it has for now and then continue to look for other "server" blocks as well. 193 193 194 -The second "server" block only matches for requests directed at " pentilescu.com", not for any of its subdomains. As the request was made specifically to passwords.pentilescu.com, which is a subdomain ofpentilescu.com, this does not match with this block at all. Nginx continues.194 +The second "server" block only matches for requests directed at "transistor.one", not for any of its subdomains. As the request was made specifically to passwords.transistor.one, which is a subdomain of transistor.one, this does not match with this block at all. Nginx continues. 195 195 196 196 The third "server" block matches with the server_name directive (as the server name is the "_" wildcard once again). However, this matches only for incoming connections coming to port 80. Our request is to port 443 though. As such, this match fails and Nginx will continue to look for more "server" directives. 197 197 198 198 Then, Nginx will also take a look at the bitwarden.conf file. 199 199 200 -Here, there is one single "server" block, one which matches exactly with the "passwords.pentilescu.com" server name (as well as the 443 port). As this one has an exact match to both server_name and port, this is the most specific one to match. 200 +Here, there is one single "server" block, one which matches exactly with the "passwords. 201 +" server name (as well as the 443 port). As this one has an exact match to both server_name and port, this is the most specific one to match. 201 201 202 202 As such, Nginx will resolve to use that server block for this request and redirect it to that server's port. As such, the configuration from bitwarden.conf won over the configurations in fallback.conf. 203 203 ... ... @@ -205,13 +205,13 @@ 205 205 206 206 Now, you might wonder, what's the purpose of the first server block in fallback.conf, then. Well, fallback.conf should contain, ideally, resolution requests for stuff that doesn't match anything that's specific. 207 207 208 -For example, suppose the aforementioned setup, but with a request for "abcdefg. pentilescu.com" port 443. Where should such a request go to?209 +For example, suppose the aforementioned setup, but with a request for "abcdefg.transistor.one" port 443. Where should such a request go to? 209 209 210 -Well, it doesn't match the "passwords. pentilescu.com" server_name block in bitwarden.conf. Nor does it match the "pentilescu.com" server block either, as it's a subdomain forpentilescu.com. Finally, it doesn't match to the third "server" directive in fallback.conf either, as that is destined only for connections incoming to port 80, not 443. The only matching candidate, as such, is the first "server" block in fallback.conf, which resolves to any name whatsoever, due to its wildcard server_name. In this situation, Nginx will just take it, as it's the only candidate. It also has the "default server" descriptor for port 443, which means that, even if the server name parameter didn't match, this would have still been the "server" block to handle it. The action detailed for this "server" block is "return 404", which tells Nginx to simply return a 404 status code, immediately. The browser will then report this "404" status code to the visitor, letting him know that the service he/she was attempting to access does not exist on this server, an indication that their request was malformed.211 +Well, it doesn't match the "passwords.transistor.one" server_name block in bitwarden.conf. Nor does it match the "transistor.one" server block either, as it's a subdomain for transistor.one. Finally, it doesn't match to the third "server" directive in fallback.conf either, as that is destined only for connections incoming to port 80, not 443. The only matching candidate, as such, is the first "server" block in fallback.conf, which resolves to any name whatsoever, due to its wildcard server_name. In this situation, Nginx will just take it, as it's the only candidate. It also has the "default server" descriptor for port 443, which means that, even if the server name parameter didn't match, this would have still been the "server" block to handle it. The action detailed for this "server" block is "return 404", which tells Nginx to simply return a 404 status code, immediately. The browser will then report this "404" status code to the visitor, letting him know that the service he/she was attempting to access does not exist on this server, an indication that their request was malformed. 211 211 212 212 This block effectively handles all malformed requests or any request that does not have a specific resolver for it. 213 213 214 -The second "server" block in fallback.conf is specifically for all requests coming to " pentilescu.com", not any of its subdomains. It listens to both ports 80 and 443 and, when it matches, it will return an HTTP code 301 to "https://alexandru.pentilescu.com$request_uri", effectively preserving the exact same URI that was used in the original request but simply redirecting it forcefully to the "alexandru.pentilescu,com", so that any requests to "pentilescu.com" will redirect the browser to "alexandru.pentilescu.com". Basically, this is a forced redirection so that, when accessing the naked domain, one would forcefully be redirected to a specific subdomain. This is just a quirck of my own website. You may choose not to include this behavior in your own platform.215 +The second "server" block in fallback.conf is specifically for all requests coming to "transistor.one", not any of its subdomains. It listens to both ports 80 and 443 and, when it matches, it will return an HTTP code 301 to "https://alex.transistor.one$request_uri", effectively preserving the exact same URI that was used in the original request but simply redirecting it forcefully to the "alexandru.pentilescu,com", so that any requests to "transistor.one" will redirect the browser to "alex.transistor.one". Basically, this is a forced redirection so that, when accessing the naked domain, one would forcefully be redirected to a specific subdomain. This is just a quirck of my own website. You may choose not to include this behavior in your own platform. 215 215 216 216 Finally, the third and last "server" block in our fallback.conf matches to literally any server name due to its wildcard and is the default server for port 80. Being the default server, this means this block will handle all requests coming from port 80 to our server that doesn't have a match. The action of this block is to return a 301 HTTP code to "https://$host$request_uri". "https://$host$request_uri" will resolve to the exact same URL as the original requested one, except that it has the "https://" prefix before it, instead. This means that we're telling Nginx that, for any request that doesn't have a better match to it coming from port 80 (i.e. through the HTTP protocol) we must send back a HTTP response code of 301 (i.e. redirect to) to redirect our visitor to the exact same URL that they already used but with the "https://" prefix appended, effectively forcing them to use port 443 instead. This will move all their requests from HTTP to HTTPS to automatically secure them with encryption! 217 217 ... ... @@ -228,9 +228,9 @@ 228 228 Now, what should this ssl.conf snippets file contain? Easy: 229 229 230 230 {{code language="nginx"}} 231 -ssl_certificate /etc/letsencrypt/live/ pentilescu.com/fullchain.pem; # managed by Certbot232 -ssl_certificate_key /etc/letsencrypt/live/ pentilescu.com/privkey.pem; # managed by Certbot233 -ssl_trusted_certificate /etc/letsencrypt/live/ pentilescu.com/chain.pem;232 +ssl_certificate /etc/letsencrypt/live/transistor.one/fullchain.pem; # managed by Certbot 233 +ssl_certificate_key /etc/letsencrypt/live/transistor.one/privkey.pem; # managed by Certbot 234 +ssl_trusted_certificate /etc/letsencrypt/live/transistor.one/chain.pem; 234 234 {{/code}} 235 235 236 236 Now, I admit, these file paths are usually generated by the certbot utility. Configuring certbox is outside the scope of this article and I will not cover it. ... ... @@ -250,7 +250,7 @@ 250 250 251 251 {{code language="nginx"}} 252 252 server { 253 - server_name git. pentilescu.com;254 + server_name git.transistor.one; 254 254 255 255 listen [::]:443 ssl http2; # managed by Certbot 256 256 listen 443 ssl http2; # managed by Certbot ... ... @@ -264,7 +264,7 @@ 264 264 } 265 265 {{/code}} 266 266 267 -This will redirect all requests meant for "git. pentilescu.com" to localhost port 3000. It also supports TLS, as usual.268 +This will redirect all requests meant for "git.transistor.one" to localhost port 3000. It also supports TLS, as usual. 268 268 269 269 Once you've got everything ready, run the following command to test all your configuration files at once: 270 270