- Locate the nginx.conf file my nginx is actually using
- 9 Answers 9
- Creating NGINX Plus and NGINX Configuration Files
- Directives
- Feature-Specific Configuration Files
- Contexts
- Virtual Servers
- Sample Configuration File with Multiple Contexts
- Inheritance
- Reloading Configuration
- how can i change nginx Configuration file path
- 1 Answer 1
Locate the nginx.conf file my nginx is actually using
Working on a client’s server where there are two different versions of nginx installed. I think one of them was installed with the brew package manager (its an osx box) and the other seems to have been compiled and installed with the nginx packaged Makefile. I searched for all of the nginx.conf files on the server, but none of these files define the parameters that nginx is actually using when I start it on the server. Where is the nginx.conf file that I’m unaware of?
9 Answers 9
Running nginx -t through your commandline will issue out a test and append the output with the filepath to the configuration file (with either an error or success message).
Both nginx -t and nginx -V would print out the default nginx config file path.
$ nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful $ nginx -V nginx version: nginx/1.11.1 built by gcc 4.9.2 (Debian 4.9.2-10) built with OpenSSL 1.0.1k 8 Jan 2015 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf .
If you want, you can get the config file by:
$ nginx -V 2>&1 | grep -o '\-\-conf-path=\(.*conf\)' | cut -d '=' -f2 /etc/nginx/nginx.conf
Even if you have loaded some other config file, they would still print out the default value.
ps aux would show you the current loaded nginx config file.
$ ps aux USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 11 0.0 0.2 31720 2212 ? Ss Jul23 0:00 nginx: master process nginx -c /app/nginx.conf
So that you could actually get the config file by for example:
$ ps aux | grep "[c]onf" | awk '' /app/nginx.conf
% ps -o args -C nginx COMMAND build/sbin/nginx -c ../test.conf
If nginx was run without the -c option, then you can use the -V option to find out the configure arguments that were set to non-standard values. Among them the most interesting for you are:
--prefix=PATH set installation prefix --sbin-path=PATH set nginx binary pathname --conf-path=PATH set nginx.conf pathname
will give you the path of the nginx being used
EDIT (2017-Jan-18)
Thanks to Will Palmer’s comment on this answer, I have added the following.
If you’ve installed nginx via a package manager such as HomeBrew.
may not give you the EXACT path to the nginx being used. You can however find it using
and as mentioned by @Daniel Li
you can get configuration of nginx via his method
alternatively you can use this:
«which» works on most Unix based systems. I just typed it on Ubuntu to make sure I hadn’t lost my mind.
which nginx only shows the default path for nginx for the current user (not even the current user — the current shell). It definitely does not show the path for which nginx «is being used».
All other answers are useful but they may not help you in case nginx is not on PATH so you’re getting command not found when trying to run nginx :
I have nginx 1.2.1 on Debian 7 Wheezy, the nginx executable is not on PATH , so I needed to locate it first. It was already running, so using ps aux | grep nginx I have found out that it’s located on /usr/sbin/nginx , therefore I needed to run /usr/sbin/nginx -t .
If you want to use a non-default configuration file (i.e. not /etc/nginx/nginx.conf ), run it with the -c parameter: /usr/sbin/nginx -c -t .
You may also need to run it as root , otherwise nginx may not have permissions to open for example logs, so the command would fail.
Creating NGINX Plus and NGINX Configuration Files
Understand the basic elements in an NGINX or NGINX Plus configuration file, including directives and contexts.
NGINX and NGINX Plus are similar to other services in that they use a text‑based configuration file written in a particular format. By default the file is named nginx.conf and for NGINX Plus is placed in the /etc/nginx directory. (For NGINX Open Source , the location depends on the package system used to install NGINX and the operating system. It is typically one of /usr/local/nginx/conf , /etc/nginx , or /usr/local/etc/nginx .)
Directives
The configuration file consists of directives and their parameters. Simple (single‑line) directives each end with a semicolon. Other directives act as “containers” that group together related directives, enclosing them in curly braces ( <> ); these are often referred to as blocks. Here are some examples of simple directives.
user nobody; error_log logs/error.log notice; worker_processes 1;
Feature-Specific Configuration Files
To make the configuration easier to maintain, we recommend that you split it into a set of feature‑specific files stored in the /etc/nginx/conf.d directory and use the include directive in the main nginx.conf file to reference the contents of the feature‑specific files.
include conf.d/http; include conf.d/stream; include conf.d/exchange-enhanced;
Contexts
A few top‑level directives, referred to as contexts, group together the directives that apply to different traffic types:
- events – General connection processing
- http – HTTP traffic
- mail – Mail traffic
- stream – TCP and UDP traffic
Directives placed outside of these contexts are said to be in the main context.
Virtual Servers
In each of the traffic‑handling contexts, you include one or more server blocks to define virtual servers that control the processing of requests. The directives you can include within a server context vary depending on the traffic type.
For HTTP traffic (the http context), each server directive controls the processing of requests for resources at particular domains or IP addresses. One or more location contexts in a server context define how to process specific sets of URIs.
For mail and TCP/UDP traffic (the mail and stream contexts) the server directives each control the processing of traffic arriving at a particular TCP port or UNIX socket.
Sample Configuration File with Multiple Contexts
The following configuration illustrates the use of contexts.
user nobody; # a directive in the 'main' context events < # configuration of connection processing > http < # Configuration specific to HTTP and affecting all virtual servers server < # configuration of HTTP virtual server 1 location /one < # configuration for processing URIs starting with '/one' > location /two < # configuration for processing URIs starting with '/two' > > server < # configuration of HTTP virtual server 2 > > stream < # Configuration specific to TCP/UDP and affecting all virtual servers server < # configuration of TCP virtual server 1 > >
Inheritance
In general, a child context – one contained within another context (its parent) – inherits the settings of directives included at the parent level. Some directives can appear in multiple contexts, in which case you can override the setting inherited from the parent by including the directive in the child context. For an example, see the proxy_set_header directive.
Reloading Configuration
For changes to the configuration file to take effect, it must be reloaded. You can either restart the nginx process or send the reload signal to upgrade the configuration without interrupting the processing of current requests. For details, see Controlling NGINX Processes at Runtime.
With NGINX Plus, you can dynamically reconfigure load balancing across the servers in an upstream group without reloading the configuration. You can also use the NGINX Plus API and key‑value store to dynamically control access, for example based on client IP address.
how can i change nginx Configuration file path
nginx_web_agent use that nginx.conf file, How can i change nginx default nginx.conf file to nginx_web_agent nginx.conf file for example: nginx Configure use /opt/nginx_agent/conf/nginx.conf instead of /etc/nginx/nginx.conf Suggest me How can i do that?
1 Answer 1
/usr/sbin/nginx -V shows the initial configure script parameters, not necessarily the actual parameters that are running.
To use an alternative configuration file, instead on the default one, you can set the -c flag (man nginx) :
/usr/sbin/nginx -c /opt/nginx_agent/conf/nginx.conf
Ubuntu 16.04 uses systemd to manage services, so you will need to change systemd parameters for nginx service :
- Edit /lib/systemd/system/nginx.service
- Add -c flag where required :
ExecStartPre=/usr/sbin/nginx -t -c /opt/nginx_agent/conf/nginx.conf -q -g 'daemon on; master_process on;' ExecStart=/usr/sbin/nginx -c /opt/nginx_agent/conf/nginx.conf -g 'daemon on; master_process on;' ExecReload=/usr/sbin/nginx -c /opt/nginx_agent/conf/nginx.conf -g 'daemon on; master_process on;' -s reload
systemctl status nginx.service . 2411 nginx: master process /usr/sbin/nginx -c /opt/nginx_agent/conf/nginx.conf -g daemon on; master_process on .
This is the way i would do it.