2012-07-11 58 views
0

我試圖在nginx.conf上用多個分離的php-fpm服務器負載均衡上游塊,在AmazonEC2上運行php5。我正在測試兩個t1.micro實例,但當我嘗試加載php文件時,在我的瀏覽器上收到502 Bad Gateway錯誤。 (靜態HTML文件工作正常,但不能讓PHP文件工作。)在ec2上運行多php-fpm服務器實例運行nginx時出現連接錯誤

這是我的nginx錯誤日誌。

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「

有時我會得到這個。

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」

我花時間從ec2 sequrity groups/iptables打開9000端口,同時在nginx和php-fpm上聲明本地IP地址,所以我認爲這不是問題。 (我曾經有連接拒絕錯誤日誌)

任何人都可以幫助我? 以下是我的服務器設置和首選項。

[實例1]

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

[實例2]

  • t1.micro CentOS的6.2 .2
  • PHP 5.3.14(fpm-fcgi)帶有eAccelerator的Zend Engine v2.3.0 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 
+0

什麼是日誌?可能是你的PHP腳本是segfaulting。 – VBart 2012-07-11 13:39:29

+0

日誌for php-fpm?我沒有收到日誌級別爲「通知」的php-fpm錯誤日誌。我能夠從CLI運行php文件,但是仍然有可能發生段錯誤? – 2012-07-11 15:49:53

+0

您的PHP服務器上的document_root與您的Nginx服務器上的不一樣嗎? – 2012-07-11 18:24:47

回答

0
最後

我已經明白了這一點!

我仍然不確定爲什麼,但通過在我的每個實例上分配彈性IP,並使用私有IP而不是私有DNS地址解決了我的問題。

所以我的conf文件現在看起來像這樣以防萬一。

[nginx.conf]

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

[PHP-fpm.d/www.conf]

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

謝謝!

相關問題