1

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?

peterh
  • 4,953
  • 13
  • 30
  • 44
Poma
  • 1,299
  • 6
  • 24
  • 35

2 Answers2

0

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>
}
Poma
  • 1,299
  • 6
  • 24
  • 35
  • 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 03 '13 at 06:29
  • @JohnMee the point is *not* to redirect user but to serve same content. This config does exactly that. – Poma Jul 11 '13 at 08:38
0

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;

       [....]
}
Drew Khoury
  • 4,637
  • 8
  • 27
  • 28
  • Looks like rewrite directive will send 301 redirect to user. I don't want to send any redirects to user. – Poma Apr 04 '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 04 '13 at 12:52