1

I am still newbie configuring nginx.

Is it posibble to configure multiple domain in one file and they share mostly the same config?
For example I want to configure two domains that based from one app and one domain need basic auth, the other doesn't.
I would like to do something like this, but I think this does not work:

sites-enabled/mysite

server {

    listen 127.0.0.1:80 default_server;
    server_name www.mysite.com;
    include sharedconf.conf;
}


server {
    listen 127.0.0.1:80;
    server_name www.mysite.co.jp;
    auth_basic "restricted";
    auth_basic_user_file /etc/nginx.htpasswd;
    include sharedconf.conf;
}


sharedconf.conf

location / {
    proxy_pass_header Server;
    #... bunch of config line ...

}
1
  • The basic logic behind 2 vhosts in one file and both sharing an 'includes' sounds fine. Can you elaborate on what happens you try this configuration?
    – Peter
    Aug 22, 2014 at 14:14

1 Answer 1

1

Yes, this works fine.

I am assuming that you are using Debian or Debian derived Linux distribution. Then you cannot put sharedconf.conf into /etc/nginx/sites-enabled nor into /etc/nginx/conf.d, since files in those directories are included in main nginx.conf on Debian.

2
  • Yep, create directory like /etc/nginx/shared/ and include it via full-path. It should work without any problems. Just check permissions on directory and files
    – Navern
    Aug 23, 2014 at 23:14
  • 1
    There is a ready-made /etc/nginx/snippets, exactly for this. I don't know whether it was already existed back in 2014. Aug 3, 2023 at 5:08

You must log in to answer this question.

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