繁体   English   中英

以非 root 用户身份运行 Nginx

[英]Running Nginx as non root user

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

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

我知道以 root 身份运行是不可取的。 那么我如何解决这个问题并以非 root 用户身份运行 nginx。 谢谢

/etc/nginx/nginx.conf添加/更改以下/etc/nginx/nginx.conf

user nginx;

您应该创建用户并以递归方式授予 webroot 目录的权限。

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

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

更改以下内容的所有权:

  • 错误日志
  • 访问日志
  • 进程号
  • client_body_temp_path
  • fastcgi_temp_path
  • proxy_temp_path
  • scgi_temp_path
  • uwsgi_temp_path

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

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

以防万一它有助于在 2020 年遇到这个问题的人,这是我的最小 nginx.conf,用于在端口 8088 上运行 Web 服务器,适用于非 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;
        }
      }
    }

为什么不使用无根的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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM