# Redirect from http to https

**URL:** <https://www.ruby-forum.com/t/redirect-from-http-to-https/162336>\
**Category:** NGINX\
**Created:** [March 8, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336 "2009-03-08T21:28:46Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 8, 2009, 9:28pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/1 "2009-03-08T21:28:46Z")

</div>

Hi

I’m trying to redirect all income request from http to https: i’ve  
tried the rewrite module , but i always got the redirect in a loop  
issue. please help.

server {  
listen 80;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
rewrite ^(.\*) https://$server\_name$1 permanent;

```
   '''''''
   }

```

server {  
listen 443;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
…  
}

Thank You.

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 8, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/2 "2009-03-08T23:52:14Z")

</div>

On Sun, Mar 08, 2009 at 09:28:46PM +0100, Steve Z. wrote:

> Hi
> 
> I’m trying to redirect all income request from http to https: i’ve  
> tried the rewrite module , but i always got the redirect in a loop  
> issue. please help.

You need to enable SSL on port 443.

> ```
> listen 443;
> server_name www.domain.com domain.com;
> 
> ```

- ssl\_certificate /path/to/server.crt;
- ssl\_certificate\_key /path/to/server.key;
- ssl on;

> ```
> .....
> }
> 
> ```

Best regards,  
Grzegorz N. (replying by diffs is fun! ;))

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 9, 2009, 12:04am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/3 "2009-03-09T00:04:22Z")

</div>

Hi, thanks for the reply, i enable SSL on port 443, i just didn’t paste  
it. Here is a more complete version. if i use [https://domain.com](https://domain.com), it  
works no problem, just when i tried to redirect from http to https, it  
went into a redirect loop..

server {  
listen 80;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
rewrite ^(.\*) https://$server\_name$1 permanent;

location / {

```
             # needed for HTTPS
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For 

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;  
}  
}

server {  
listen 443;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);

```
    ssl on;
    ssl_certificate /path/myssl.crt;
    ssl_certificate_key /path/myssl.key;

    ssl_session_timeout 5m;

```

location / {  
proxy\_pass [http://mysvr](http://mysvr);  
}  
}

Thank You

Grzegorz N. wrote:

> On Sun, Mar 08, 2009 at 09:28:46PM +0100, Steve Z. wrote:
> 
> > Hi
> > 
> > I’m trying to redirect all income request from http to https: i’ve  
> > tried the rewrite module , but i always got the redirect in a loop  
> > issue. please help.
> 
> You need to enable SSL on port 443.
> 
> > ```
> > listen 443;
> > server_name www.domain.com domain.com;
> > 
> > ```
> 
> - ssl\_certificate /path/to/server.crt;
> - ssl\_certificate\_key /path/to/server.key;
> - ssl on;
> 
> > ```
> > .....
> > }
> > 
> > ```
> 
> Best regards,  
> Grzegorz N. (replying by diffs is fun! ;))

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 9, 2009, 12:23am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/4 "2009-03-09T00:23:48Z")

</div>

On Sun, Mar 8, 2009 at 3:04 PM, Steve Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, thanks for the reply, i enable SSL on port 443, i just didn’t paste  
> it. Â Here is a more complete version. Â if i use [https://domain.com](https://domain.com), it  
> works no problem, just when i tried to redirect from http to https, it  
> went into a redirect loop..

i don’t see why it would be (first off)

try using lynx -mime\_header [http://foo.com/](http://foo.com/) and see what it says. i do  
http to https all the time without an issue. you need to inspect the  
headers and what is happening on the client side. firebug for firefox  
or fiddler even could help too.

why do you have this chunk? these headers are being sent to the  
upstream and there is no upstream.

> location / {
> 
> Â Â Â Â Â Â Â Â # needed for HTTPS  
> Â Â Â Â Â Â Â Â proxy\_set\_header Â X-Real-IP Â $remote\_addr;  
> Â Â Â Â Â Â Â Â proxy\_set\_header Â X-Forwarded-For  
> $proxy\_add\_x\_forwarded\_for;  
> Â Â Â Â Â Â Â Â proxy\_set\_header Host $http\_host;  
> Â Â Â Â }  
> }

> Â location / {  
> Â Â Â proxy\_pass Â [http://mysvr](http://mysvr);

this would be where you would put the proxy\_set\_headers

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 9, 2009, 1:08am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/5 "2009-03-09T01:08:10Z")

</div>

Hi mike, i have a upstream portion:

upstream mysvr {  
server 127.0.0.1 weight=1;  
}

Is there other things i’m missing besides rewrite module to do the  
redirect from http to https? I’m still new to nginx. any help is  
appreciated.. Thank You

mike wrote:

> On Sun, Mar 8, 2009 at 3:04 PM, Steve Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hi, thanks for the reply, i enable SSL on port 443, i just didn’t paste  
> > it. Â Here is a more complete version. Â if i use [https://domain.com](https://domain.com), it  
> > works no problem, just when i tried to redirect from http to https, it  
> > went into a redirect loop..
> 
> i don’t see why it would be (first off)
> 
> try using lynx -mime\_header [http://foo.com/](http://foo.com/) and see what it says. i do  
> http to https all the time without an issue. you need to inspect the  
> headers and what is happening on the client side. firebug for firefox  
> or fiddler even could help too.
> 
> why do you have this chunk? these headers are being sent to the  
> upstream and there is no upstream.
> 
> > location / {
> > 
> > Â Â Â Â Â Â Â Â # needed for HTTPS  
> > Â Â Â Â Â Â Â Â proxy\_set\_header Â X-Real-IP Â $remote\_addr;  
> > Â Â Â Â Â Â Â Â proxy\_set\_header Â X-Forwarded-For  
> > $proxy\_add\_x\_forwarded\_for;  
> > Â Â Â Â Â Â Â Â proxy\_set\_header Host $http\_host;  
> > Â Â Â Â }  
> > }
> 
> > Â location / {  
> > Â Â Â proxy\_pass Â [http://mysvr](http://mysvr);
> 
> this would be where you would put the proxy\_set\_headers

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 9, 2009, 2:55am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/6 "2009-03-09T02:55:14Z")

</div>

your proxy\_set\_header stuff would go where you proxy things, not on  
your redirection

your http to https should be a simple

server {  
listen 80;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
rewrite ^(.\*) https://$server\_name$1 permanent;  
}

this is literally one i have working flawlessly (just changed the  
domain):

```
    server {
            listen 80;
            server_name foo.com bar.com;
            rewrite ^/(.*) https://foo.com/$1 permanent;
    }

```

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 9, 2009, 4:43am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/7 "2009-03-09T04:43:48Z")

</div>

Thanks mike, it’s working now, but i encounter another problem, every  
time i visited the website, it had so many log entries, like few hundred  
of them. all from the same ip address. Any ideas?

thank you

mike wrote:

> your proxy\_set\_header stuff would go where you proxy things, not on  
> your redirection
> 
> your http to https should be a simple
> 
> server {  
> listen 80;  
> server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
> rewrite ^(.\*) https://$server\_name$1 permanent;  
> }
> 
> this is literally one i have working flawlessly (just changed the  
> domain):
> 
> ```
> server {
> listen 80;
> server_name foo.com bar.com;
> rewrite ^/(.*) https://foo.com/$1 permanent;
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 12:42am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/8 "2009-03-10T00:42:15Z")

</div>

No, only myself are testing the site, when i visiting the site, somehow  
it generates so many log entries at the same time…

127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux  
i686; en-US; rv:1.9b5) Gecko/2008041514 Firefox/3.0b5”  
127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux  
i686; en-US; rv:1.9b5) Gecko/2008041514 Firefox/3.0b5”  
127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux  
i686; en-US; rv:1.9b5) Gecko/2008041514 Firefox/3.0b5”  
127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux  
i686; en-US; rv:1.9b5) Gecko/2008041514 Firefox/3.0b5”

