sorin
sorin

Reputation: 170658

Is it possible to use conditions in nginx config in order to have same config file for test and production?

I want to use the same config nginx site config file for test and change only the servername and port.

How can I achieve that or something close to it?

I want something like: if hostname==xxx listen on 80 else listen on 81.

Upvotes: 0

Views: 542

Answers (2)

Gnarfoz
Gnarfoz

Reputation: 3236

You could put all the shared parts in a separate file (say, common.conf) and include that in two minimalistic server blocks, like this:

http {
    [...]
    server {
        listen 80;
        server_name production.local;

        root /some/path/production;
        include common.conf;
    }

    server {
        listen 81;
        server_name testing.local;

        root /some/path/testing;
        include common.conf;
    }
}

Edit Actually, you don't even need the listen directives. They default to listen 80 when running as root, and to listen 8000 otherwise. As long as you have a DNS entry, or hosts file entry, you'd be good.

Upvotes: 2

VBart
VBart

Reputation: 15110

You should use bash/sh. Unix way.

Upvotes: -1

Related Questions