How to Change Nginx Configuration File Path to Custom Location in Ubuntu 16.04


2 views

When you install Nginx on Ubuntu 16.04, the default configuration file path is typically /etc/nginx/nginx.conf. You can verify this by running:

nginx -V 2>&1 | grep --colour=auto conf-path

This will output something like:

--conf-path=/etc/nginx/nginx.conf

In your case, you're working with the OpenAM Nginx WebAgent which comes with its own nginx.conf file located at /opt/nginx_agent/conf/nginx.conf. There are several valid reasons to change the configuration path:

  • To maintain separate configurations for different environments
  • When using third-party tools that provide their own configuration files
  • For better organization of configuration files

The most thorough way to change the configuration path is to recompile Nginx with the new path:

./configure --conf-path=/opt/nginx_agent/conf/nginx.conf
make
sudo make install

However, this isn't always practical in production environments.

A simpler approach is to specify the configuration file when starting Nginx:

sudo nginx -c /opt/nginx_agent/conf/nginx.conf

To make this permanent, modify your systemd service file:

sudo systemctl edit --full nginx

Then find the ExecStart line and modify it to:

ExecStart=/usr/sbin/nginx -c /opt/nginx_agent/conf/nginx.conf

For a quick solution without modifying service files:

sudo mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
sudo ln -s /opt/nginx_agent/conf/nginx.conf /etc/nginx/nginx.conf

After making changes, verify the active configuration path:

sudo nginx -T 2>&1 | head -n 1

This should show the new configuration file path being used.

  • Always back up your original configuration file
  • Check file permissions on the new configuration path
  • Ensure SELinux contexts are correct if using SELinux
  • Test configuration before applying to production

If you encounter issues:

sudo nginx -t -c /opt/nginx_agent/conf/nginx.conf

This will test the configuration file for syntax errors.


When you install Nginx on Ubuntu 16.04, the default configuration file is typically located at /etc/nginx/nginx.conf. You can verify this using:

$ /usr/sbin/nginx -V 2>&1 | grep --colour=auto conf
--conf-path=/etc/nginx/nginx.conf

The OpenAM nginx Web Agent (available at GitHub) comes with its own configuration file located at:

/opt/nginx_agent/conf/nginx.conf

This configuration contains specialized directives for OpenAM integration that won't work properly if simply copied into the default location.

There are three primary methods to make Nginx use the Web Agent's configuration:

Method 1: Recompiling Nginx

The most permanent solution is to recompile Nginx with the new configuration path:

./configure --conf-path=/opt/nginx_agent/conf/nginx.conf [other existing flags]
make
sudo make install

You can find your existing compilation flags using:

nginx -V

Method 2: Using Command Line Argument

For temporary testing or development:

sudo nginx -c /opt/nginx_agent/conf/nginx.conf

This will start Nginx with the specified configuration file.

Method 3: Systemd Service Modification

For Ubuntu 16.04 using systemd:

sudo systemctl edit --full nginx

Then modify the ExecStart line to include the -c parameter:

ExecStart=/usr/sbin/nginx -c /opt/nginx_agent/conf/nginx.conf -g 'daemon on; master_process on;'

After making changes, verify the configuration syntax and restart Nginx:

sudo nginx -t -c /opt/nginx_agent/conf/nginx.conf
sudo systemctl restart nginx