gpt4 book ai didi

nginx - 用于 NGINX Web 服务器的 Dockerfile

转载 作者:行者123 更新时间:2023-12-04 14:51:27 27 4
gpt4 key购买 nike

我一直在尝试建立一个本地 docker 容器来托管 NGINX 服务器。首先,这是我的 Dockerfile:

# Set nginx base image
FROM nginx

# File Author / Maintainer
MAINTAINER myuser "myemail@mydomain.com"

# Copy custom configuration file from the current directory
COPY nginx.conf /etc/nginx/nginx.conf

我确实使用 docker build 命令构建了这个文件,当我列出图像时,我可以在列表中看到这个图像。

现在,我尝试运行这个新创建的图像,结果出现错误:
my-MacBook-Pro:nginx-docker me$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myrepo nginx-latest 0d73419e8da9 12 minutes ago 182.8 MB
hello-world latest c54a2cc56cbb 13 days ago 1.848 kB
nginx latest 0d409d33b27e 6 weeks ago 182.8 MB

my-MacBook-Pro:nginx-docker me$ docker run -it myrepo:nginx-latest
2016/07/15 07:07:35 [emerg] 1#1: open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)

日志文件的路径在我的 nginx.conf 中配置,如下所示:
#user  nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include 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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 8080;
server_name localhost;
root /Users/me/Projects/Sandbox/my-app;

#charset koi8-r;

access_log logs/host.access.log main;

#
# Wide-open CORS config for nginx
#
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

# /api will server your proxied API that is running on same machine different port
# or another machine. So you can protect your API endpoint not get hit by public directly
location /api {
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

add_header Access-Control-Allow-Origin *;

proxy_set_header Access-Control-Allow-Origin $http_origin;
}

error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

include servers/*;
}

当我现在尝试运行此 NGINX 镜像时,出现以下错误:
2016/07/15 07:07:35 [emerg] 1#1: open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)

我该怎么做才能解决这个问题?还有那条路径应该是什么以及在哪里?我想它是在 Docker 公开的底层操作系统路径上?

最佳答案

您的 Dockerfile 中的基本图像是 nginx ( nginx:latest 准确地说)。它有一个来自 Debian Nginx 包的预配置 nginx 配置。您可以自己检查容器:docker run -it --rm nginx /bin/bash并查看文件和目录以了解有关它的一些事实:

  • 它提供 nginx用户
  • 它提供 /var/log/nginx目录,但 root拥有它
  • 它提供 access.logerror.log在任何人都可以写的目录中

  • ( Dockerfile 用于基础 Nginx 图像是 here )

    您的配置:
  • 运行 Nginx 为 nginx (它是 default )
  • 尝试将日志文件写入 /etc/nginx/logs

  • 显然,这个目录不存在,因为没有人创建它。如果它存在,它应该可以由 nginx 写用户。

    关于nginx - 用于 NGINX Web 服务器的 Dockerfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38390246/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com