Is there a way to share configuration directives across two nginx server {} blocks? I'd like to avoid duplicating the rules, as my site's HTTPS and HTTP content are served with the exact same config.

Currently, it's like this:

server {
  listen 80;
  ...
}

server {
  listen 443;

  ssl on; # etc.
  ...
}

Can I do something along the lines of:

server {
  listen 80, 443;
  ...

  if(port == 443) {
    ssl on; #etc
  }
}
link|improve this question

feedback

3 Answers

up vote 29 down vote accepted

Per Igor,

You can combine this into one server block like so:

server {
    listen 80;
    listen 443 default_server ssl;

    # other directives
}

Post showing from Igor

link|improve this answer
Ah, I had no idea nginx was intelligent enough to ignore the SSL directives if loaded over port 80. Awesome! – ceejayoz May 21 '09 at 19:34
8  
nginx is all sorts of WIN. – Jauder Ho May 21 '09 at 19:38
1  
and if you have several sites at one server, it's worth mentioning that "default" is not obligatory – how Jul 1 '11 at 15:32
feedback

I don't know of a way like you suggest, but there's certainly an easy and maintainable way:

server {
    listen 80;
    include serverFoo.conf;
}
server {
    listen 443;
    ssl on;
    include serverFoo.conf;
}
link|improve this answer
1  
+1 = works for me. (Couldn't get it to work with the other method.) – lackey Jul 3 '09 at 15:44
feedback

Just to add to Igor/Jauder's post, if you're listening to a specific IP you can use:

listen xxx.xxx.xxx.xxx;
listen xxx.xxx.xxx.xxx:443 default ssl;
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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