-1

I have tried all the solutions found on this site and others but am still having a problem with redirecting a wildcard subdomain with http to https. I still end up with

https://%2A.example.com/site_admin

The above url displays in Chrome and "This page can not be displayed" in IE and "Your connection is not private" from chrome on my phone.

The non-wildcard nginx server blocks work fine.

I have tried rewrite and redirect 301 in the server block for the http wildcards, but it still rewrites the url to %A2 (the *). I think the default server is using the server_name of *.example.com but even when using $host it still rewrites as the *. Is there another directive that needs to be included?

server {
    listen  80;
    server_name example.com www.example.com;

    # tell users to go to SSL version this time
    if ($ssl_protocol = "") {
       rewrite     ^   https://$server_name$request_uri? permanent;
    }
}
server {
    listen 443 ssl;
    server_name example.com www.example.com;

    ## ssl and locations not shown
}

server {
    listen  80;
    server_name manager.example.com ;

    # tell users to go to SSL version this time
    if ($ssl_protocol = "") {
       rewrite     ^   https://$server_name$request_uri? permanent;
    }
}
server {
    listen 443 ssl;
    server_name manager.example.com ;

    ## ssl and locations not shown
}

server {
    listen  80 default_server;
    server_name *.example.com "" ;

    # tell users to go to SSL version this time
    if ($ssl_protocol = "") {
        rewrite     ^   https://$host$request_uri? permanent;
        #rewrite     ^   https://$server_name$request_uri? permanent;
}

}
server {
    listen 443  default_server;
    server_name *.example.com ;

    ssl on;
    ## other ssl and locations not shown
}
2
  • There's a lot of general nginx misunderstanding/bad practices in there. You should simply be using $host. It's somewhat bewildering why you wouldn't just use server { listen 80 default_server; return 301 https://$host$request_uri; } (or very similar) to replace all three of your http server blocks.
    – AD7six
    Nov 20, 2014 at 20:42
  • Yes I was wondering about the if statement testing for ssl_protocol. I'll change to the return 301. Thanks.
    – AllanD
    Nov 20, 2014 at 21:23

2 Answers 2

1

Turned out to be the browser cache. Once it was cleared the redirects worked. Sometimes it is the simplest thing.

2
  • Thanks @AllanD I had the same issue with %2A appearing in my subdomain redirect and it turned out to be a Chrome cache issue. You should probably accept this answer as your solution.
    – randlet
    Aug 24, 2016 at 23:55
  • You've gotta whatch out with this config as the $host header can be easily spoofed and nginx will respect it curl -L -H 'Host: google.com' subdomain.yourdomain.local Using host for it is totally fine in dev but I would advise not to use $host in production. Nov 3, 2017 at 17:48
0

You missed the actual ssl statement in what is supposed to be a wildcard ssl server {} block, thus you actually have a http on 443 port instead of https.

1
  • Sorry, I was trying different things and used "ssl on" instead of ssl in the listen directive as I though it may interfere with the default_server and forgot to show it. Just edited. The browser actually redirects to https it just does not pass the host correctly.
    – AllanD
    Nov 20, 2014 at 18:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .