Created
October 4, 2021 08:09
-
-
Save sebastian13/2e85fb6be13a3f2204d6c6c351dab54c to your computer and use it in GitHub Desktop.
Redirect any server_name to the first one defined
This file contains 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
server { | |
listen 80; | |
listen [::]:80; | |
server_name example.com 1.example.com; | |
# Redirect all HTTP links to the matching HTTPS page | |
location / { | |
return 301 https://$host$request_uri; | |
} | |
} | |
server { | |
server_name example.com 1.example.com; | |
listen 443 ssl http2 ; | |
ssl_certificate /etc/nginx/ssl/live/example.com/fullchain.pem; | |
ssl_certificate_key /etc/nginx/ssl/live/example.com/privkey.pem; | |
ssl_trusted_certificate /etc/nginx/ssl/live/example.com/chain.pem; | |
# Forward to the first Server Name | |
# https://stackoverflow.com/a/40411590 | |
if ($host != $server_name) { | |
rewrite ^/(.*) $scheme://$server_name/$1 permanent; | |
} | |
location / { | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment