【问题标题】:starting nginx docker container with custom nginx config file使用自定义 nginx 配置文件启动 nginx docker 容器
【发布时间】:2020-03-30 22:03:05
【问题描述】:

我的要求

步骤

我正在遵循来自

的指示

我正在尝试创建一个容器(名称 = mynginx1),指定我自己的 nginx 配置文件

$ docker run --name mynginx1 -v C:/zNGINX/testnginx/conf:/etc/nginx:ro -P -d nginx

其中“C:/zNGINX/testnginx/conf”包含文件“default.conf”,其内容为

server {
    listen 80;
    server_name  localhost;

    location / {
        proxy_pass http://localhost:3000;
    }

}

返回一个容器 ID,但“docker ps”没有显示它正在运行。

使用“docker logs mynginx1”查看容器日志显示如下错误

2020/03/30 12:27:18 [emerg] 1#1: open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: No such file or directory)

我做错了什么?

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    我正在做的事情有 2 个错误

    (1) 在 conf 文件中,我使用的是“proxy_pass http://localhost:3000;”
    容器中的“localhost”是 CONTAINER 主机,而不是我的计算机。因此,这需要更改为

    proxy_pass http://host.docker.internal:3000;
    

    (2) 将我的配置文件复制到容器上的路径不对,我需要添加“conf.d”

    docker run --name mynginx1 -v C:/zNGINX/testnginx/conf:/etc/nginx/conf.d:ro -P -d nginx
    

    我正在阅读的文档(多个网站)没有提到在路径末尾添加“conf.d”目录。但是,如果您查看“/etc/nginx/nginx.conf”文件,最后一行会有提示

    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;
    }
    

    “包括/etc/nginx/conf.d/*.conf;”表示它从“/etc/nginx/conf.d/”目录加载任何以“.conf”结尾的文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-31
      相关资源
      最近更新 更多