others-How to solve 'nginx: [emerg] master_process directive is duplicate in /etc/nginx/nginx.conf' error when trying to start nginx in debug mode?

1. Purpose

In this post, I would demonstrate how to solve the following error when trying to start nginx in debug mode:

Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: Starting A high performance web server and a reverse proxy server...
-- Subject: Unit nginx.service has begun start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit nginx.service has begun starting up.
Jan 11 15:50:14 launch-advisor-20191120 nginx[5282]: nginx: [emerg] "master_process" directive is duplicate in /etc/nginx/nginx.conf:7
Jan 11 15:50:14 launch-advisor-20191120 nginx[5282]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: nginx.service: Control process exited, code=exited status=1
Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: nginx.service: Failed with result 'exit-code'.
Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit nginx.service has failed.
--
-- The result is RESULT.



2. The problem and solution

2.1 How to cause the error?

I want to start nginx in debug mode to have some tests, but when I try to add this line to nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

master_process off;          ### here is the line I added to start nginx in debug mode

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    ....

Then I start nginx:

systemctl start nginx

I got this error:

Jan 11 15:50:14 launch-advisor-20191120 nginx[5282]: nginx: [emerg] "master_process" directive is duplicate in /etc/nginx/nginx.conf:7
Jan 11 15:50:14 launch-advisor-20191120 nginx[5282]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: nginx.service: Control process exited, code=exited status=1
Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: nginx.service: Failed with result 'exit-code'.
Jan 11 15:50:14 launch-advisor-20191120 systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit nginx.service has failed.
--
-- The result is RESULT.


The core error message is:

Jan 11 15:50:14 launch-advisor-20191120 nginx[5282]: nginx: [emerg] "master_process" directive is duplicate in /etc/nginx/nginx.conf:7
Jan 11 15:50:14 launch-advisor-20191120 nginx[5282]: nginx: configuration file /etc/nginx/nginx.conf test failed


2.2 How to debug the error?

First we should check the status of our service:

root@launch-advisor-20191120:/etc/nginx# systemctl status nginx.service

We got this:

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2022-01-11 16:05:46 CST; 7s ago
     Docs: man:nginx(8)
  Process: 5203 ExecStop=/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid (code=exited, status=0/SUCCESS)
  Process: 5016 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 5354 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 5018 (code=exited, status=0/SUCCESS)

Jan 11 16:05:46 launch-advisor-20191120 systemd[1]: Starting A high performance web server and a reverse proxy server...
Jan 11 16:05:46 launch-advisor-20191120 nginx[5354]: nginx: [emerg] "master_process" directive is duplicate in /etc/nginx/nginx.conf:7
Jan 11 16:05:46 launch-advisor-20191120 nginx[5354]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jan 11 16:05:46 launch-advisor-20191120 systemd[1]: nginx.service: Control process exited, code=exited status=1
Jan 11 16:05:46 launch-advisor-20191120 systemd[1]: nginx.service: Failed with result 'exit-code'.
Jan 11 16:05:46 launch-advisor-20191120 systemd[1]: Failed to start A high performance web server and a reverse proxy server.

Let’s view the nginx.service file content: /lib/systemd/system/nginx.service

[Unit]
Description=A high performance web server and a reverse proxy server
Documentation=man:nginx(8)
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target
~

So the reason to cause ‘nginx: [emerg] “master_process” directive is duplicate in /etc/nginx/nginx.conf’ error is apparent: It’s because that the nginx.service already specified the master_process option in its service file. So if we added another master_process off in nginx.conf, it’s really duplicated.


2.3 How to solve the error?

Now after analysis, we have known the reason , now let’s solve it.

We should avoid adding duplicated master_process off in the nginx.conf, instead, we should add it in command line, we can start nginx in foreground in command line as follows:

nginx -g 'daemon off; master_process off; '

Now verify that only one master process exists in system:

root@launch-advisor-20191120:~# ps -ef|grep nginx
root      5547  4232  0 16:40 pts/0    00:00:00 nginx -g daemon off; master_process off; error_log /dev/stdout debug;
root      5554  5442  0 16:41 pts/1    00:00:00 grep --color=auto nginx
root@launch-advisor-20191120:~#

You can see that the master_process off works! There is only one nginx process in system, and there are no worker processes in system.



3. Summary

In this post, I demonstrated how to solve the nginx: [emerg] "master_process" directive is duplicate in /etc/nginx/nginx.conf error , the key point is to start the nginx in foreground and specify the master_process off , otherwise, your option would be conflicted with the system service command line options . That’s it, thanks for your reading.