48

My nginx default configuration file is becoming huge. I'd like to split it to smaller config files, each including only one, maximum 4 locations to each file, so that I can enable/disable them quickly.

Actual file looks like this:

server {
    listen 80 default_server;
    root /var/www/

    location /1 {
        config info...;
    }

    location /2 {
        config info....;
    }        
    location /abc {
        proxy_pass...;
    }

    location /xyz {
        fastcgi_pass....;
    }
    location /5678ab {
        config info...;
    }

    location /admin {
        config info....;
    }

now, if I want to split that up to have only a few locations in each file (locations belonging together), what would be a proper way to do it without causing chaos (like declaring root in each file, hence having weird path's that nginx tries to find files) ?

1

2 Answers 2

61

You are probably looking for Nginx's include function: http://nginx.org/en/docs/ngx_core_module.html#include

You can use it like this:

server {
  listen 80;
  server_name example.com;
  […]
  include conf/location.conf;
}

include also accepts wildcards so you could also write

include include/*.conf;

to include every *.conf file in the directory include.

5
  • I considered this already but skipped it, because it would mean to edit the files content, rather than just unlinking files in the sites-enabled folder.
    – oliverjkb
    Jul 23, 2015 at 14:53
  • @ardukar so your solution was to use the sites-enabled folder? Jul 23, 2015 at 14:58
  • I'm a little confused right now...
    – FLXN
    Jul 23, 2015 at 17:28
  • Sorry for answering so late! Seems like I didn't read the notification.. -.- I am using FLXN's solution already. But that doesn't make me happy. Since I am building a server for a smaller company, where the administration is done solely via Browser, i'd rather not want to make changes inside a file. If a service is deactivated via browser, I want the subfolder (say 'location') in nginx to be deactivated as well, hence unlinking the config file in the sites-enabled folder seemed to be the best idea.
    – oliverjkb
    Aug 12, 2015 at 11:05
  • Please beware of the priority of the wildcard. Related: serverfault.com/a/361163/345785
    – Artfaith
    Jul 15, 2023 at 19:30
8

You can create site folders with

mkdir -p /etc/nginx/sites-available /etc/nginx/sites-enabled

# And then split your large your_config.conf file into smaller files into sites-available/ with:

YOURCONF="/etc/nginx/conf.d/your_config.conf"
cd /etc/nginx
mkdir -p sites-available sites-enabled
cd  sites-available/
csplit "$YOURCONF" '/^\s*server\s*{*$/' {*}
for i in xx*; do
  new=$(grep -oPm1 '(?<=server_name).+(?=;)' $i|sed -e 's/\(\w\) /\1_/g'|xargs);
  if [[ -e $new.conf ]] ; then
    echo "" >>$new.conf
    cat "$i">>$new.conf
    rm "$i"
  else
    mv "$i" $new.conf
  fi
done

(I enhanced this from this source: https://stackoverflow.com/a/9635153/1069083 )

Be sure to add this at the end inside the http block of your/etc/nginx/conf.d/*.conf;:

include /etc/nginx/sites-enabled/*.conf; 

Note: comments outside the server blocks are cut into the bottom of each file, so there should be no comments BEFORE a server block. Move comments in the first line INSIDE the block instead, example:

 # don't put comments here
 server {
    # put your comments about domain xyz.org here
    listen 80;
    server_name xyz.org;
    ...
6
  • Thanks for answering so fast and adding the example, cool! :-) I understand what I should do and what I shouldn't do. But I don't get the reason. What do you mean with are cut into the bottom of each file? Feb 13, 2020 at 13:21
  • 1
    If you would put a comment above, it would end up in the wrong file
    – rubo77
    Feb 13, 2020 at 13:40
  • This moves entire server blocks into separate files which is nice but unhelpful to the OP who is trying move sets of location blocks within a single server block into separate files. Apr 27, 2020 at 6:54
  • To split by location instead of server, just change this in the csplit line
    – rubo77
    Apr 27, 2020 at 8:02
  • Doing that would write out orphaned location blocks that belong to no server without the include directives discussed in the top-voted answer? Apr 27, 2020 at 8:11

You must log in to answer this question.

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