以非root用户身份运行Nginx

问题描述 投票:0回答:6

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

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

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

linux nginx centos7
6个回答
47
投票

在您的

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

user nginx;

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

这样,只有主进程以

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

有关主进程和工作进程的文档的注释:

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

工作进程实际处理请求。

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

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

  • 错误日志
  • 访问日志
  • pid
  • client_body_temp_path
  • fastcgi_temp_path
  • 代理临时路径
  • scgi_临时路径
  • uwsgi_temp_path

将监听指令更改为1024以上的端口,以所需用户身份登录并通过

nginx -c /path/to/nginx.conf

运行nginx

19
投票

为了测试/调试目的,我有时会在我的 Debian(延伸)笔记本电脑上以非特权用户身份运行 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

17
投票

以防万一它可以帮助有人在 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;
        }
      }
    }

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

它很容易配置(请参阅docs),甚至可以在运行时定义的随机 UID 下运行(即不在 Dockerfile 中硬编码)。事实上,这是 Bitnami 的政策,即让所有容器都无根,并为运行时的 UID 更改做好准备,这就是为什么我们在非常注重安全的 Openshift 3.x 下使用它们几年了(

bitnami/nginx
特别是需要反向代理才能启用 MLflow Web 应用程序的身份验证)。


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”

  • 主进程现在应该以您的“所述用户”身份运行。

0
投票

很可能以普通用户身份运行 nginx,并且能够列出 443 端口。为此,您需要利用 systemd 强化功能。 查看此存储库以了解操作方法

© www.soinside.com 2019 - 2024. All rights reserved.