docker - Dockerized Nginx 将不同的子域重定向到同一页面
问题描述
背景
我正在尝试使用一堆 webapps 建立一个家庭服务器。目前,我专注于 seafile 和静态 html 页面。服务器使用 Docker 和 Nginx。我想实现以下行为:
- 该地址
domain-name.eu
重定向到静态页面,显示“欢迎使用域名”。 - 地址
seafile.domain-name.eu
重定向到 seafile 容器。
问题描述
我按照网上的各种教程介绍了如何设置 docker-compose.yml 和 nginx conf 以允许 nginx 服务于不同的网站。我设法让我的 seafile 在正确地址的 nginx 后面单独工作,并且我设法让静态页面在正确地址的 nginx 后面单独工作。当我尝试将两者混合使用domain-name.eu
并seafile.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:问题是由于配置错误(见接受的答案)+来自我的旧注册商的残留重定向导致了奇怪的行为。谢谢您的帮助!
解决方案
尝试在我的本地运行。发现你在容器中挂载了错误的 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
推荐阅读
- php - PHP Laravel将数据合并到单个数组
- xslt-1.0 - 这个xpath可以简化吗?
- c++ - 查找数组中信号幅度(最大值)的函数和指针
- jupyter-notebook - 如何在 nbconvert 库中配置工作目录
- javascript - 如何通过 jquery 监听器访问 javascript 原型方法?
- android - 如何在android中获取当前设备语言?
- file-upload - 将 UploadedFile (primefaces) 转换为 FileObject (Apache Common)
- reactjs - 为什么在 React 中,我的 axios API 调用具有包含 Bearer 的授权标头
但未获得授权并给出 401 错误 - c# - or-tools - 从 SumArray() 计算标准差
- windows - 不支持产品 (Visual Studio 2019)