【问题标题】:Running Nginx as non root user以非 root 用户身份运行 Nginx
【发布时间】:2017-07-08 19:53:59
【问题描述】:

我使用 Ansible 安装了 Nginx。为了在 Centos7 上安装,我使用了 yum 包,所以它默认以 root 用户身份运行。我希望它在 Centos 框中以不同用户(例如 - nginx 用户)的身份启动和运行。当我尝试使用其他用户运行它时,我收到以下错误:

nginx.service 的作业失败,因为控制进程退出 错误代码。请参阅“systemctl status nginx.service”和“journalctl -xe” 了解详情。

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

【问题讨论】:

标签: linux nginx centos7


【解决方案1】:

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

user nginx;

您应该递归地创建用户并授予对 webroot 目录的权限。

这种方式只有主进程以root 运行。 因为:只有 root 进程可以监听 1024 以下的端口。网络服务器通常运行在端口 80 和/或 443。这意味着它需要以 root 身份启动。

以非 root 用户身份运行主进程:

更改以下内容的所有权:

  • 错误日志
  • 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

【讨论】:

  • 嗨法哈德。默认情况下,在 conf 文件中设置为该用户。我尝试了su - to nginx并启动它,仍然是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:工作进程
  • 我的也是这样,只有master进程以root身份运行
  • 更新了答案
  • 好的。感谢您为我检查。但是,我不确定主进程和工作进程的区别。只是想知道是否存在让主进程以 root 身份运行的风险。
【解决方案2】:

为了以防万一,为了测试/调试目的,我有时会在我的 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

【讨论】:

    【解决方案3】:

    以防万一它有助于有人在 2020 年遇到这个问题,这是我用于在端口 8088 上运行 Web 服务器的最小 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 的回答提到了同样的原则,但无论如何感谢! :)。
    【解决方案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 甚至在内部都没有侦听受 root 限制的端口 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
    

    它很容易配置(参见docs),甚至可以在运行时定义的随机 UID 下运行(即未在 Dockerfile 中硬编码)。事实上,这是 Bitnami 的策略,即让所有容器无根并在运行时为 UID 更改做好准备,这就是为什么我们几年来一直在非常注重安全的 Openshift 3.x 下使用它们(bitnami/nginx 特别是启用对 MLflow Web 应用的身份验证所需的反向代理)。

    【讨论】:

    • 非常容易设置和安全意识。这个答案应该更高。
    猜你喜欢
    • 1970-01-01
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-19
    • 2020-11-16
    • 2015-04-01
    相关资源
    最近更新 更多