【问题标题】:Why nginx not read my conf file using docker?为什么 nginx 不使用 docker 读取我的 conf 文件?
【发布时间】:2019-08-13 09:04:43
【问题描述】:

我在我的机器(本地主机)上使用 docker 运行 nginx。

当我浏览到 localhost:8080 我希望得到“hello world”,但我得到“Welcome to nginx!”屏幕。

我在配置中遗漏了什么?

docker-compose.yml

web:
  image: nginx
  volumes:
    - ./example.com.conf:/etc/nginx/conf.d/example.com.conf
  ports:
    - '8080:80'

example.com.conf

server {
    location / {
        return 200 "hello world";
    }
}

我运行命令:

docker-compose up

【问题讨论】:

    标签: docker nginx docker-compose


    【解决方案1】:

    nginx镜像里面有一个/etc/nginx/conf.d/default.conf文件,里面有

    server {
      listen        80;
      server_name   default_server;
      ...
    }
    

    您要么删除 default.conf 文件并正确设置您的 example.com.conf(监听端口、服务器名称等),要么将 default.conf 替换为您的 example.com.conf

    您可以通过以下方式替换:

    volumes:
      - ./example.com.conf:/etc/nginx/conf.d/default.conf
    

    【讨论】:

    • 但我不想覆盖来自 nginx 的配置。因为可能有我想要的重要规则和行为。如果我用自己的文件覆盖,那么我会丢失 nginx 的所有内容。
    • 为什么要丢失 nginx 的所有内容? conf文件没有被合并!他们是“独立”的网站。你需要正确设置你的 conf 文件,如果你仍然想保留 default.conf(它仍然会监听端口 80)你需要让你的 exmaple.conf 监听一些 not 一个 80端口并将您的网站请求为 http://...:8080(如果您将端口设置为侦听 8080)
    • @JonSud 首先,当您将 default.conf 文件替换为您自己的文件时,不会发生任何坏事,如果您觉得这样,请不要在自定义 conf 文件中使用 80 端口,使用其他端口,例如: 8080 并在端口部分更改为“8080:8080”
    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 2021-09-26
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    • 2021-02-18
    • 2015-06-02
    相关资源
    最近更新 更多