SoFunction
Updated on 2025-03-04

Detailed explanation of the method of nginx specifying the path of the conf file

When using Nginx, you sometimes need to specify the path to the configuration file() instead of using the default path. You can specify the configuration file path in the following ways:

1. Use the -c or --conf-path option

When starting Nginx, you can use the -c or --conf-path option to specify the path to the configuration file. For example:

nginx -c /path/to/your/

or

nginx --conf-path=/path/to/your/

2. Use environment variables

You can also specify the path to the configuration file by setting the environment variable NGINX_CONF_PATH. For example, before starting Nginx, you can set environment variables:

export NGINX_CONF_PATH=/path/to/your/
nginx

3. Modify system service files (Nginx for system service management)

If you are managing Nginx through system services (such as systemd or ), you can modify the corresponding service file to specify the configuration file path.

Nginx managed by systemd

Edit the /etc/systemd/system/ file, find the ExecStart line, and modify it to:

ExecStart=/usr/sbin/nginx -c /path/to/your/

Then reload the systemd configuration and restart Nginx:

sudo systemctl daemon-reload
sudo systemctl restart nginx

For managed Nginx

Edit the /etc//nginx file, find the DAEMON line, and modify it to:

DAEMON=/usr/sbin/nginx -c /path/to/your/

Then restart Nginx:

sudo service nginx restart

4. Modify the default configuration file

If you want to permanently change the default profile path of Nginx, you can modify the default profile path of Nginx. This usually involves modifying Nginx's installation script or system configuration file.

Modify the installation script

If you are compiling Nginx installed through source code, you can modify the NGX_CONF_PATH variable in the objs/Makefile file, or specify the default configuration file path through the --conf-path option at compile time.

Modify system configuration files

On some systems, the default configuration file path for Nginx may be stored in the /etc/default/nginx or /etc/sysconfig/nginx file. You can edit these files and modify the NGINX_CONF_PATH variable to specify the default configuration file path.

This is the introduction to this article about the detailed explanation of the method of nginx specifying the conf file path. For more related contents of nginx specifying the conf file path, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!