以非root用户身份运行Nginx

34

我使用Ansible安装了Nginx。在Centos7上安装时,我使用yum包,因此默认以root用户身份运行。我想要将其作为不同的用户(例如nginx用户)启动和运行在Centos机器上。当我尝试用不同的用户运行它时,会出现以下错误:

  

nginx.service启动失败,因为控制进程退出并显示错误代码。请参阅“systemctl status nginx.service”和“journalctl -xe”获取详细信息。

我知道建议不要以root身份运行,那么我该如何解决这个问题并以非root用户身份运行nginx。谢谢。


6个回答

48

请在您的/etc/nginx/nginx.conf中添加/更改以下内容:

user nginx;

你应该创建用户并递归授予webroot目录权限。

这样只有主进程以root身份运行。因为:只有root进程才能监听1024以下的端口。Web服务器通常在80号和/或443号端口运行。这意味着它需要以root身份启动。

来自主控和工作进程文档的注意事项:

主进程的主要目的是读取和评估配置文件,以及维护工作进程。

工作进程执行实际请求处理。

要将主进程作为非root用户运行:

更改以下Nginx指令所指定路径的文件所有权:

  • error_log
  • access_log
  • pid
  • client_body_temp_path
  • fastcgi_temp_path
  • proxy_temp_path
  • scgi_temp_path
  • uwsgi_temp_path

将侦听指令更改为1024以上的端口,以所需用户身份登录,然后通过nginx -c /path/to/nginx.conf运行nginx。


1
嗨Farhad。在conf文件中,这是默认设置为此用户的。我尝试使用su -切换到nginx并启动它,但进程仍然是root。 - Sarith
root 5830 0.0 0.1 122232 2216 ? Ss 17:07 0:00 nginx: 主进程 /usr/sbin/nginx nginx 5831 0.0 0.1 122664 3292 ? S 17:07 0:00 nginx: 工作进程 nginx 5832 0.0 0.1 122664 3088 ? S 17:07 0:00 nginx: 工作进程 - Sarith
我的情况也一样,只有主进程以root身份运行。 - Farhad Farahi
它也在2048端口上运行。 - Sarith
应该使用 fastcgi 而不是 fast_cgi。 - Prince Mishra
显示剩余8条评论

20

以防有帮助,为了测试/调试目的,我有时会在我的Debian(stretch)笔记本电脑上作为非特权用户运行nginx实例。

我使用像这样的最小配置文件:

worker_processes 1;
error_log stderr;
daemon off;
pid nginx.pid;

events {
  worker_connections  1024;
}

http {
  include             /etc/nginx/mime.types;
  default_type        application/octet-stream;

  sendfile on;

  keepalive_timeout   65;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2; 
  ssl_prefer_server_ciphers on;
  access_log access.log;
  server {
    listen            8080;
    server_name       localhost;

    location / {
      include /etc/nginx/uwsgi_params;
      uwsgi_pass localhost:8081;
    }
  }
}

然后我开始这个过程:

/usr/sbin/nginx -c nginx.conf -p $PWD

18

如果有人在2020年碰到这个问题,以下是我用于在8088端口上运行网页服务器的最简nginx.conf配置文件,适用于非root用户。不需要修改文件权限!(在Centos 7.4上测试过,使用nginx 1.16.1)

    error_log /tmp/error.log;
    pid       /tmp/nginx.pid;
    
    events {
      # No special events for this simple setup
    }
    http {
      server {
        listen       8088;
        server_name  localhost;
    
        # Set a number of log, temp and cache file options that will otherwise
        # default to restricted locations accessible only to root.
        access_log /tmp/nginx_host.access.log;
        client_body_temp_path /tmp/client_body;
        fastcgi_temp_path /tmp/fastcgi_temp;
        proxy_temp_path /tmp/proxy_temp;
        scgi_temp_path /tmp/scgi_temp;
        uwsgi_temp_path /tmp/uwsgi_temp;
    
        # Serve local files
        location / {
          root /home/<your_user>/web;
          index  index.html index.htm;
          try_files $uri $uri/ /index.html;
        }
      }
    }

答案由@david-douard提到了相同的原则,不过还是谢谢! :)。 - jg6

4

为什么不使用无根bitnami/nginx镜像:

$ docker run --name nginx bitnami/nginx:latest
  • 更多信息

为了验证它并非以 root 用户身份运行,而是以标准用户 (属于docker 组) 的身份运行:

$ docker exec -it nginx id
uid=1**8 gid=0(root) groups=0(root)

为了验证Nginx没有在内部监听受限的443端口:

$ docker ps -a | grep nginx
2453b37a9084   bitnami/nginx:latest                       "/opt/bitnami/script…"   4 minutes ago    Up 30 seconds                 8080/tcp, 0.0.0.0:8443->8443/tcp                  jenkins_nginx

这个配置很容易(参见文档),并且即使在运行时定义的随机UID下也可以运行(即没有在Dockerfile中硬编码)。实际上,这是Bitnami的策略,使得他们的所有容器都是无根(rootless)的,并准备好在运行时进行UID更改。这就是为什么我们在非常注重安全的Openshift 3.x下使用它们已经几年了(bitnami/nginx特别作为反向代理需要启用对MLflow Web应用程序的身份验证)。


1
非常容易设置且注重安全性。这个答案应该排名更高。 - Alistair Jones
1
我喜欢这个答案。只是要补充一点,有一种情况是你想要一个更小的图像。这个 Bitnami 大小(未压缩)约为 90MB,而 nginx:1.23.3-alpine-slim 则为 11.5MB。 - rustyMagnet

0
为了让主NGINX进程以非特权用户身份运行,您需要:
  1. 更改/etc/nginx目录的所有权以由该用户拥有。
  2. 更改/var/log/nginx的所有权以由相同的用户拥有。
  3. 更新/etc/nginx/nginx.conf文件以赋予它读取权限(644)。

免责声明:确保在/etc/nginx/sites-enabled/default中,监听使用非特权端口。

最后,请用所述用户登录并执行“nginx -c /etc/nginx/nginx.conf”。
  • 现在,主进程应该正在以您的“said user”身份运行。

0

虽然这个链接可能回答了问题,但最好在这里包含答案的关键部分,并提供链接作为参考。仅有链接的答案如果链接页面发生变化,就会失效。- 来自审核 - mechnicov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接