在 nginx/nginx-proxy 容器中使用 Nextcloud 以及其他服务
Posted
技术标签:
【中文标题】在 nginx/nginx-proxy 容器中使用 Nextcloud 以及其他服务【英文标题】:Using Nextcloud inside a nginx/nxgin-proxy container togheter with other services 【发布时间】:2018-01-28 02:52:48 【问题描述】:我正在尝试让nextcloud:fpm
docker 图像与nginx
docker 图像和neginx-proxy
docker 图像一起运行,并尝试从同一个@提供除netxcloud(用于实例声纳等)之外的多个服务987654325@图片。
为了做到这一点,我想配置所有内容,以便当我尝试时:http//my-server.com/nextcloud
我会看到 nexcloud,当我执行“http//my-server.com/sonarr”时,我会转到 sonarr服务。
我创建了一个目录,例如:
nginx
那里有与 nginx 相关的所有内容。
在nginx
nextcloud` 里面我配置了nexcloud。
在nginx
中,docker-compose.yml
是:
version: '2'
services:
proxy:
image: jwilder/nginx-proxy
container_name: proxy
ports:
- 80:80
- 443:443
volumes:
- /mnt/server/proxy/conf.d:/etc/nginx/conf.d
- /mnt/server/proxy/vhost.d:/etc/nginx/vhost.d
- /mnt/server/proxy/html:/usr/share/nginx/html
- /mnt/server/proxy/certs:/etc/nginx/certs:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
networks:
- proxy-tier
restart: always
letsencrypt-companion:
image: alastaircoote/docker-letsencrypt-nginx-proxy-companion
container_name: letsencrypt-companion
volumes_from:
- proxy
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- /mnt/server/proxy/certs:/etc/nginx/certs:rw
restart: always
web:
# image: nginx:alpine
image: nginx
container_name: nginx-webserver
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf:ro
- /mnt/server/nextcloud:/var/www/html/nextcloud/
external_links:
- nextcloud
environment:
- VIRTUAL_HOST=my-server.com
- VIRTUAL_NETWORK=nginx-proxy
- VIRTUAL_PORT=80
- LETSENCRYPT_HOST=my-server.com
- LETSENCRYPT_EMAIL=myemail@google.com
networks:
- proxy-tier
restart: always
networks:
proxy-tier:
external:
name: nginx-proxy
还有nginx.conf
:
user www-data;
events
worker_connections 768;
http
upstream docker-nextcloud
server nextcloud:9000;
server
listen 80;
location /nextcloud
proxy_pass http://docker-nextcloud;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
server_name_in_redirect on;
在nextcloud
目录下我的docker-compose.yml
:
version: '2'
services:
nextcloud:
image: nextcloud:fpm
container_name: nextcloud
links:
- db
volumes:
- /mnt/server/nextcloud:/var/www/html/nextcloud/
- /mnt/server/nextcloud/apps:/var/www/html/nextcloud/apps/
- /mnt/server/nextcloud/config:/var/www/html/nextcloud/config/
- /mnt/server/nextcloud/data:/var/www/html/nextcloud/data/
networks:
- proxy-tier
restart: always
db:
image: postgres
container_name: db
volumes:
- /mnt/server/nextcloud/db:/var/lib/postgresql/
environment:
- POSTGRES_DB=nextcloud
- POSTGRES_USER=nextcloud
- POSTGRES_PASSWORD=somepassword
networks:
- proxy-tier
restart: always
networks:
proxy-tier:
external:
name: nginx-proxy
这一切都失败了,nginx
docker 容器日志中的消息:
2017/08/19 15:05:09 [error] 8#8: *3 recv() failed (104: Connection reset by peer) while reading response header from upstream, client:
172.18.0.6, server: , request: "GET /nextcloud/ HTTP/1.1", upstream: "http://172.18.0.4:9000/nextcloud/", host: "my-server.com"
172.18.0.6 - - [19/Aug/2017:15:05:09 +0000] "GET /nextcloud/ HTTP/1.1" 502 173 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:55.0) Gecko/20100101 Firefox/55.0"
【问题讨论】:
那么你在nginx代理镜像前面放了一个nginx服务器?不应该只是nginx代理,还是在nginx代理之后放置一个nginx服务器?你看过nextcloud官方的docker-compose.yml吗? github.com/nextcloud/docker/blob/master/.examples/… 【参考方案1】:以下几行确实危险,我建议不要在记录的内容之外进行任何其他卷映射:
- /mnt/server/nextcloud:/var/www/html/nextcloud/
- /mnt/server/nextcloud/apps:/var/www/html/nextcloud/apps/
- /mnt/server/nextcloud/config:/var/www/html/nextcloud/config/
- /mnt/server/nextcloud/data:/var/www/html/nextcloud/data/
其背后的原因是入口点脚本对 /var/www/html
的所有文件执行 rsync 并删除它们未排除的所有内容。结果,rsync 脚本试图删除/var/www/html/nextcloud
和它下面的所有数据。
实际上,如果版本不同,它在内部是如何工作的:将所有文件从一个文件夹复制到 /var/www/html 并删除除了一小部分排除文件夹(例如 /data)之外的所有文件.这就是它找不到所需文件的原因:它们只被复制到 /var/www/html 而不是 /var/www/html/nextcloud 期望它们的地方。
我认为以下选项是一种解决方法:
使用nextcloud:apache
镜像,只需使用 nginx 作为反向代理来重写它:
location /nextcloud/
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://nextcloud:80; # nexcloud is the name of the container
client_max_body_size 1000M;
rewrite ^/nextcloud(/.*)$ $1 break;
使用两个 nginx 系统(在不同的端口或不同的实例上):一个使用 nextcloud:fpm
就像 nextcloud 会监听 /
和一个重写 /nextcloud
(参见前面的配置)
修改 nginx 以使用 fpm 并重写它看起来非常非常痛苦,您需要确保各种重写不会重叠。我尝试了一些方法,但效果不佳,因此我自己放弃了这种方法。使用默认值的一个好处是使用来自 Nextcloud 的推荐 nginx 配置,该配置可能因版本而异。
【讨论】:
【参考方案2】:我正在使用这个设置,它工作得很好:
NGINX 反向代理:
version: '3'
services:
nginx-web:
image: nginx
labels:
com.github.jrcs.letsencrypt_nginx_proxy_companion.nginx_proxy: "true"
container_name: $NGINX_WEB
restart: always
ports:
- "$IP:80:80"
- "$IP:443:443"
volumes:
- $NGINX_FILES_PATH/conf.d:/etc/nginx/conf.d
- $NGINX_FILES_PATH/vhost.d:/etc/nginx/vhost.d
- $NGINX_FILES_PATH/html:/usr/share/nginx/html
- $NGINX_FILES_PATH/certs:/etc/nginx/certs:ro
- $NGINX_FILES_PATH/htpasswd:/etc/nginx/htpasswd:ro
nginx-gen:
image: jwilder/docker-gen
command: -notify-sighup $NGINX_WEB -watch -wait 5s:30s /etc/docker-gen/templates/nginx.tmpl /etc/nginx/conf.d/default.conf
container_name: $DOCKER_GEN
restart: always
volumes:
- $NGINX_FILES_PATH/conf.d:/etc/nginx/conf.d
- $NGINX_FILES_PATH/vhost.d:/etc/nginx/vhost.d
- $NGINX_FILES_PATH/html:/usr/share/nginx/html
- $NGINX_FILES_PATH/certs:/etc/nginx/certs:ro
- $NGINX_FILES_PATH/htpasswd:/etc/nginx/htpasswd:ro
- /var/run/docker.sock:/tmp/docker.sock:ro
- ./nginx.tmpl:/etc/docker-gen/templates/nginx.tmpl:ro
nginx-letsencrypt:
image: jrcs/letsencrypt-nginx-proxy-companion
container_name: $LETS_ENCRYPT
restart: always
volumes:
- $NGINX_FILES_PATH/conf.d:/etc/nginx/conf.d
- $NGINX_FILES_PATH/vhost.d:/etc/nginx/vhost.d
- $NGINX_FILES_PATH/html:/usr/share/nginx/html
- $NGINX_FILES_PATH/certs:/etc/nginx/certs:rw
- /var/run/docker.sock:/var/run/docker.sock:ro
environment:
NGINX_DOCKER_GEN_CONTAINER: $DOCKER_GEN
NGINX_PROXY_CONTAINER: $NGINX_WEB
networks:
default:
external:
name: $NETWORK
这是为 Nextcloud 容器设置的:
version: '3'
services:
cloud-db:
container_name: $DB_CONTAINER_NAME
image: mariadb:latest
restart: unless-stopped
volumes:
- $LOCAL_DB_DIR:/var/lib/mysql
environment:
MYSQL_DATABASE: $MYSQL_DATABASE
MYSQL_ROOT_PASSWORD: $MYSQL_ROOT_PASSWORD
MYSQL_USER: $MYSQL_USER
MYSQL_PASSWORD: $MYSQL_PASSWORD
cloud-app:
depends_on:
- cloud-db
container_name: $APP_CONTAINER_NAME
image: nextcloud:latest
restart: unless-stopped
volumes:
- $LOCAL_DATA_DIR:/var/www/html
- $LOCAL_CONF_DIR:/var/www/html/config
- $LOCAL_APPS_DIR:/var/www/html/apps
environment:
NEXTCLOUD_ADMIN_USER: $NEXTCLOUD_ADMIN_USER
NEXTCLOUD_ADMIN_PASSWORD: $NEXTCLOUD_ADMIN_PASSWORD
NEXTCLOUD_DATA_DIR: $NEXTCLOUD_DATA_DIR
NEXTCLOUD_TABLE_PREFIX: $NEXTCLOUD_TABLE_PREFIX
VIRTUAL_HOST: $VIRTUAL_HOST
LETSENCRYPT_HOST: $LETSENCRYPT_HOST
LETSENCRYPT_EMAIL: $LETSENCRYPT_EMAIL
MYSQL_DATABASE: $MYSQL_DATABASE
MYSQL_USER: $MYSQL_USER
MYSQL_PASSWORD: $MYSQL_PASSWORD
MYSQL_HOST: $MYSQL_HOST
networks:
default:
external:
name: $NETWORK
都是从这里长大的:
Docker-Compose-LetsEncrypt-Nginx-Proxy-Companion
Docker-Nextcloud-LetsEncrypt
编辑:
正如您提到的FPM
图像,您必须为 Nextcloud 容器添加以下环境选项:
environment:
[..]
VIRTUAL_ROOT: /var/www/html
VIRTUAL_PROTO: fastcgi
或者您可以按照 Nextcloud 示例:
https://github.com/nextcloud/docker/tree/master/.examples/docker-compose/with-nginx-proxy/mariadb/fpm
【讨论】:
以上是关于在 nginx/nginx-proxy 容器中使用 Nextcloud 以及其他服务的主要内容,如果未能解决你的问题,请参考以下文章
在 Jenkins Docker 代理中使用测试容器:容器无法启动,NoRouteToHostException