Skip to content

Instantly share code, notes, and snippets.

@johannesvogel
Created December 28, 2025 13:03
Show Gist options
  • Select an option

  • Save johannesvogel/9643361a21751d8274cc12aec5df6cbd to your computer and use it in GitHub Desktop.

Select an option

Save johannesvogel/9643361a21751d8274cc12aec5df6cbd to your computer and use it in GitHub Desktop.
nginx-redirector
# NGINX REDIRECTOR
#
# This nginx config will redirect a URL from redirect.example.com to any domain passed in using the redirectURL parameter
#
# Example:
# https://redirect.example.com/api/assets?redirectURL=mydomain.com
# would be redirected to
# https://mydomain.com/api/assets
#
# The redirect is done by returning an HTTP 307 (Temporary Redirect) response. With this response code the client will
# do the exat some request again at the new URL (passed in the `Location` header). The response will be sent once the
# headers have been read, meaning the body will probably not be fully sent to the "redirector".
server {
server_name redirect.example.com;
listen 443 ssl http2;
listen [::]:443 ssl http2;
location / {
# Respond to request immediately after having read the headers
client_body_buffer_size 16k;
client_max_body_size 10g;
proxy_request_buffering off;
client_body_in_single_buffer off;
# Return 400 if redirectURL is missing
if ($arg_redirectURL = "") {
return 400 "Missing redirectURL parameter";
}
# Remove redirectURL from query string
set $qs "";
if ($args ~* "(?:^|&)redirectURL=[^&]*(?:&(.*))?$") {
set $qs $1;
}
# Ensure redirect URL starts with https://
set $target https://$arg_redirectURL;
# Build full redirect URL with path
set $full_redirect $target$uri;
if ($qs != "") {
set $full_redirect $target$uri?$qs;
}
# Issue a 307 redirect
return 307 $full_redirect;
}
ssl_certificate /etc/letsencrypt/live/redirect.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/redirect.example.com/privkey.pem; # managed by Certbot
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment