AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1549120
Accepted
Leos Literak
Leos Literak
Asked: 2020-05-06 23:31:58 +0800 CST2020-05-06 23:31:58 +0800 CST 2020-05-06 23:31:58 +0800 CST

Nginx 创建由 root 而不是指定用户拥有的日志文件

  • 772

我正在运行 CentOS 7.7,我想在具有自定义 www 和日志位置的自定义用户下运行 Nginx。我已经点击了 SELinux,所以我启用了我的自定义目录:

semanage permissive -a httpd_t # temporarily make SELinux permissive
semanage fcontext -a -t httpd_sys_content_t  '/opt/x/data/nginx(/.*)?'
restorecon -R -v /opt/x/data/nginx
semanage fcontext -a -t  httpd_log_t '/opt/x/log/nginx(/.*)?'
restorecon -R -v /opt/x/log/nginx

这是我的 nginx.conf:

user Xnginx Xgrp;
worker_processes auto;
error_log /opt/x/log/nginx/error.log;
pid /run/nginx.pid;

和目录权限:

ls -lZ /opt/X/log/
drwxrwxr-x. Xnginx Xgrp unconfined_u:object_r:httpd_log_t:s0 nginx

我开始 Nginx

systemctl start nginx

它给了

[root@RE1 nginx]# systemctl status nginx
nginx.service - The nginx HTTP and reverse proxy server
  Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
  Active: active (running) since Wed 2020-05-06 09:15:06 CEST; 12min ago
  Process: 109185 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 109182 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 109180 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
  Main PID: 109187 (nginx)
   CGroup: /system.slice/nginx.service
       ├─109187 nginx: master process /usr/sbin/nginx
       ├─109188 nginx: worker process
       ├─109189 nginx: worker process
       ├─109190 nginx: worker process
       └─109191 nginx: worker process

May 06 09:15:06 RE1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
May 06 09:15:06 RE1 nginx[109182]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
May 06 09:15:06 RE1 nginx[109182]: nginx: configuration file /etc/nginx/nginx.conf test is successful
May 06 09:15:06 RE1 systemd[1]: Started The nginx HTTP and reverse proxy server.

我可以看到子进程在用户下运行,而主进程以 root 身份运行

1     0 109187      1  20   0 131148  2268 sigsus Ss   ?          0:00 nginx: master process /usr/sbin/nginx
5   603 109188 109187  20   0 133632  3544 ep_pol S    ?          0:00  \_ nginx: worker process
5   603 109189 109187  20   0 133632  3544 ep_pol S    ?          0:00  \_ nginx: worker process
5   603 109190 109187  20   0 133632  3544 ep_pol S    ?          0:00  \_ nginx: worker process
5   603 109191 109187  20   0 133632  3544 ep_pol S    ?          0:00  \_ nginx: worker process

最后是问题:

ls -lZ /opt/X/log/nginx/
-rw-r--r--. root root system_u:object_r:httpd_log_t:s0 access.log
-rw-r--r--. root root system_u:object_r:httpd_log_t:s0 error.log

这些文件应该归 Xnginx 所有,而不是 root。为什么?

nginx centos-7
  • 1 1 个回答
  • 2092 Views

1 个回答

  • Voted
  1. Best Answer
    matt
    2020-10-07T01:31:34+08:002020-10-07T01:31:34+08:00

    tl;博士

    这可能是一个错误。当 nginx 启动时,它会创建日志文件,ngx_init_cycle()其中似乎不会调用chown()日志文件。但是,当您指示 nginx 重新打开文件 ( nginx -s reopen) 时,它会通过ngx_reopen_files()调用chown().

    此外,nginx 从不使用user <user> [group]指令中指定的组。

    调查

    一、版本:

    root@69ef55b3f57d:/# nginx -v
    nginx version: openresty/1.13.6.
    

    我user www-data;在我们的/etc/nginx/nginx.conf. www-data有 UID 33。我所有的日志文件都有“.log”后缀。

    首先,让我们删除日志文件,以便 nginx 必须创建它们:

    rm /var/log/nginx/*.log
    

    然后,启动 nginx 并记录strace它的作用。-k记录调用堆栈。

    root@69ef55b3f57d:/# strace -o /out.log -k -e trace=%file /usr/local/openresty/bin/openresty -g 'daemon off;'
    

    注意 nginx 会一直在这里运行直到我们杀死它,所以切换到另一个终端窗口。

    确认日志已创建并归以下人员所有root:root:

    ls -l /var/log/nginx/*.log
    ....
    -rw-r--r-- 1 root root 0 Oct  6 09:02 /var/log/nginx/access.log
    ....
    

    删除旧文件以便 nginx 必须再次创建它们,并要求 nginx 重新打开日志文件:

    rm /var/log/nginx/*.log
    nginx -s reopen
    

    检查文件是否属于www-data:root:

    ls -l /var/log/*.log
    ...
    rw-r--r-- 1 www-data root 0 Oct  6 09:03 /var/log/nginx/access.log
    ...
    

    我们现在可以杀死 nginx,在原来的终端中只需点击CTRL+C(或使用kill)

    现在,让我们看看 strace 记录了什么:

    root@69ef55b3f57d:/# grep -A5 -n /var/log/nginx/access /out.log
    1162:openat(AT_FDCWD, "/var/log/nginx/access.log", O_WRONLY|O_CREAT|O_APPEND, 0644) = 5
    1163- > /lib/x86_64-linux-gnu/libpthread-2.27.so(open64+0x4b) [0x11d2b]
    1164- > /usr/local/openresty/nginx/sbin/nginx(ngx_init_cycle+0x982) [0xc7132]
    1165- > /usr/local/openresty/nginx/sbin/nginx(main+0x891) [0xb4061]
    1166- > /lib/x86_64-linux-gnu/libc-2.27.so(__libc_start_main+0xe7) [0x21b97]
    1167- > /usr/local/openresty/nginx/sbin/nginx(_start+0x2a) [0xb443a]
    --
    1226:openat(AT_FDCWD, "/var/log/nginx/access.log", O_WRONLY|O_CREAT|O_APPEND, 0644) = 4
    1227- > /lib/x86_64-linux-gnu/libpthread-2.27.so(open64+0x4b) [0x11d2b]
    1228- > /usr/local/openresty/nginx/sbin/nginx(ngx_reopen_files+0x91) [0xc7db1]
    1229- > /usr/local/openresty/nginx/sbin/nginx(ngx_master_process_cycle+0x410) [0xdca80]
    1230- > /usr/local/openresty/nginx/sbin/nginx(main+0xa75) [0xb4245]
    1231- > /lib/x86_64-linux-gnu/libc-2.27.so(__libc_start_main+0xe7) [0x21b97]
    --
    1233:stat("/var/log/nginx/access.log", {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
    1234- > /lib/x86_64-linux-gnu/libc-2.27.so(__xstat64+0x15) [0x10f775]
    1235- > /usr/local/openresty/nginx/sbin/nginx(ngx_reopen_files+0xb7) [0xc7dd7]
    1236- > /usr/local/openresty/nginx/sbin/nginx(ngx_master_process_cycle+0x410) [0xdca80]
    1237- > /usr/local/openresty/nginx/sbin/nginx(main+0xa75) [0xb4245]
    1238- > /lib/x86_64-linux-gnu/libc-2.27.so(__libc_start_main+0xe7) [0x21b97]
    --
    1240:chown("/var/log/nginx/access.log", 33, -1) = 0
    1241- > /lib/x86_64-linux-gnu/libc-2.27.so(chown+0x7) [0x1113e7]
    1242- > /usr/local/openresty/nginx/sbin/nginx(ngx_reopen_files+0xd8) [0xc7df8]
    1243- > /usr/local/openresty/nginx/sbin/nginx(ngx_master_process_cycle+0x410) [0xdca80]
    1244- > /usr/local/openresty/nginx/sbin/nginx(main+0xa75) [0xb4245]
    1245- > /lib/x86_64-linux-gnu/libc-2.27.so(__libc_start_main+0xe7) [0x21b97]
    

    如您所见chown(),仅在重新打开日志文件时调用,在ngx_reopen_files()

    • 5

相关问题

  • nginx 不尊重 server_name

  • Centos 7 日期用加号显示当前时间

  • 传入的主机头加倍

  • 续订 letsencrypt SSL 证书后,服务器仅返回响应代码 400

  • Nextcloud 共享在 nginx 反向代理中不起作用

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    fixer1234 “HTTPS Everywhere”仍然相关吗? 2019-10-27 18:06:25 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve