如何使用nginx前面两个生成两个docker容器的wordpress站点。

时间:2016-05-04 05:17:09

标签: wordpress nginx docker

我有两个wordpress实例,每个实例作为一个docker容器运行。我想通过一个nginx服务器前端/代理这两个wordpress实例。

以下是我的nginx配置文件。

upstream docker-site1 {
 server site1_site;
}

upstream docker-site2 {
 server site2_site;
}

server {
  listen         80;
  server_name    myserver.domain.com;

  proxy_set_header Host "myserver.domain.com"; # required for Docker client sake
  proxy_set_header X-Real-IP $remote_addr; #pass on real client IP
  client_max_body_size 0; # disable any limits to avoid HTTP 413 for large image uploads
  # required to avoid HTTP 411: see issue #1486 (https://github.com/dotcloud/docker/issues/1486)
  chunked_transfer_encoding on;

  location /site2 {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/pass/.site1_htpasswd;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_pass http://docker-site2/;

  }

  location / {
    auth_basic "Restricted";
    auth_basic_user_file /etc/nginx/pass/.site1_htpasswd;
    proxy_set_header Host $http_host;
    add_header HTTP_HOST $http_host;
    proxy_pass http://docker-site1;
    proxy_http_version 1.1;
  }
}

2 个答案:

答案 0 :(得分:0)

有一个nginx-proxy映像可以自动代理容器。你可以找到它here。像这样运行代理:

$ docker run -d -p 80:80 -v /var/run/docker.sock:/tmp/docker.sock:ro jwilder/nginx-proxy

然后,您必须使用环境变量

运行您的wordpress图像
$docker run -e VIRTUAL_HOST=foo.bar.com ...
$docker run -e VIRTUAL_HOST=bar.foo.com ...

或者,更好的是,在Dockerfile中添加一个ENV指令并公开端口

# My Wordpress 1 Dockerfile
ENV VIRTUAL_HOST=foo.bar.com
EXPOSE 80

答案 1 :(得分:0)

我使用反向代理和ssl做了同样的事情。首先,您将所有内容重定向到https,然后:

server {
    listen *:443 ssl;
    ssl on;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate         /etc/nginx/certs/certbundle.pem;
    ssl_certificate_key     /etc/nginx/certs/server.key;
    ssl_dhparam             /etc/nginx/certs/dhparam.pem;

    server_name myserver.domain.com;

    location /site1/ {
            proxy_pass http://127.0.0.1:8030/; #your wordpress container should have the same port

            proxy_set_header Host    $http_host;

            proxy_read_timeout       3500;
            proxy_connect_timeout    3250;

            proxy_set_header   X-Real-IP          $remote_addr;
            proxy_set_header   Host               $host;
            proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto  https;


            proxy_set_header   SSL_PROTOCOL       $ssl_protocol;
            proxy_set_header   SSL_CLIENT_CERT    $ssl_client_cert;
            proxy_set_header   SSL_CLIENT_VERIFY  $ssl_client_verify;
            proxy_set_header   SSL_SERVER_S_DN    $ssl_client_s_dn;

            auth_basic "Area 51 - No trespassing beyound this point without authentication";
            auth_basic_user_file sec/htpasswd;
    }

    location /site2/ {
            proxy_pass http://127.0.0.1:8080/; # again, change the port

            proxy_set_header Host    $http_host;

            proxy_read_timeout       3500;
            proxy_connect_timeout    3250;

            proxy_set_header   X-Real-IP          $remote_addr;
            proxy_set_header   Host               $host;
            proxy_set_header   X-Forwarded-For    $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto  https;


            proxy_set_header   SSL_PROTOCOL       $ssl_protocol;
            proxy_set_header   SSL_CLIENT_CERT    $ssl_client_cert;
            proxy_set_header   SSL_CLIENT_VERIFY  $ssl_client_verify;
            proxy_set_header   SSL_SERVER_S_DN    $ssl_client_s_dn;

            auth_basic "Area 51 - No trespassing beyound this point without authentication";
            auth_basic_user_file sec/htpasswd;
    }

在第一个网站的wp-config.php中,您应该添加以下行:

 define('WP_HOME', 'https://myserver.domain.com;/site1'); 
 define('WP_SITEURL', 'https://myserver.domain.com;/site1'); 

确保您已连接到数据库。我猜你正在为你的数据库使用另一个容器?如果是,请在wp-config.php

中相应地配置这些属性
define('DB_NAME', 'youDbName'); #default is wordpress
define('DB_USER', 'root');     
define('DB_PASSWORD', 'yourPass');    
define('DB_HOST', 'mysql'); # your db container name    
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');

没有ssl

,它应该或多或少相同

编辑:
正如ShanShan指出的那样,请确保使用-p选项和所需的端口运行容器,即站点1 -p 8030:80