反向代理到WordPress容器

时间:2016-12-09 11:11:26

标签: wordpress nginx docker docker-compose

我正在尝试通过Docker创建一个WordPress容器。但我在NGINX级别安装了MySQLLet's Encrypthost

所以,我的Docker Compose文件是:

wordpress:
  image: wordpress:latest
  restart: always
  net: "host"
  environment:
      WORDPRESS_DB_HOST: 172.17.0.1:3306
      WORDPRESS_DB_NAME: DB_NAME
      WORDPRESS_DB_USER: DB_USER
      WORDPRESS_DB_PASSWORD: DB_PASSWORD
  ports:
      - "8080:8080"
  volumes:
      - /home/ubuntu/wordpress:/var/www/html

我的NGINX配置是:

server {
    listen                                      80 443 ssl http2;

    server_name                                 domain.com www.domain.com;

    # some headers

    ssl                                         on;
    # some ssl settings

    gzip                                        on;
    # some gzip settings

    # pagespeed and let's encrypt locations

    location / {
        if ($scheme = http) {
            return 301                          https://$server_name$request_uri;
        }

        # proxy pass headers

        proxy_pass                              http://172.17.0.1:8080;
    }
}

最后,我的ifconfig输出为:

docker0   Link encap:Ethernet  HWaddr 02:24:ad:a2:e8:dd  
          inet addr:172.17.0.1  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::24:adff:fea2:e8dd/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:324 errors:0 dropped:0 overruns:0 frame:0
          TX packets:334 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:20589 (20.5 KB)  TX bytes:10261280 (10.2 MB)

但是当我打开domain.com时,我被https://domain.com重定向到502 Bad Gateway

docker-compose logs的最后一项输出:

Attaching to ubuntu_wordpress_1
wordpress_1  | (98)Address already in use: AH00072: make_sock: could not bind to address [::]:80
wordpress_1  | (98)Address already in use: AH00072: make_sock: could not bind to address 0.0.0.0:80
wordpress_1  | no listening sockets available, shutting down
wordpress_1  | AH00015: Unable to open logs

所以,我正在尝试的是通过Docker容纳WordPress,每当有80端口的请求时,将其重定向到Docker容器。

限制:

  • 我不想Dockerize MySQL,那是最后的。
  • 我不想Dockerize NGINX,因为我个人喜欢使用http2pagespeed模块从源代码构建它。

2 个答案:

答案 0 :(得分:1)

我想问的是网络配置问题。您的容器使用端口80进行wordpress,而您的主机使用相同的端口进行nginx。当您使用net:“host”(https://docs.docker.com/engine/reference/run/#/network-settings)时,您的容器使用主机的网络堆栈,并且它们试图绑定到同一端口时发生冲突。

如果您的mysql服务器是远程可访问的,只需删除net:“host”部分并将mysql主机配置替换为远程连接它的方式。它应该工作。

如果由于任何原因无法将mysql打开到远程连接,那么我猜你应该创建一个叫做用户定义网络的东西。它也可以通过docker-compose.yml文件配置。 (https://docs.docker.com/compose/networking/#specifying-custom-networks

答案 1 :(得分:0)

这对我有用

version: '3.1'

services:
  db:
    image: mysql:5.7
    volumes:
      - ./db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress
  wordpress:
    depends_on:
      - db
    image: wordpress:4.8.2-apache
    volumes:
      - ./wordpress:/var/www/html
      - ./uploads.ini:/usr/local/etc/php/conf.d/uploads.ini
    restart: always
    environment:
      - VIRTUAL_HOST=blog.dom.local
      - WORDPRESS_DB_HOST=db:3306
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - "80:80"
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro