【问题标题】:Nginx & php-fpm - host unreachableNginx & php-fpm - 主机无法访问
【发布时间】:2018-06-05 20:02:45
【问题描述】:

我有一个包含一个 nginx 和一个 php-fpm 服务的 docker swarm。我的问题是,从 swarm 中的其他服务中,我随机收到错误 Failed to connect to nginx-fpm port 8081: Host unreachable

nginx 和 fpm 镜像来自官方 docker 镜像,配置更改很少。

nginx-fpm dockerfile

FROM nginx:1.13.12-alpine

COPY ./nginx/config/ /etc/nginx/
ADD ./nginx/docker/entrypoint.sh /bin/

EXPOSE 8081
ENTRYPOINT ["/bin/entrypoint.sh"]
CMD ["nginx", "-g", "daemon off;"]

nginx 配置

user  nginx;
worker_processes auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    accept_mutex on;
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  65;

    server {
        listen 8081;
        server_name worker;

        keepalive_timeout 30;
        send_timeout 30s;
        fastcgi_read_timeout 30s;
        client_max_body_size 1024M;

        root /app/www;
        index index.php;

        location / {
            try_files $uri $uri/ /index.php?$args;
        }

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php-fpm:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_buffers 16 16k;
            fastcgi_buffer_size 32k;
        }
    }
}

我尝试了启用 nginx_status 的事件,但我只能看到 1 个活动连接(或主机无法访问)。

在我看来,nginx 一次只能处理一个连接,但我找不到原因...感谢任何帮助

【问题讨论】:

    标签: php docker nginx


    【解决方案1】:

    你是如何在 Swarm 中运行你的服务的?

    一般情况下,需要对外暴露服务端口,如下:

    $ docker service create --name my_service \
                            --replicas 3 \
                            --publish published=8081,target=8081 \
                            nginx-fpm
    

    【讨论】:

    • 仅通过EXPOSE 公开。我不需要从外部连接到这个,而只需要从群中的其他服务。大约一半时间我立即得到响应,其他时间主机无法访问......
    • 你的 swarm 在单台机器上运行?
    • 它在两台服务器上
    • 这就是为什么您需要发布以便不在8081 上收听的人可以向8081 上收听的人发出请求。监听8081 的服务器可以在本地同一个容器网络命名空间发出请求。
    • 如果是这样的话,我根本无法连接,对吧?我可以从同一个容器连接,不管它在哪台服务器上,但有时我会得到响应,有时我会无法访问主机......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-12
    • 1970-01-01
    • 2021-11-18
    • 2015-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多