nginx-common包附带的systemd单元文件以root身份运行主进程,这让我感到紧张。
我假设它以root身份运行的唯一原因是绑定端口80,但作为root是为了矫枉过正,所以我以普通用户的身份运行它,并为其提供CAP_NET_BIND_SERVICEfunction。
我已经调整了单元文件,看起来像这样:
[Service] Type=forking PIDFile=/var/run/nginx/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=/usr/sbin/nginx -s quit CapabilityBoundingSet=CAP_NET_BIND_SERVICE Capabilities=cap_net_bind_service+=ep user=www-data
这种方法有什么缺点吗? nginx是否因为其他原因而期望成为root? 有没有更好的方法来lockingnginx? 我知道一些人喜欢做这个,但是这似乎很麻烦。
主进程必须以root身份运行,否则nginx将无法绑定到端口80,因为这是一个特权端口。
http://www.w3.org/Daemon/User/Installation/PrivilegedPorts.html
您应该确保工作进程使用不同的用户。 这个用户可以在你的nginx.conf指定。