大网数据广告活动
服务器运维问答

What should be the right logs permissions for NGINX on CentOS?

· 52CE 我爱测

on /var/log/nginx/ I have access and error logs. Some are .log errors and others have .gz I think I messed up with the permissions. Some files are created by root/root , other files are created using the user configured in nginx.…

问题原文

原帖链接 · Pikk · CC BY-SA 3.0

on /var/log/nginx/ I have access and error logs. Some are .log errors and others have .gz

I think I messed up with the permissions. Some files are created by root/root, other files are created using the user configured in nginx.conf, etc.

  • What user/group should create those files? And why? Can it be root? I can see that now new .log files are being created by root, but not sure if it is the right way.
  • And also I'd like to ask where to change who is the user that creates those files?

已采纳解答

原帖链接 · eschipul · CC BY-SA 4.0

On the files located in /var/log/nginx/ the rules have changed over the time (at least in my experience). Yet without more data I'm not comfortable giving a definitive suggestion. But I'll try.

NGINX itself runs as "root" NGINX processes run as the user specified in /etc/nginx/nginx.conf which is usually "www-data" (hang on, keep reading)

You can use this command to determine how your NGINX is running:

ps -eo "%U %G %a" | grep nginx

Your output should look something like this:

root     root     nginx: master process /usr/sbin/nginx -g daemon on; master_process on;

www-data www-data nginx: worker process

What you did not say is WHY you are asking this question. Hypothetically, let's just say you are running OSSEC with an ELK stack. And if you are on Ubuntu 16.04, then your second issue is what processes can READ the files as well as write. If you are using logrotate then you would need to edit the logrotate files as well as change permissions on the log folder.

Step 1 - in logrotate update the nginx file:

nano /etc/logrotate.d/nginx

In that file, on recent versions of nginx and ubuntu, I have found that changing the ownership line as follows will allow it to work. This is in the file in /etc/logrotate/nginx which sets the file permissions when the file is rotated.

create 0640 nginx nginx

or alternatively:

create 0640 www-data www-data

Although in practice nginx:nginx has worked more consistently and is a balance between giving the nginx web process permissions to the log files vs setting them to root.

To continue, for your legacy log files (e.g. to be read by logstash) you may want to reset permissions (assuming default file locations which I realize most of us don't actually use.)

chown nginx:nginx /var/log/nginx/*

or (again) alternatively

chown www-data:www-data /var/log/nginx/*

Hopefully this helps. I have found data on the proper permissions for nginx to be confusing at best when it comes to logging, which I attribute to the evolution of their AMAZING software. And I welcome others who know more to provide feedback on my reply.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-05-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。