2

I'm trying to enable SSL on my current Nginx configuration, which works fine. However I'm wondering if it's possible to do this alongside HTTP, so that i do not need another server{} section which would just be a replication of the http section.

I thought the following would work, however i get the below when accessing http://

400 Bad Request

The plain HTTP request was sent to HTTPS port

Nginx Config:

  ssl_certificate      /etc/nginx/ssl/domains.pem;
  ssl_certificate_key  /etc/nginx/ssl/server.key;

server {
        listen 80;
        listen 443;
    //other configuration
}
2
  • Do any of your users actually need unencrypted connections? If they don't you can just redirect from 80 to 443 and get added security in the process.
    – 84104
    Oct 21, 2013 at 23:46
  • I didn't think of that, but at this point, we would need plain HTTP.
    – bhavicp
    Oct 21, 2013 at 23:59

2 Answers 2

3

You can have multiple listen directives per server directive.

http {
 server {
  listen 80;
  listen 443 default ssl;

  <config stuff including ssl>
 }
}
1
  • Thanks. Been trying to post my own answer for the past hour, but need to wait another 3 hours to do it! Got this helpful advice from the folk at #nginx in freenode. I added listen 443 ssl;
    – bhavicp
    Oct 22, 2013 at 4:12
1

I do something similar with include, such that the ssl related config lines are only applied to one of the listeners.

http {
 server {
  listen 443;
  ssl on;
  ssl_certificate     /etc/nginx/ssl_keys/server.crt ;
  ssl_certificate_key /etc/nginx/ssl_keys/server.key ;
  include otherstuff.conf;
  }

 server {
  listen 80;
  include otherstuff.conf;
  }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .