I got my /etc/init.d/nginx init script from here: https://www.nginx.com/resources/wiki/start/topics/examples/redhatnginxinit/

in my /etc/nginx/nginx.conf, there's include /etc/nginx/conf.d/*.conf; which includes the config files of my website.

however, after /etc/init.d/nginx start, it seems nginx is not loading those config files, and it returns 502 on access.

on the other hand, start nginx with command nginx works just fine.

what's wrong here?

  • 1
    What OS are you using that a) still uses init scripts and b) didn't provide a service script for the package? – jordanm Jul 20 '17 at 17:08
  • @jordanm centos – wong2 Jul 21 '17 at 2:22
  • Why not installing nginx by using yum? – 030 Jul 23 '17 at 13:13
  • nginx was installed with yum – wong2 Jul 23 '17 at 16:04
  • Do you have selinux on that machine? If so, check and post related logs from /var/log/audit/audit.log. If you can - try setting setenforce 0 and relaunch daemon. If it works - then it's certanly selinux problem. Don't forget to re-enable selinux back! How are you running nginx, "by hand", as a root user? What is user of nginx worker process after you run it? Try launching init file as bash -x /etc/init.d/nginx start to see debug output. – Andrew Jul 24 '17 at 15:07

Looks like your problem with wrong init file. Usually nginx install it from RPM and no additional files required. In your link metioned, that it init file tested on CentOS 5, but you using CentOS 6, where it not tested. Try to remove this init file and reinstall nginx from RPM or repo, it should install good one init file.

If you don't want to do it, you could try to copy init file from there. It's copy of init file from installed on CentOS 6 nginx.


You're using sysvinit scripts, but Red Hat (assuming you use Red Hat / CentOS from the link) has moved on to systemd.



Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

ExecStartPre=/usr/sbin/nginx -t
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID


Once that's taken care of,

systemctl enable nginx.service && systemctl start nginx.service

Then don't worry about it because it will now start on boot. But make sure you've killed the nginx instance that you started manually before.

  • A systemd unit file comes with package when you do yum install nginx. – jordanm Jul 21 '17 at 3:36
  • @Benny I'm using Centos6, which is still using sysvinit – wong2 Jul 22 '17 at 11:20
  • Delete your init script, or move it aside and "yum reinstall nginx", it should provide you with a proper init script out of the box, there shouldn't be a need to create your own. – Alex Berry Jul 25 '17 at 14:32

Since CentOS 6 does not provide own nginx packages, it is important to understand where your package comes from. An output of nginx -V would give essential details.

My guess that HTTP 502 errors indicate the inability of nginx process to communicate with an application server. Nginx error_log would contain details on this condition. Running setenforce 0 before service nginx restart would help to understand if a problem is related to SELinux mechanism.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.