Docker上的Localhost出现“服务器名称冲突”问题

时间:2018-12-11 12:06:29

标签: php docker nginx docker-compose

我正在尝试使用本地主机启动docker映像。该代码是开源的,在这里:

https://github.com/Helium-MVC/Boilerplate

运行docker-compose up时,我得到以下信息:

helium_nginx | 2018/12/11 07:43:31 [warn] 1#1: conflicting server name "localhost" on 0.0.0.0:80, ignored
helium_nginx | nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored

现在我的docker site.conf仅具有1个虚拟主机:

server {
    #The port to listen on. SSL would listen on 443
    listen 80 default_server;

    #The name/alias the server listens for when deciding to use this configuration
    server_name  localhost _;

    #Where the lo files are being written too
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    #Location of our site
    location / {
            #The root of the site is in public_html
        root    /code/site/public_html/;
        index  index.php index.html index.htm;
        #important for pretty url and routing
        try_files $uri $uri/ /index.php?rt=$uri&$args;
    }                                                                 
}

像这样在yml中设置它:

web:
    image: nginx:latest
    container_name: "helium_nginx"
    ports:
      - "8000:80"
    volumes:
      - ./:/code
      - ./site.conf:/etc/nginx/conf.d/site.conf
      - ./nginx_custom_settings.conf:/etc/nginx/conf.d/nginx_custom_settings.conf
    links: ['php']

奇怪的是,如果我将server_name更改为www.example.com之类的名称,然后将该名称放入我的/etc/hosts文件中,就可以使用!否则,使用本地主机,我只会得到 nginx默认页面。有什么问题吗?

2 个答案:

答案 0 :(得分:1)

要提供答案,我必须更改默认的nginx配置。在我的YML中,我添加了:

services:
  web:
    image: nginx:latest
    container_name: "helium_nginx"
    ports:
      - "8000:80"
    volumes:
      - ./:/usr/share/nginx/html
      - ./:/code
      - ./site.conf:/etc/nginx/conf.d/site.conf
    additionnx.conf:/etc/nginx/nginx.conf

加法为- ./nginx.conf:/etc/nginx/nginx.conf

要复制的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/site.conf;
}

我所做的更改不是包含要加载所有配置的/etc/nginx/conf.d/*.conf;,而是加载了我想要的包含/etc/nginx/conf.d/site.conf;的配置

答案 1 :(得分:0)

我正在docker中使用nginx映像,该映像具有一个带有default.conf的{​​{1}}文件和侦听端口80。我只需替换该文件并解决该问题即可。

docker-compose.yml

server_name localhost

和http_reverse_proxy.conf

version: '2'
services:
  nginx: 
    image: nginx:latest
    volumes:
      - ./tmp/https_reverse_proxy.conf:/etc/nginx/conf.d/https_reverse_proxy.conf
      - ./tmp/http_reverse_proxy.conf:/etc/nginx/conf.d/default.conf
    ports:
      - 80:80
      - 443:443