# Ssl redirect issue

**URL:** <https://www.ruby-forum.com/t/ssl-redirect-issue/212106>\
**Category:** NGINX\
**Created:** [September 22, 2011, 11:18am UTC](https://www.ruby-forum.com/t/ssl-redirect-issue/212106 "2011-09-22T11:18:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [September 22, 2011, 11:18am UTC](https://www.ruby-forum.com/t/ssl-redirect-issue/212106/1 "2011-09-22T11:18:09Z")

</div>

Hi,

I have an ssl nginx setup which was working fine with one server { ssl  
on }. However I have legacy ssl server\_names that I would like to have  
a ‘catch all’ for that should be redirected back to the ‘main’ ssl  
server name. Config is pasted here.

[#user nobody;worker\_processes 6;#error\_log logs/error.log;#error\_log - Pastebin.com](http://pastebin.com/dXU7BHaB) ( I removed some sections for brevity  
)

When the following lines are enabled:

```
server {
    listen 443 default;
    server_name _;
    rewrite ^(.*) https://my.example.net$1 permanent;
}

```

I will get:

SSL received a record that exceeded the maximum permissible length.  
(Error code: ssl\_error\_rx\_record\_too\_long)

If i comment those lines out, the request will go thru just fine.  
However since:

```
server {
    listen 443;
    server_name my.example.net;

```

is treated as the default vhost, even ssl requests for  
[myold.example.net](http://myold.example.net) are serviced by this server {} and are not redirected  
to [https://my.example.net](https://my.example.net) (obviously not what I want).

Thanks for the help in advanced.

---

<div class="post-metadata">

**Author:** ![Adam\_Tistler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Adam\_Tistler](https://www.ruby-forum.com/u/Adam_Tistler)\
**Post date:** [September 22, 2011, 1:24pm UTC](https://www.ruby-forum.com/t/ssl-redirect-issue/212106/2 "2011-09-22T13:24:33Z")

</div>

Actually, I can work around this issue by adding:

```
    if ($host !~* ^(my.example.net)$ ) {
       rewrite ^(.*) https://my.example.net$1 permanent;
    }

```

I am curious however this does not work when I put the redirect in the  
default ( ‘\_’ ) server config

---

<div class="post-metadata">

**Author:** ![Adam\_Tistler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@Adam\_Tistler](https://www.ruby-forum.com/u/Adam_Tistler)\
**Post date:** [September 22, 2011, 2:42pm UTC](https://www.ruby-forum.com/t/ssl-redirect-issue/212106/3 "2011-09-22T14:42:04Z")

</div>

On 22 Set 2011 12h23 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually, I can work around this issue by adding:
> 
> if ($host !~\* ^([my.example.net](http://my.example.net))$ ) {  
> rewrite ^(.\*) [https://my.example.net$1](https://my.example.net$1) permanent;  
> }
> 
> I am curious however this does not work when I put the redirect in  
> the default ( ‘\_’ ) server config

‘\_’ is a not a valid hostname. The reason it’s used in the recommended  
configuration for default servers is that there’s no risk of colliding  
with anything else and you can have individual vhost configs for each  
proper hostname.

Your config should be something like this:

server {  
server\_name [my.example.net](http://my.example.net);  
return 301 https://myexample.net$request\_uri;  
}

Then you should have the regular HTTPs server config that listens on  
port 443.

server {  
listen 443 ssl;  
server\_name [my.example.net](http://my.example.net);

```
# remaining server config...

```

}

Repeat the above for each domain redirect you want to have.

Additional reading:

> **[Configuring HTTPS servers](http://nginx.org/en/docs/http/configuring_https_servers.html)**

> **[Server names](http://nginx.org/en/docs/http/server_names.html)**

— appa
