Nginx Embedded Variables - How do you get the domain name used in a request?

9,263

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

Share:
9,263

Related videos on Youtube

Forrest Wilkins
Author by

Forrest Wilkins

Updated on September 18, 2022

Comments

  • Forrest Wilkins
    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