nginx:[emerg] open()“/var/run/nginx.pid”失败(13:权限被拒绝)

4

我有以下的 Docker 文件

FROM grafana/grafana
EXPOSE 8080 8080
COPY config /config
COPY start-nginx-grafana.sh /start-nginx-grafana.sh
USER root
RUN apt-get update && apt-get install -y nginx
RUN chown -R grafana:grafana /etc/nginx/nginx.conf /var/log/nginx /var/lib/nginx /start-nginx-grafana.sh
RUN chmod +x /start-nginx-grafana.sh /etc/nginx/nginx.conf /var/log/nginx /var/lib/nginx
USER grafana
RUN cp /config/nginx.conf /etc/nginx/nginx.conf
ENTRYPOINT [ "/start-nginx-grafana.sh" ]

当我构建并尝试运行容器时,它可以顺利运行,但是我无法访问Nginx代理后面的网站。因此我检查了docker日志,发现了以下内容:

nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied)

我的 Nginx 配置如下:
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
    server {
        listen 3001;
        root /usr/share/nginx/www;
        index index.html index.htm;

        location / {
                proxy_pass                            http://localhost:3000/;
                proxy_set_header Host                 $http_host;
                proxy_set_header X-Real-IP            $remote_addr;
                proxy_set_header X-Forwarded-For      $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto    $scheme;
                proxy_set_header X-WEBAUTH-USER       "";
                }
        }
    server {
        listen 8080;
        location / {
                proxy_pass                            http://localhost:3000/;
                proxy_set_header Host                 $http_host;
                proxy_set_header X-Real-IP            $remote_addr;
                proxy_set_header X-Forwarded-For      $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto    $scheme;
                proxy_set_header X-WEBAUTH-USER       "guest";
        }
    }
}

我该如何解决 nginx: [emerg] open() "/var/run/nginx.pid" failed (13: Permission denied) 问题以及我做错了什么。

我通常会将Grafana和Nginx运行在不同的容器中,这样可以避免出现特定的问题。(而且您应该能够立即注意到Nginx容器启动失败,而不影响Grafana容器。) - David Maze
你可以尝试将nginx.conf中的用户更改为grafana,然后将nginx服务器也作为grafana运行。可能会起作用。 - F.Madsen
1个回答

2
你需要用用户grafana运行所有进程。
警告状态:主要的nginx进程不是超级用户。 grafana用户无法访问文件/var/run/nginx.pid
我建议你从Dockerfile中删除USER grafana,并在脚本中使用以下命令来运行grafana
runuser -l grafana -c "...."

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