# Nginx redirect http to https



## fahad (Oct 27, 2013)

I want to redirect all http request to https of a site in nginx . How to do that ?


----------



## NodeBytes (Oct 27, 2013)

http://serverfault.com/questions/67316/in-nginx-how-can-i-rewrite-all-http-requests-to-https-while-maintaining-sub-dom

and

http://www.cyberciti.biz/faq/linux-unix-nginx-redirect-all-http-to-https/


----------



## fahad (Oct 27, 2013)

Not workable.

The Workable one is :

http://z.notrace.pw/1agUHUg


----------



## Raymii (Oct 27, 2013)

Using if's in NGINX config is not good: http://wiki.nginx.org/Pitfalls#Taxing_Rewrites
 


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


----------



## 5n1p (Oct 27, 2013)

you can also have two servers defined in vhost cfg:


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

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


----------



## Sunshine (Oct 27, 2013)

Raymii said:


> Using if's in NGINX config is not good: http://wiki.nginx.org/Pitfalls#Taxing_Rewrites
> 
> 
> server {
> ...


This is actually the best answer. You should avoid if's if you can 

Also, see this: http://wiki.nginx.org/IfIsEvil

But other than that, you did the right thing in coming back and posting the solution that worked for you. I wish everybody did that.


----------



## betatester (Oct 27, 2013)

Sunshine said:


> But other than that, you did the right thing in coming back and posting the solution that worked for you. I wish everybody did that.


Can't agree more. As I start my journey with a simple LEB, you guys are a load of help.


----------



## tchen (Oct 27, 2013)

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 (Oct 27, 2013)

tchen said:


> 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 (Oct 27, 2013)

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 (Oct 27, 2013)

tchen said:


> 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.
> 
> ...


*I marked which worked for me . Thats all.*


----------



## budi1413 (Nov 16, 2013)

Rewrite or redirect better? For now i'm using redirect.


----------



## tchen (Nov 16, 2013)

budi1413 said:


> 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. (Nov 16, 2013)

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:


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


----------

