Joseph
Joseph

Reputation: 865

Fourth-Level Subdomain Forwarding

I've recently been trying to set up a reverse proxy that would forward certain 4th-level subdomains to particular locations. So, for example, this is what I'm trying to accomplish (configuration in my nginx file):

server {
    listen 80;
    server_name *.server.domain.com;
    rewrite ^ https://$server_name$request_uri;
}

The goal here being that if someone went to, for example, http://item1.server.domain.com, they would be re-routed to https://item1.server.domain.com. However, with this configuration, the URL gets rewritten to https://%2A.server.domain.com.

Is there a way to fix this so that the full domain (item1) gets added correctly to the rewritten URL? Ideally, I wanted it to eventually be able to rewrite any subdomain on server.domain.com directly to https.

Thanks!

Upvotes: 1

Views: 1612

Answers (1)

Richard Smith
Richard Smith

Reputation: 49822

The $server_name variable contains the text from the value of the server_name directive. The %2A is a URL encoded representation of the leading *.

Use $host or $http_host to obtain the hostname actually requested by the client. See this document for more.

For example:

server {
    listen 80;
    server_name *.server.domain.com;
    return 301 https://$host$request_uri;
}

Note: Restart nginx and clear the browser cache between each test. Check the configuration using nginx -T.

Upvotes: 2

Related Questions