Nginx Embedded Variables - How do you get the domain name used in a request?
Domain name that used in request is basically a host request header. In nginx, variable for host header is $host. So if you want to redirect based on domain name/host you should change the configuration to :
return 301 https://$host$request_uri;
Hope this help . Thanks
Related videos on Youtube
Forrest Wilkins
Updated on September 18, 2022Comments
-
Forrest Wilkins over 1 year
When using multiple domain names for one server, how do you get the domain name currently being used in a request? I'm using the embedded variables for Nginx in it's configuration file.
I've set up OpenSSL and have a redirect that works fine for my first domain since, as you can see, I'm explicitly redirecting to it at the bottom of my config file. So an HTTP request on any of the other domains redirects to that first one for a secure connection. Is there an embedded variable I can use, similar to
$request_uri
, for example, but just returns the given domain name used by the client?Here's the server block where I'm running the redirect.
server { listen 80; listen [::]:80; server_name example.com example1.com example2.com example3.com; location / { return 301 https://example.com$request_uri; } }
And the rest of my config: https://pastebin.com/HgnZ0aBe
-
Xedret almost 5 yearsHere is a link that contains all the variables used in the nginx conf files nginx.org/en/docs/varindex.html
-
Xedret almost 5 yearsAnd this would be the link to the variable usage of what you are looking for nginx.org/en/docs/http/ngx_http_core_module.html#var_host
-