更改nginx配置文件

3

我安装了OpenResty Alpine Docker镜像,并将conf.d挂载到其中以定义服务器。它工作得很好。

接下来,我想更改nginx.conf并设置worker_process = auto。但是worker_processes已在nginx.conf中定义。我尝试在Docker-compose文件中通过卷挂载nginx.conf

volumes:
 -  ./conf.d:/etc/nginx/conf.d  
 -  ./conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf

不过,它创建一个名为nginx.conf的目录在./conf中。

我该如何挂载/修改nginx.conf

1个回答

3
如果你想更新nginx根配置,但是使用了错误的Docker目录进行挂载。 Nginx配置文件 Docker工具会安装它自己的nginx.conf文件。如果你想直接覆盖它,可以通过自己的Dockerfile或卷挂载来替换它。对于Linux镜像,该nginx.conf包含指令include /etc/nginx/conf.d/*.conf;因此该目录中的所有nginx配置都将被包括在内。默认虚拟主机配置具有原始OpenResty配置,并复制到/etc/nginx/conf.d/default.conf
docker run -v /my/custom/conf.d:/etc/nginx/conf.d openresty/openresty:alpine

第二件事,最好使用绝对路径进行挂载。

docker run -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf openresty/openresty:1.15.8.2-1-alpine

或者
docker run -v abs_path/nginx.conf:/etc/nginx/nginx.conf openresty/openresty:1.15.8.2-1-alpine

Openresty配置:

docker run -v $PWD/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf openresty/openresty:1.15.8.2-1-alpine

你应该挂载指定的文件,否则容器会出问题。

enter image description here 这是 /usr/local/openresty/nginx/conf/nginx.conf 的默认配置文件。

# nginx.conf  --  docker-openresty
#
# This file is installed to:
#   `/usr/local/openresty/nginx/conf/nginx.conf`
# and is the file loaded by nginx at startup,
# unless the user specifies otherwise.
#
# It tracks the upstream OpenResty's `nginx.conf`, but removes the `server`
# section and adds this directive:
#     `include /etc/nginx/conf.d/*.conf;`
#
# The `docker-openresty` file `nginx.vh.default.conf` is copied to
# `/etc/nginx/conf.d/default.conf`.  It contains the `server section
# of the upstream `nginx.conf`.
#
# See https://github.com/openresty/docker-openresty/blob/master/README.md#nginx-config-files
#

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

    include /etc/nginx/conf.d/*.conf;
}

$PWD 对我来说不起作用,但是我现在可以使用相对路径,因为它仍然可以正常工作。然而,我仍然无法挂载 /usr/local/openresty/nginx/conf/nginx.conf 文件。我已经根据我的 docker-compose 文件中的条目修改了问题。 - marcg
我更新了答案,附上截图,它正常工作。你用的是哪个操作系统? - Adiii
Linux Mint。您是否挂载了 /etc/nginx/nginx.conf 或 /usr/local/openresty/nginx/conf/nginx.conf 的卷?您的答案包含两者。请注意,/etc/nginx/nginx.conf 在我的容器中不存在。如果您查看我的 docker-compose,我已经挂载了 /usr/local/openresty/nginx/conf/nginx.conf。 - marcg
看起来你正在使用不同的镜像标签。请尝试使用 openresty/openresty:1.15.8.2-1-alpine - Adiii
我正在使用openresty/openresty:1.15.8.2-1-alpine,但问题仍然存在。你是否挂载了/etc/nginx/nginx.conf或/usr/local/openresty/nginx/conf/nginx.conf? - marcg
我正在挂载 usr/local/openresty/nginx/conf/nginx.conf,请确保它在主机操作系统上。 - Adiii

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