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
}
}
You can combine this into one server block like so:
Official How-To
To clarify the accepted answer, you need to omit
and you just need the following for nginx version after 0.8.21
Reference:
Nginx Docs - Configuring A single HTTP/HTTPS server
I don't know of a way like you suggest, but there's certainly an easy and maintainable way.
Move common server settings into a separate file, i.e. "serverFoo.conf" and then
include
it in separateserver {}
blocks like so:Expanding on the already helpful answers, here is a more complete example:
Just to add to Igor/Jauder's post, if you're listening to a specific IP you can use: