amuck-landowner

Nginx redirect http to https

5n1p

New Member
you can also have two servers defined in vhost cfg:

Code:
server {
listen 80;
server_name domain.com;
return 301 http://www.domain.com$request_uri;
}

server {
listen 80;
server_name www.domain.com;
...
}
 

Sunshine

New Member
Last edited by a moderator:

tchen

New Member
I'm perplexed as to why the best answer is your own blog post. And don't say the posts above yours didn't work as the extra 'scheme' check you out in the post does nothing. Is this a new form of rep back linking?
 

fahad

Member
I'm perplexed as to why the best answer is your own blog post. And don't say the posts above yours didn't work as the extra 'scheme' check you out in the post does nothing. Is this a new form of rep back linking?
Because that works , I was searching for the answer , I got that , posted that in my blog and i shared it here. Just it. I don't need linkbacks . If any admin/moderator has some question about my act too then i will just remove it.


But actually i thought it will be helpful for the new searches .
 

tchen

New Member
So just for reference to anyone else searching... your own-post has several issues (pointed out by everyone else on this board):

1)  Uses "if" in a nginx config where it really doesn't even need it.  Your $scheme will always be 'http' give its location in the :80 server config.

2)  Your use of a heafty regex capture goes against almost all known examples. 

5n1p's answer of

return 301 http://www.domain.com$request_uri;
is the canonical answer form as it avoids regex altogether and uses the builtin request string.  Raymii's answer is still usable and better than yours - and Sunshine went so far as to educate you why ifs are bad.

You're more than within your rights to select yourself as the 'best answer', but there's something odd about it no matter your reasoning, especially when its so far off the mark.
 

fahad

Member
So just for reference to anyone else searching... your own-post has several issues (pointed out by everyone else on this board):

1)  Uses "if" in a nginx config where it really doesn't even need it.  Your $scheme will always be 'http' give its location in the :80 server config.

2)  Your use of a heafty regex capture goes against almost all known examples. 

5n1p's answer of

is the canonical answer form as it avoids regex altogether and uses the builtin request string.  Raymii's answer is still usable and better than yours - and Sunshine went so far as to educate you why ifs are bad.

You're more than within your rights to select yourself as the 'best answer', but there's something odd about it no matter your reasoning, especially when its so far off the mark.
I marked which worked for me . Thats all.
 

tchen

New Member
Rewrite or redirect better? For now i'm using redirect.
Both 'return 301' and 'rewrite .... permanent' in the above examples will barf out the same 301 redirect http code.  The only difference between the two is that the 'return' examples don't need the extra regex.

note: if you use rewrite without either permanent (301) or redirect (302) flags, then it behaves as a server-side rewrite.  Same words, totally different meaning.
 

Marc M.

Phoenix VPS
Verified Provider
Here is how to do this the right way:


server {
listen 192.168.0.1:80;
server_name www.example.com;
return 301 https://www.example.com$request_uri;
}

... and then of course you can create another server {} block within the same config file for the https version:

Code:
server {
        listen 192.168.0.1:443 ssl spdy;
        server_name www.example.com;
        .
        .
        .
        [SSL Certtificate Code]
        .
        .
        .
}
 
Top
amuck-landowner