简体   繁体   English

在EC2上使用多个php-fpm服务器实例运行nginx时出现连接错误

[英]connection error on running nginx with multi php-fpm server instance on ec2

I'm trying to run php5 on AmazonEC2 with multi separated php-fpm servers load balanced by upstream block on nginx.conf. 我正在尝试通过多个分离的php-fpm服务器在AmazonEC2上运行php5,并通过nginx.conf上的上游块进行负载均衡。 I'm testing with two t1.micro instances, but getting 502 Bad Gateway error on my browser when I try loading php files. 我正在测试两个t1.micro实例,但是当我尝试加载php文件时,在浏览器上出现502 Bad Gateway错误。 (Static html files are working fine, but cant get php files to work.) (静态html文件工作正常,但无法使php文件正常工作。)

Here is my nginx error logs. 这是我的Nginx错误日志。

2012/07/11 12:28:21 [error] 18626#0: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: www.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://10.xxx.xxx.xxx:9000", host: "www.example.com" 2012/07/11 12:28:21 [错误] 18626#0:* 1 recv()失败(104:对等连接重置),同时从上游读取响应标头,客户端:xxx.xxx.xxx.xxx,服务器: www.example.com,请求:“ GET / HTTP / 1.1”,上游:“ fastcgi://10.xxx.xxx.xxx:9000”,主机:“ www.example.com”

and sometimes I get this. 有时候我明白了

2012/07/11 13:25:51 [error] 1157#0: *4 upstream prematurely closed connection while reading response header from upstream, client:xxx.xxx.xxx.xxx, server: www.example.com, request: "GET / HTTP/1.1", upstream: "fastcgi://10.xxx.xxx.xxx:9000", host: "www.example.com" 2012/07/11 13:25:51 [错误] 1157#0:* 4上游过早关闭连接,同时从上游读取响应标头,客户端:xxx.xxx.xxx.xxx,服务器:www.example.com,请求: “ GET / HTTP / 1.1”,上游:“ fastcgi://10.xxx.xxx.xxx:9000”,主机:“ www.example.com”

I spent time on opening 9000 port from ec2 sequrity groups/iptables and also declaring local ip addresses on both nginx and php-fpm so I'm thinking that's not a problem. 我花了一些时间从ec2顺序组/ iptables打开9000端口,并且还在nginx和php-fpm上声明了本地ip地址,所以我认为这不是问题。 (I used to have connection refused error logs) (我曾经有连接被拒绝的错误日志)

Could anyone help me out?? 有人可以帮我吗? Below are my server settings and preferences. 以下是我的服务器设置和首选项。

[instance 1] [实例1]

  • t1.micro CentOS 6.2.2 t1.micro CentOS 6.2.2
  • nginx/1.2.2 nginx / 1.2.2

[instance 2] [实例2]

  • t1.micro CentOS 6.2.2 t1.micro CentOS 6.2.2
  • PHP 5.3.14 (fpm-fcgi) Zend Engine v2.3.0 with eAccelerator v0.9.6 PHP 5.3.14(fpm-fcgi)Zend Engine v2.3.0和eAccelerator v0.9.6

[nginx.conf] [nginx.conf]

user  nginx nginx;
worker_processes  1;
worker_rlimit_nofile 1024;
worker_priority -5;

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

events {
    multi_accept on;
    worker_connections  1024;
}


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

    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;

    keepalive_timeout  0;

    gzip  on;

    upstream apserver {
        ip_hash;
        server ip-10-xxx-xxx-xxx.ap-northeast-1.compute.internal:9000;
    }

    include /etc/nginx/conf.d/*.conf;
}

[example.conf] [example.conf]

server {
    listen       80;
    server_name  www.example.com;

    charset utf-8;
    access_log  /var/log/nginx/www.example.com.access.log  main;
    error_log   /var/log/nginx/www.example.com.error.log  debug;
    root    /var/www;

    location / {
        index   index.php index.html index.html;
        if (-f $request_filename) {
            expires max;
            break;
        }

        if (!-e $request_filename) {
            rewrite ^(.+)/index\.php/(.*)$ $1/index.php?q=$2 last;
        }
    }

        location ~ \.php$ {
        fastcgi_send_timeout  10m;
            fastcgi_read_timeout 10m;
        fastcgi_connect_timeout 10m;
        fastcgi_pass    apserver;
        fastcgi_index   index.php;
        fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
        fastcgi_param   PATH_INFO       $fastcgi_script_name;
        include     /etc/nginx/fastcgi_params;
    }
}

[php-fpm.d/www.conf] [php-fpm.d / www.conf]

[www]
listen = ip-10-xxx-xxx-xxx.ap-northeast-1.compute.internal:9000
listen.backlog = -1
listen.allowed_clients = ip-10-yyy-yyy-yyy.ap-northeast-1.compute.internal

; Tried testing with below and got the same error
;listen = 9000
;listen.allowed_clients = any

listen.owner = prod
listen.group = prod
listen.mode = 0666
user = prod
group = prod

pm = dynamic
pm.max_children = 10
pm.start_servers = 5
pm.min_spare_servers = 3
pm.max_spare_servers = 8
pm.max_requests = 500

request_terminate_timeout = 30
request_slowlog_timeout = 2
slowlog = /var/log/php-fpm/www-slow.log

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_admin_flag[expose_php] = off

Finally I've figured this out!! 终于我明白了!

I'm still not sure why but by allocating elastic ip's on each of my instances, and using the Private IP instead of Private DNS Addresses solved my problem. 我仍然不确定为什么,但是通过在每个实例上分配弹性ip,然后使用私有IP代替私有DNS地址解决了我的问题。

so my conf files now looks like this just in case. 所以我的conf文件现在看起来像这样,以防万一。

[nginx.conf] [nginx.conf]

upstream apserver {
    ip_hash;
    server 10.xxx.xxx.xxx:9000;
}

[php-fpm.d/www.conf] [php-fpm.d / www.conf]

[www]
listen = 10.xxx.xxx.xxx:9000
listen.backlog = -1
listen.allowed_clients = 10.yyy.yyy.yyy

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM