Created
December 28, 2025 13:03
-
-
Save johannesvogel/9643361a21751d8274cc12aec5df6cbd to your computer and use it in GitHub Desktop.
nginx-redirector
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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