首页 > 解决方案 > Dockerized Nginx 将不同的子域重定向到同一页面

问题描述

背景

我正在尝试使用一堆 webapps 建立一个家庭服务器。目前,我专注于 seafile 和静态 html 页面。服务器使用 Docker 和 Nginx。我想实现以下行为:

  1. 该地址domain-name.eu重定向到静态页面,显示“欢迎使用域名”。
  2. 地址seafile.domain-name.eu重定向到 seafile 容器。

问题描述

我按照网上的各种教程介绍了如何设置 docker-compose.yml 和 nginx conf 以允许 nginx 服务于不同的网站。我设法让我的 seafile 在正确地址的 nginx 后面单独工作,并且我设法让静态页面在正确地址的 nginx 后面单独工作。当我尝试将两者混合使用domain-name.euseafile.domain-name.eu提供静态页面“欢迎使用域名”时。

这是我的docker-compose.yml

nginx:
    image: nginx
    ports:
        - 80:80
    volumes:
        - ./nginx.conf:/etc/nginx/nginx.conf
        - ./html/:/usr/share/nginx/html
    links:
        - seafile

seafile:
    image: seafileltd/seafile:latest
    expose:
        - 80
    volumes:
        - /home/docker/seafile-data:/shared

我的nginx.conf

http {

  upstream seafile {
  server seafile;
}
server {
   listen 80;
   server_name seafile.domain-name.eu;

   location /{
     proxy_pass http://seafile/;
     proxy_http_version 1.1;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection 'upgrade';
     proxy_cache_bypass $http_upgrade;
    }
  }

server {
   listen 80;
   server_name domain-name.eu;
   root /usr/share/nginx/html;
   index index.html index.htm;

 }
}
events {}

当我尝试访问seafile.domain-name.eu时,我从 nginx 容器收到此日志:

nginx_1    | xxx.xxx.xxx.xxx - - [05/Jun/2018:09:44:24 +0000] "GET / HTTP/1.1" 200 22 "http://seafile.domain-name.eu/" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"

当我尝试访问时domain-name.eu,我收到:

nginx_1    | xxx.xxx.xxx.xxx - - [05/Jun/2018:10:07:11 +0000] "GET / HTTP/1.1" 200 22 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0"

所以这个地址确实被海文件识别了,这帮助我消除了我的 DNS 配置错误的可能原因。还是我弄错了?

谁能帮我解决问题?谢谢。

编辑:添加docker ps输出:

CONTAINER ID        IMAGE                       COMMAND                CREATED             STATUS              PORTS                NAMES
b6d018169d76        nginx                       "nginx -g 'daemon of…"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp   jarvis-compose_nginx_1
7e701ce7650d        seafileltd/seafile:latest   "/sbin/my_init -- /s…"   About an hour ago   Up About an hour    80/tcp               jarvis-compose_seafile_1

编辑 2:问题是由于配置错误(见接受的答案)+来自我的旧注册商的残留重定向导致了奇怪的行为。谢谢您的帮助! 

标签: dockernginx

解决方案


尝试在我的本地运行。发现你在容器中挂载了错误的 nginx 配置文件。nginx.conf应该安装在 上/etc/nginx/conf.d/default.conf,这是在 nginx 上支持 vhost 的默认配置。以下是正确的设置:

nginx.conf

upstream seafile {


server seafile;
}

server {
   listen 80;
   server_name seafile.domain-name.eu;

   location /{
     proxy_pass http://seafile/;
     proxy_http_version 1.1;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection 'upgrade';
     proxy_cache_bypass $http_upgrade;
   }
}

server {
   listen 80;
   server_name domain-name.eu;
   root /usr/share/nginx/html;
   index index.html index.htm;

}

码头工人-compose.yml

version: '3'
services:
    nginx:
        image: nginx
        ports:
            - 80:80
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/default.conf
            - ./html/:/usr/share/nginx/html
        links:
            - seafile
        container_name: web

    seafile:
        image: seafileltd/seafile:latest
        expose:
            - 80
        volumes:
            - /home/docker/seafile-data:/shared
        container_name: seafile_server

推荐阅读