题 Docker + nginx + Php-FPM 502 Bad Gateway


我正在尝试通过docker安装php-fpm和nginx,我有一个nginx的问题,它返回一个502错误的网关错误,但是当我尝试继续任何HTML文件只能正确显示。我必须对php文件做什么才能正常使用这个系统?

Nginx配置站点:

server {
        listen      80;
        server_name  api.local.dev;
        access_log  /var/log/nginx/api.access.log;
        error_log   /var/log/nginx/api.error.log;
        root        /www;
        charset     utf-8;
        index  index.php index.html index.htm;
        location / {
                index  index.php index.html index.htm;
        }

        location ~ \.php$ {
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
}

Dockerfile

FROM ubuntu:13.10

# Keep upstart from complaining
RUN dpkg-divert --local --rename --add /sbin/initctl
RUN ln -sf /bin/true /sbin/initctl

# Let the conatiner know that there is no tty
ENV DEBIAN_FRONTEND noninteractive

RUN locale-gen en_US.UTF-8
ENV LANG       en_US.UTF-8
ENV LC_ALL     en_US.UTF-8


RUN apt-get update && apt-get upgrade -y

RUN apt-get -y install nginx php5-fpm php5-mysql php-apc pwgen python-setuptools curl git unzip
RUN apt-get -y install php5-curl php5-gd php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-sqlite php5-tidy php5-xmlrpc

VOLUME  ["/var/log/nginx"]

CMD echo "127.0.0.1      api.local.dev" >> /etc/hosts
EXPOSE 80

RUN mkdir /www
RUN chown www-data:www-data -R /www
RUN echo "<?php phpinfo()  ?>" > /www/index.php
RUN cat /www/index.php


RUN mkdir /docker
ADD nginx /docker/nginx

RUN mkdir -p /var/log/nginx

RUN chown www-data:www-data /var/log/nginx
RUN sed -i -e"s/keepalive_timeout\s*65/keepalive_timeout 2/" /etc/nginx/nginx.conf
RUN sed -i -e"s/keepalive_timeout 2/keepalive_timeout 2;\n\tclient_max_body_size 100m/" /etc/nginx/nginx.conf

RUN sed -i -e "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/upload_max_filesize\s*=\s*2M/upload_max_filesize = 100M/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/post_max_size\s*=\s*8M/post_max_size = 100M/g" /etc/php5/fpm/php.ini
RUN sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" /etc/php5/fpm/php-fpm.conf
RUN sed -i -e "s/;catch_workers_output\s*=\s*yes/catch_workers_output = yes/g" /etc/php5/fpm/pool.d/www.conf
RUN cat /etc/php5/fpm/pool.d/www.conf
RUN find /etc/php5/cli/conf.d/ -name "*.ini" -exec sed -i -re 's/^(\s*)#(.*)/\1;\2/g' {} \;


RUN echo "cgi.fix_pathinfo = 0;" >> /etc/php5/fpm/php.ini

RUN cat /docker/nginx/api.local.dev >  /etc/nginx/sites-available/api.local.dev
RUN ln -s /etc/nginx/sites-available/api/local.dev /etc/nginx/sites-enabled/api/local.dev

RUN echo "daemon off;" >> /etc/nginx/nginx.conf

CMD ["nginx"]

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

5
2018-05-17 10:36




你在nginx的日志中看到了什么有趣的东西吗?在php-fpm日志中? - greg0ire
什么呢 netstat -lntp 输出? - greg0ire
Nginx日志       connect()到unix:/var/run/php5-fpm.sock失败(111:连接被拒绝)或根据我的测试失败(2:没有这样的文件或目录)连接到上游 - Flug
netstat的 活动Internet连接(仅限服务器)Proto Recv-Q Send-Q本地地址外部地址状态PID /程序名称tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN - 活动UNIX域套接字(仅限服务器)Proto RefCnt标志类型状态I节点PID /程序名称路径unix 2 [ACC] STREAM LISTENING 1820188 - /var/run/php5-fpm.sock - Flug
请勿对此使用评论,请更新您的问题。如果你有解决方案,你可以回答你自己的问题。 - greg0ire


答案:


好, 我解决了我的问题,我就是这样做的:

docker run -i -t ubuntu /bin/bash

然后我查看了表明上游php fpm服务器出现问题的nginx日志,如上所示

tail -f /var/log/nginx/*

最后我重新启动php5-fpm服务

service php5-fpm restart

所以最后我的问题是在安装容器服务期间启动但是配置错误,因此重新启动服务已重新加载php5-fpm配置


3
2018-05-19 20:36





您正在做出错误的假设,即docker容器只是另一台机器或vm。 Docker应该总是尝试只为每个容器运行一件事。然后你应该使用链接和卷来让它们一起说话。

CMD应该直接启动程序,而守护进程应该配置为在前台运行,因此不作为服务运行。最好也应该将日志转发给docker,但这有点超出我的范围(请参阅github上的官方应用程序构建)。

还要确保某些内容不是以root身份运行,因为此时Docker不提供适当的安全隔离功能。

最后,通过查看新改进的文档并学习基本知识,可以解决您的问题。


2
2017-07-21 13:31