Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Typically my nginx config looks like this:

server {
    listen       80;
    server_name  example.com;

    <some long config>
}

server {
    listen       443;
    server_name  example.com;

    ssl                 on;
    ssl_certificate     qwe.crt;
    ssl_certificate_key qwe.key;

    <the same long config>
}

How to forward all https requests to http server directive (not send redirect to client but forward within nginx) so that I will not need to write the same config twice?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

Found solution:

server {
    listen              80;
    listen              443 ssl;
    server_name         www.example.com;
    ssl_certificate     www.example.com.crt;
    ssl_certificate_key www.example.com.key;

    <some long config>
}
share|improve this answer
    
Whilst this will serve the same stuff to both http and https, does it actually redirect the client from https over to http? –  John Mee Jul 3 '13 at 6:29
    
@JohnMee the point is not to redirect user but to serve same content. This config does exactly that. –  Poma Jul 11 '13 at 8:38
add comment

You simply have two server directives...

  • One that listens on port 80 and does a rewrite
  • And another that listens on port 443 and is your main config

Of course you can do this the other way around (https redirect to http) it makes no difference.

In Nginx, how can I rewrite all http requests to https while maintaining sub-domain?

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

server {
       listen         443;
       server_name    my.domain.com;

       ssl            on;

       [....]
}
share|improve this answer
    
Looks like rewrite directive will send 301 redirect to user. I don't want to send any redirects to user. –  Poma Apr 4 '13 at 12:28
    
Oh sorry, I didn't read that bit. Typically I'm used to doing redirections as it's best for SEO purposes to have only one site. If you respond with 200's on multiple sites (ie http & https) this could be seen as duplicate content. Make sure you're aware of the implications. –  Drew Khoury Apr 4 '13 at 12:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.