42

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) ?

oliverjkb
  • 554
  • 1
  • 4
  • 10

2 Answers2

51

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.

mat
  • 510
  • 5
  • 20
FLXN
  • 658
  • 7
  • 6
  • 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 '15 at 14:53
  • @ardukar so your solution was to use the sites-enabled folder? – Mark Stosberg Jul 23 '15 at 14:58
  • I'm a little confused right now... – FLXN Jul 23 '15 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 '15 at 11:05
7

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;
    ...
rubo77
  • 2,282
  • 3
  • 32
  • 63
  • 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_? – Peter Wippermann Feb 13 '20 at 13:21
  • 1
    If you would put a comment above, it would end up in the wrong file – rubo77 Feb 13 '20 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. – Dzamo Norton Apr 27 '20 at 06:54
  • To split by `location` instead of `server`, just change this in the `csplit` line – rubo77 Apr 27 '20 at 08:02
  • Doing that would write out orphaned `location` blocks that belong to no `server` without the `include` directives discussed in the top-voted answer? – Dzamo Norton Apr 27 '20 at 08:11
  • I referred more to the overall question title. In this particular case you need both answers, this and the one by @FLXN – rubo77 Apr 27 '20 at 08:21