Dockerized Nginx将不同的子域重定向到同一页面

问题描述 投票:-1回答:1

Background

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

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

Problem description

我在网上关于如何设置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"

因此,地址确实被识别为seafile,这有助于我消除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:问题是由于配置错误(请参阅接受的答案)+我的旧注册商的剩余重定向导致了奇怪的行为。谢谢您的帮助!

docker nginx
1个回答
0
投票

试试这个在我的本地运行。发现你在容器中安装了错误的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
© www.soinside.com 2019 - 2024. All rights reserved.