Nginx – cannot change nginx error log file name

nginx

Is there a way to change the error log file name. Whatever I am doing, I always get the error logs written to the default file name "error.log" when a request gets processed. I have a global and only one error log configuration in main context:

user  npcache;
worker_processes  2;

error_log /opt/logs/nginx/error1.log notice;
pid       /opt/logs/nginx/nginx.pid;

events {
  worker_connections  1024;
  multi_accept on;
}

http {
  error_log  /opt/logs/nginx/error.log notice;
  include ./mime.types;
  default_type application/octet-stream;

  #Access log configuration
  log_format main '"$remote_addr" "$host" "$http_host" ["$time_local"]'
                  '"$request" $status $body_bytes_sent "$http_referer"';
  access_log /opt/logs/nginx/access.log main;
. . .
}

With this config I am getting error logs written to /opt/logs/nginx/error.log instead of "error1.log".

Am I missing something in the configuration?

I discovered that the issue was related to the second error log in http context that was eclipsing the top-level one.

Thanks

Best Answer

You need to post something like this in your nginx.conf, which is probably in /etc/nginx/nginx.conf. It goes at the top of the file, outside the events and http blocks. You can also put it in at the server or location level. Documentation here.

error_log  /var/log/nginx/error.log warn;

Nginx documentation is excellent, and many queries have been answered on Server Fault and online already. Sometimes a search can save you time having to wait for people to answer your question.