I’m trying to run php5 on AmazonEC2 with multi separated php-fpm servers load balanced by upstream block on 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. (Static html files are working fine, but cant get php files to work.)
Here is my nginx error logs.
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: http://www.example.com, request: “GET / HTTP/1.1”,
upstream: “fastcgi://10.xxx.xxx.xxx:9000”, host: “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: http://www.example.com, request: “GET /
HTTP/1.1”, upstream: “fastcgi://10.xxx.xxx.xxx:9000”, host:
“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. (I used to have connection refused error logs)
Could anyone help me out??
Below are my server settings and preferences.
[instance 1]
- t1.micro CentOS 6.2.2
- nginx/1.2.2
[instance 2]
- t1.micro CentOS 6.2.2
- PHP 5.3.14 (fpm-fcgi) Zend Engine v2.3.0 with eAccelerator v0.9.6
[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]
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]
[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.
so my conf files now looks like this just in case.
[nginx.conf]
[php-fpm.d/www.conf]
Thanks!