【问题标题】:Nginx reverse proxy to wordpress in docker container through ecsNginx通过ecs反向代理docker容器中的wordpress
【发布时间】:2020-01-19 14:19:14
【问题描述】:

我正在尝试通过 Amazon ecs 实例将代理传递到在 docker 容器中设置的 http Wordpress 站点。客户端通过我们设置的测试服务器 (https://test.xxxxxxx.com) 访问站点。当用户访问https://test.xxxxxxx.com 时,我希望它在地址栏中显示https://test.xxxxxxx.com,但会打开我的Wordpress 网站的页面(端口80 上的http://xx.xxx.xxx.xxx)。

我可以把它放到我的 Wordpress 网站上,但它看起来很有趣。我收到很多混合内容错误,因为我试图通过 https 请求访问 http 文件。我了解发生了什么,但我似乎无法修复它,即使在尝试了我可以在网上找到的所有建议之后。

我尝试更改站点可用文件夹中 Nginx 文件中的几个设置,以及更改我的 Wordpress 站点上 wp-config.php 中的设置。下面是我尝试过的一件事。我发现的几乎所有教程,以及我尝试过的所有教程,都是它的变体。

#Nginx file

server {
    listen 443;

    location / {
        proxy_pass http://xx.xxx.xxx.xxx:80;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
#wp-config.php

if ($_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')
    $_SERVER['HTTPS'] = '1';

if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) {
    $_SERVER[HTTP_HOST'] = $_SERVER[HTTP_X_FORWARDED_HOST'];
}

define( 'WP_HOME', 'http://xx.xxx.xxx.xxx');
define( 'WP_SITEURL', 'http://xx.xxx.xxx.xxx');

我希望发生的是,当用户在地址栏中输入 https://test.xxxxxxx.com 时,我的 Wordpress 网站会加载正确的主题和我的所有图片,但 https://test.xxxxxxx.com 仍会显示在地址栏中。

【问题讨论】:

  • WP_HOME 和 WP_SITEURL 应设置为 https://test.xxxxxxx.com
  • 我试过这个,但是当我转到那个页面时,它说 test.xxxxxxx.com 目前无法处理这个请求。 HTTP 错误 500

标签: wordpress docker nginx amazon-ecs nginx-reverse-proxy


【解决方案1】:

我想建议您在 ECS 中使用 HA-Proxy 反向代理。 我尝试了 nginx 反向代理,但失败了。以及 HA-Proxy 的成功。 比nginx配置更简单。

首先,使用 Docker 的“链接”选项并设置“环境变量”(例如 LINK_APP、LINK_PORT)。

其次,将这个“环境变量”填入haproxy.cfg。

另外,我建议您使用“动态端口映射”到 ALB。它使工作更加灵活。

taskdef.json:

# taskdef.json

{
    "executionRoleArn": "arn:aws:iam::<AWS_ACCOUNT_ID>:role/<APP_NAME>_ecsTaskExecutionRole",
    "containerDefinitions": [
      {
        "name": "<APP_NAME>-rp",
        "image": "gnokoheat/ecs-reverse-proxy:latest",
        "essential": true,
        "memoryReservation": <MEMORY_RESV>,
        "portMappings": [
          {
            "hostPort": 0,
            "containerPort": 80,
            "protocol": "tcp"
          }
        ],
        "links": [
          "<APP_NAME>"
        ],
        "environment": [
          {
            "name": "LINK_PORT",
            "value": "<SERVICE_PORT>"
          },
          {
            "name": "LINK_APP",
            "value": "<APP_NAME>"
          }
        ]
      },
      {
        "name": "<APP_NAME>",
        "image": "<IMAGE_NAME>",
        "essential": true,
        "memoryReservation": <MEMORY_RESV>,
        "portMappings": [
          {
            "protocol": "tcp",
            "containerPort": <SERVICE_PORT>
          }
        ],
        "environment": [
          {
            "name": "PORT",
            "value": "<SERVICE_PORT>"
          },
          {
            "name": "APP_NAME",
            "value": "<APP_NAME>"
          }
        ]
      }
    ],
    "requiresCompatibilities": [
      "EC2"
    ],
    "networkMode": "bridge",
    "family": "<APP_NAME>"
  }

haproxy.cfg:

# haproxy.cfg

global
    daemon
    pidfile /var/run/haproxy.pid

defaults
    log global
    mode http
    retries 3
    timeout connect 5000
    timeout client 50000
    timeout server 50000

frontend http
    bind *:80

    http-request set-header X-Forwarded-Host %[req.hdr(Host)]

    compression algo gzip
    compression type text/css text/javascript text/plain application/json application/xml

    default_backend app

backend app
    server static "${LINK_APP}":"${LINK_PORT}"

Dockerfile(haproxy):

FROM haproxy:1.7
USER root
COPY haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg

见:

Github:https://github.com/gnokoheat/ecs-reverse-proxy

Docker 镜像:gnokoheat/ecs-reverse-proxy:latest

【讨论】:

    猜你喜欢
    • 2017-11-13
    • 2021-05-04
    • 2018-03-20
    • 1970-01-01
    • 2017-03-21
    • 2019-08-31
    • 2020-02-26
    • 2020-09-10
    • 1970-01-01
    相关资源
    最近更新 更多