mike wrote:

> a) is debugging on ?
> 
> b) maybe you’re getting flooded, or you’re popular somehow?
> 
> c) is it a search engine crawler, or some application like it? i’ve  
> been flooded with a bunch of requests per minute for weeks before we  
> noticed someone was hitting our website for no good reason…

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 9, 2009, 5:27am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/9 "2009-03-09T05:27:24Z")

</div>

a) is debugging on ?

b) maybe you’re getting flooded, or you’re popular somehow?

c) is it a search engine crawler, or some application like it? i’ve  
been flooded with a bunch of requests per minute for weeks before we  
noticed someone was hitting our website for no good reason…

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 1:01am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/10 "2009-03-10T01:01:17Z")

</div>

no clue, maybe there’s multiple access log directives and each one is  
active

i’d have to defer to igor to answer/ask the better questions

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 1:13am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/11 "2009-03-10T01:13:02Z")

</div>

one more question, now the website can be redirect to https if i enter  
[http://domain.com/newpage.html](http://domain.com/newpage.html), but i don’t know somehow if i only enter  
[http://domain.com/](http://domain.com/), but doesn’t redirect and outputs and redirect loop  
error.

mike wrote:

> no clue, maybe there’s multiple access log directives and each one is  
> active
> 
> i’d have to defer to igor to answer/ask the better questions

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 6:43am UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/12 "2009-03-10T06:43:25Z")

</div>

curl -I [http://yoursite/favicon.ico](http://yoursite/favicon.ico)

I wouldn’t be supprised if you’re generating a redirect from  
/favicon.ico to  
favicon.ico.

Cheers

Dave

mike writes:

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 7:02pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/13 "2009-03-10T19:02:57Z")

</div>

I didn’t redirect from /favicon.ico, i think this happens when it goes  
into a loop, let’s say if i enter the [http://domain.com/status](http://domain.com/status), the log  
will show as  
127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /status  
HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5)  
Gecko/2008041514 Firefox/3.0b5”

Only when i enter [http://domain.com/](http://domain.com/) will show “/favicon.co” in the  
log.

127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5)  
Gecko/2008041514 Firefox/3.0b5”

Dave C. wrote:

> curl -I [http://yoursite/favicon.ico](http://yoursite/favicon.ico)
> 
> I wouldn’t be supprised if you’re generating a redirect from  
> /favicon.ico to  
> favicon.ico.
> 
> Cheers
> 
> Dave
> 
> mike writes:

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 8:07pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/14 "2009-03-10T20:07:15Z")

</div>

On Tue, Mar 10, 2009 at 07:02:57PM +0100, Steve Z. wrote:

> 127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
> HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5)  
> Gecko/2008041514 Firefox/3.0b5”

Could you create a debug log:

./configure --with-debug …

nginx.conf:

error\_log /path/to/log debug;

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 9:35pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/15 "2009-03-10T21:35:06Z")

</div>

On Tue, Mar 10, 2009 at 08:51:23PM +0100, Steve Z. wrote:

> 2009/03/10 15:40:32 [notice] 15197#0: \*4 rewritten redirect:  
> “[www.domain.com](http://www.domain.com)”  
> 127.0.0.1, server: [www.domain.com](http://www.domain.com), request: “GET / HTTP/1.0”, host:  
> “[www.domain.com](http://www.domain.com)”
> 
> …
> 
> 2009/03/10 15:40:36 [notice] 15197#0: \*42 rewritten redirect:  
> “[https://www.domain.com/](https://www.domain.com/)”, client: 127.0.0.1, server: [www.domain.com](http://www.domain.com),  
> request: “GET / HTTP/1.0”, host: “[www.domain.com](http://www.domain.com)”

As client address is 127.0.0.1, it seems that your HTTPS proxy\_pass

location / {  
proxy\_pass [http://mysvr](http://mysvr);  
}

proxies just to your first server:

server {  
listen 80;  
server\_name [www.domain.com](http://www.domain.com) [domain.com](http://domain.com);  
rewrite ^(.\*) https://$server\_name$1 permanent;  
}

and here is a loop.

---

<div class="post-metadata">

**Author:** ![stevezhuo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@stevezhuo](https://www.ruby-forum.com/u/stevezhuo)\
**Post date:** [March 10, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/redirect-from-http-to-https/162336/16 "2009-03-10T20:51:23Z")

</div>

42 entries were logged…

_1 "^/(._)" matches “/”, client: 10.10.10.10, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.1”, host: “[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:31 [notice] 15197#0: \*1 rewritten redirect:  
“[https://www.domain.com/](https://www.domain.com/)”, client: 10.10.10.10, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.1”, host: “[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: _4 "^/(._)" matches “/”, client:  
127.0.0.1, server: [www.domain.com](http://www.domain.com), request: “GET / HTTP/1.0”, host:  
“[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: \*4 rewritten redirect:  
“[https://www.domain.com/](https://www.domain.com/)”, client: 127.0.0.1, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.0”, host: “[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: _6 "^/(._)" matches “/”, client:  
127.0.0.1, server: [www.domain.com](http://www.domain.com), request: “GET / HTTP/1.0”, host:  
“[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: \*6 rewritten redirect:  
“[https://domain.com/](https://domain.com/)”, client: 127.0.0.1, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.0”, host: “[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: _8 "^/(._)" matches “/”, client:  
127.0.0.1, server: [www.domain.com](http://www.domain.com), request: “GET / HTTP/1.0”, host:  
“[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: \*8 rewritten redirect:  
“[https://www.domain.com/](https://www.domain.com/)”, client: 127.0.0.1, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.0”, host: “[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: _10 "^/(._)" matches “/”, client:  
127.0.0.1, server: [www.domain.com](http://www.domain.com), request: “GET / HTTP/1.0”, host:  
“[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:32 [notice] 15197#0: \*10 rewritten redirect:  
“[https://www.domain.com/](https://www.domain.com/)”, client: 127.0.0.1, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.0”, host: “[www.domain.com](http://www.domain.com)”  
2009/03/10 15:40:33 [notice] 15197#0: _12 "^/(._)" matches “/”, client:  
127.0.0.1, server: [www.domain.com](http://www.domain.com), request: “GET / HTTP/1.0”, host:  
“[www.domain.com](http://www.domain.com)”

…

2009/03/10 15:40:36 [notice] 15197#0: \*42 rewritten redirect:  
“[https://www.domain.com/](https://www.domain.com/)”, client: 127.0.0.1, server: [www.domain.com](http://www.domain.com),  
request: “GET / HTTP/1.0”, host: “[www.domain.com](http://www.domain.com)”

Igor S. wrote:

> On Tue, Mar 10, 2009 at 07:02:57PM +0100, Steve Z. wrote:
> 
> > 127.0.0.1 - main [09/Mar/2009:23:36:52 +0000] “GET /favicon.ico  
> > HTTP/1.1” 302 161 “-” “Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9b5)  
> > Gecko/2008041514 Firefox/3.0b5”
> 
> Could you create a debug log:
> 
> ./configure --with-debug …
> 
> nginx.conf:
> 
> error\_log /path/to/log debug;
