Redirect Domain on Laravel Forge using NGINX while preserving Let's Encrypt Cert...
source link: https://stefanzweifel.dev/posts/2023/08/26/redirect-domain-on-laravel-forge-using-nginx-while-preserving-lets-encrypt-certificate
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Redirect Domain on Laravel Forge using NGINX while preserving Let's Encrypt Certificate
August 26, 2023
Recently I needed to redirect the traffic for an entire domain, that is hosted on Laravel Forge, to another domain while preserving the SSL certificate from Let's Encrypt.
The paths should be mapped 1:1. So http://example.com/path/to/site
would be redirected to http://new-example.com/path/to/site
.
To ensure that certbot
on Laravel Forge can still issue a valid SSL certificate for the now deprecated example.com
domain, I needed to make sure that requests to /.well-known/acme-challenge/
are not redirected.
After tinkering with various ideas I landed on the following solution.
location / {
# Check if request is used to generate Let's Encrypt SSL Certificate
if ($request_uri !~ ^/.well-known/acme-challenge/) {
# Redirect to the new domain including query parameters
return 301 https://example.com$request_uri;
}
# Create alias to /home/forge/.letsencrypt directory.
# Taken from /etc/nginx/forge-conf/example.com/server/letsencrypt_challenge.conf
auth_basic off;
allow all;
alias /home/forge/.letsencrypt;
}
Replace example.com
with the domain you would like to redirect to and then replace the existing location / { try_files index.php }
block with the snippet above in your Laravel Forge NGINX configuration.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK