aboutsummaryrefslogtreecommitdiff
path: root/blog/2022-12-07-nginx-wildcard-redirect.org
diff options
context:
space:
mode:
Diffstat (limited to 'blog/2022-12-07-nginx-wildcard-redirect.org')
-rw-r--r--blog/2022-12-07-nginx-wildcard-redirect.org116
1 files changed, 60 insertions, 56 deletions
diff --git a/blog/2022-12-07-nginx-wildcard-redirect.org b/blog/2022-12-07-nginx-wildcard-redirect.org
index 88c76c0..81b12bc 100644
--- a/blog/2022-12-07-nginx-wildcard-redirect.org
+++ b/blog/2022-12-07-nginx-wildcard-redirect.org
@@ -1,25 +1,26 @@
-+++
-date = 2022-12-07
-title = "Redirect Nginx Subdomains & Trailing Content with Regex"
-description= "You can easily redirect Nginx traffic from subdomains and TLDs to a new domain while preserving trailing content."
-+++
-
-## Problem
-
-I recently migrated domains and replaced the
-old webpage with a simple info page with instructions to users on how
-to edit their bookmarks and URLs to get to the page they were seeking.
-
-This was not ideal as it left the work up to the user and may have caused
-friction for users who accessed my RSS feed.
-
-## Solution
-
-Instead, I finally found a solution that allows me to redirect both subdomains
-AND trailing content. For example, both of these URLs now redirect properly
-using the logic I'll explain below:
-
-```txt
+#+title: Redirect Nginx Subdomains & Trailing Content with Regex
+#+date: 2022-12-07
+
+** Problem
+:PROPERTIES:
+:CUSTOM_ID: problem
+:END:
+I recently migrated domains and replaced the old webpage with a simple
+info page with instructions to users on how to edit their bookmarks and
+URLs to get to the page they were seeking.
+
+This was not ideal as it left the work up to the user and may have
+caused friction for users who accessed my RSS feed.
+
+** Solution
+:PROPERTIES:
+:CUSTOM_ID: solution
+:END:
+Instead, I finally found a solution that allows me to redirect both
+subdomains AND trailing content. For example, both of these URLs now
+redirect properly using the logic I'll explain below:
+
+#+begin_src txt
# Example 1 - Simple base domain redirect with trailing content
https://domain1.com/blog/alpine-linux/ -> https://domain2.com/blog/alpine-linux/
@@ -27,23 +28,25 @@ https://domain1.com/blog/alpine-linux/ -> https://domain2.com/blog/alpine-linux/
https://libreddit.domain1.com/r/history/comments/7z8cbg/new_discovery_mode_turns_video_game_assassins/
->
https://libreddit.domain2.com/r/history/comments/7z8cbg/new_discovery_mode_turns_video_game_assassins/
-```
+#+end_src
Go ahead, try the URLs if you want to test them.
-### Nginx Config
+*** Nginx Config
+:PROPERTIES:
+:CUSTOM_ID: nginx-config
+:END:
+To make this possible. I needed to configure a proper redirect scheme in
+my Nginx configuration.
-To make this possible. I needed to configure a proper redirect scheme in my
-Nginx configuration.
-
-```sh
+#+begin_src sh
doas nano /etc/nginx/http.d/domain1.conf
-```
+#+end_src
-Within this file, I had one block configured to redirect HTTP requests to HTTPS
-for the base domain and all subdomains.
+Within this file, I had one block configured to redirect HTTP requests
+to HTTPS for the base domain and all subdomains.
-```conf
+#+begin_src conf
server {
listen [::]:80;
listen 80;
@@ -59,13 +62,14 @@ server {
return 404;
}
-```
+#+end_src
-For the base domain, I have another `server` block dedicated to redirecting all
-base domain requests. You can see that the `rewrite` line is instructing Nginx
-to gather all trailing content and append it to the new `domain2.com` URL.
+For the base domain, I have another =server= block dedicated to
+redirecting all base domain requests. You can see that the =rewrite=
+line is instructing Nginx to gather all trailing content and append it
+to the new =domain2.com= URL.
-```conf
+#+begin_src conf
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
@@ -77,20 +81,21 @@ server {
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
}
-```
+#+end_src
-Finally, the tricky part is figuring out how to tell Nginx to redirect while
-keeping both a subdomain and trailing content intact. I found that the easiest
-way to do this is to give it a `server` block of its own.
+Finally, the tricky part is figuring out how to tell Nginx to redirect
+while keeping both a subdomain and trailing content intact. I found that
+the easiest way to do this is to give it a =server= block of its own.
-Within this block, we need to do some regex on the `server_name` line before we
-can rewrite anything. This creates a variable called `subdomain`.
+Within this block, we need to do some regex on the =server_name= line
+before we can rewrite anything. This creates a variable called
+=subdomain=.
-Once the server gets to the `rewrite` line, it pulls the `subdomain` variable
-from above and uses it on the new `domain2.com` domain before appending the
-trailing content (`$request_uri`).
+Once the server gets to the =rewrite= line, it pulls the =subdomain=
+variable from above and uses it on the new =domain2.com= domain before
+appending the trailing content (=$request_uri=).
-```conf
+#+begin_src conf
server {
listen [::]:443 ssl http2;
listen 443 ssl http2;
@@ -102,18 +107,17 @@ server {
ssl_certificate /etc/letsencrypt/live/domain1.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain1.com/privkey.pem;
}
-```
+#+end_src
-That's all there is to it. With this, I simply restarted Nginx and watched the
-redirections work in-action.
+That's all there is to it. With this, I simply restarted Nginx and
+watched the redirections work in-action.
-```sh
+#+begin_src sh
doas rc-service nginx restart
-```
-
+#+end_src
-Looking back on it, I wish I had done this sooner. Who knows how many people
-went looking for my sites or bookmarks and gave up when they saw the redirect
-instructions page.
+Looking back on it, I wish I had done this sooner. Who knows how many
+people went looking for my sites or bookmarks and gave up when they saw
+the redirect instructions page.
Oh well, it's done now. Live and learn.