Fargate:无法从其他容器访问我的PHP容器

时间:2020-03-07 00:25:01

标签: php docker nginx amazon-ecs aws-fargate

我有一个任务,在Fargate上有两个容器:一个Java和一个php / nginx容器。

java容器将其内部端口80映射到外部端口8080,而php容器将其内部端口80映射到外部端口8081。

任务正在使用awsvpc网络模式。根据{{​​3}},一旦他们在Fargate上,他们就应该能够通过localhost相互通信。

我尝试通过使Java容器使用“ localhost:8081”向PHP容器发送请求来对此进行测试:

    public boolean isPHPReachable() {
        System.out.println("Attempting to make request to PHP");
        try {
            final URL url = new URL("http://localhost:8081/test");
            final HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            final int status = con.getResponseCode();
            System.out.printf("Get request to PHP resulted in a %d: %s", status, con.getResponseMessage());
            return status == 200;
        } catch (final IOException err) {
            err.printStackTrace();
            return false;
    }

它应该被击中的php代码是这样的:

<?php

if ($_GET['q'] === 'test') {
    echo('healthy');
    die;
}

“ q”由nginx设置(请参见下面的配置)。

但是,当Java尝试向PHP发送请求时,我看到cloudwatch日志显示错误:

22:55:04
Attempting to make request to PHP
22:55:04
java.net.ConnectException: Connection refused (Connection refused)

我试图直接联系PHP容器,但是超时。当我尝试使用Java时不会发生这种情况。

潜在问题1:Nginx配置错误

我怀疑这可能是nginx问题。但是,当我在本地计算机上创建PHP容器时,我无法重现该问题-当我ping localhost:8081时可以访问。

仅在Fargate中出现问题。

这是我的配置:

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        # Reject requests not matching any server block
        listen      80 default_server;
        server_name "";
        return      410;
    }

    server {
        listen 80;

        root /var/www/public_html;

        server_name localhost;

        index index.php;
        set_real_ip_from 0.0.0.0/0; # we trust that the webserver is firewalled
        real_ip_header  X-Forwarded-For;

        set $webroot $document_root;

        error_page 404 410 /404_empty.html;
        autoindex off;

        # Disable server signature
        # (i.e. remove "nginx 1.10" or similar from error pages and response headers)
        server_tokens off;

        #################
        # PHP locations #
        #################
        location / {
            rewrite ^/(.*)$ /sites/all/index.php?q=$1;
            error_page 404 /sites/all/index.php;
        }
}

2。配置错误的安全组:

我认为情况并非如此。附加到此容器的安全组允许从任何目标到任何端口的入站TCP请求。它还允许所有出站请求。

有人发现眼前的问题吗?有调试提示吗?

0 个答案:

没有答案