简体   繁体   English

nginx和wordpress不同的docker-compose文件

[英]nginx and wordpress different docker-compose files

I am trying to connect multiple docker-compose files with one nginx docker-compose file. 我正在尝试将多个docker-compose文件与一个nginx docker-compose文件连接。

For example, this setup works great(I've create the network before running): 例如,此设置的效果很好(在运行之前,我已经创建了网络):

(Bear with me here. I know this example is redundant because I have two nginx servers) (在这里忍受我。我知道这个例子是多余的,因为我有两个nginx服务器)

#static_site/docker-compose.yml
version: '3'

services:
  web:
    image: nginx:stable-alpine
    container_name: app_web_static
    volumes:
      - "./nginx.conf:/etc/nginx/conf.d/default.conf"
      - "./web:/var/www/static_web"
    ports:
      - "8080:8080"
    networks:
      - test_network

#static_site/nginx.conf
server {
    listen 8080;

    location / {
       root /var/www/static_web;
       index  index.html index.htm;
    }
}

#nginx/docker-compose.yml
server {
    listen 80;
    server_name myapp.local;

    #Docker DNS
    resolver 127.0.0.11;

    location / {
        set $docker_host "app_web_static"; 
        proxy_pass http://$docker_host:8080;
    }
}

I run the containers and everything works as expected. 我运行容器,一切正常。 My website is proxied over myapp.local 我的网站通过myapp.local代理

BUT. 但。 When I change my static website to a wordpress image like this: 当我将静态网站更改为这样的wordpress图片时:

#wordpress/docker-composer.yml
wordpress:
    image: wordpress
    container_name: app_web
    links:
      - db:mysql
    ports:
      - "8080:80"
    restart: always
    environment:
      WORDPRESS_DB_PASSWORD: example
    networks:
      - test_network
      - test_wordpress_db

  db:
    image: mysql:5.7
    container_name: test.web.db
    ports:
      - 3306:3306
    environment:
      MYSQL_ROOT_PASSWORD: example
    volumes:
      - ./mysql/data:/var/lib/mysql
    networks:
      - test_wordpress_db

I run the containers again(after updating the nginx .yml file). 我再次运行容器(更新nginx .yml文件之后)。 I get the request logged in my terminal (nginx container which means the request is handle) but as an error : test.nginx | [error] 5#5: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: myapp.local, request: "GET / HTTP/1.1", upstream: "http://172.23.0.2:8080/", host: "myapp.local" 我将请求登录到我的终端(nginx容器,这意味着该请求已处理),但出现错误: test.nginx | [error] 5#5: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: myapp.local, request: "GET / HTTP/1.1", upstream: "http://172.23.0.2:8080/", host: "myapp.local" test.nginx | [error] 5#5: *6 connect() failed (111: Connection refused) while connecting to upstream, client: 172.23.0.1, server: myapp.local, request: "GET / HTTP/1.1", upstream: "http://172.23.0.2:8080/", host: "myapp.local"

The thing is that the wordpress app is working when I access it from localhost:8080 . 问题是当我从localhost:8080访问它时,wordpress应用程序正在运行。

Is there a problem with the wordpress app in this configuration? 此配置中的wordpress应用是否存在问题? It cannot detect my host or something? 它无法检测到我的主机或其他东西吗? How can I solve this problem? 我怎么解决这个问题?

I figured it out. 我想到了。

It seems I have to use the internal port for my proxy_pass not the external one. 看来我必须为proxy_pass使用内部端口,而不是外部端口。 For example, for the wordpress docker-compose.yml file I have to write the proxy like so: 例如,对于wordpress docker-compose.yml文件,我必须这样编写代理:

#nginx/docker-compose.yml
server {
    listen 80;
    server_name myapp.local;

    #Docker DNS
    resolver 127.0.0.11;

    location / {
        set $docker_host "app_web_static"; 
        proxy_pass http://$docker_host:80; # here. the internal wordpress port
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM