How to dynamically reload nginx config

6,178

Solution 1

If you can't use a post-receive hook, perhaps you can use inotify to watch for changes in the nginx configuration.

In this case, you would use incrond and incrontab to set up a watch on specified files and actions to take when those files change. Something like this in the incrontab:

/etc/nginx/nginx.conf IN_MODIFY /etc/init.d/nginx reload

Here's the man page for incrontab. You should be able to find other documentation and examples for using the inotify toolset that will fit your configuration.

Solution 2

As cjc said, inotify and service nginx reload are the commands you are looking for.

Check out this post from nixCraft which gives a great explanation on how to configure inotify.

Share:
6,178

Related videos on Youtube

Martijn
Author by

Martijn

Currently doing web development primarily (PHP, jQuery, JS, HTML, CSS, MySQL) Past experience with graphics effects (FilterMeister, C, C++), realtime audio (SynthMaker) and financial systems (Java, COBOL, PL/1, DB2, Oracle). Mostly interrested in finding new problems, then developing methods to solve them. Would like to do some Android development if I ever find the spare time for it.

Updated on September 18, 2022

Comments

  • Martijn
    Martijn over 1 year

    I'm currently trying to setup Nginx for a domain with production, testing and development stages. Each comes with it's own partial Nginx config file with rewrites.

    The production and testing stages are set up as branches from a Git repository and automatic reloading of these configurations in Git's post-receive hook. This all works just fine.

    The development stage I'd like to transmit using SFTP using Netbeans' "SFTP-on-save" as I don't need version control at this granularity and it's a bit more direct. However, here I have no such thing as git's post-receive hook to trigger Nginx to reload config upon upload.

    In Apache, .htaccess files would just load dynamically. Is there anything similar in Nginx and/or how could I simulate this? Ideally a solution that reloads the config files only and immediately when changed.

  • Martijn
    Martijn over 11 years
    Would it be possible to match for a wildcard? "Like /var/www/*/nginx.*.conf" or "nginx.*"?