V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iguoji
V2EX  ›  程序员

nginx 负载均衡

  •  
  •   iguoji · 2017-07-13 10:29:18 +08:00 · 3212 次点击
    这是一个创建于 2694 天前的主题,其中的信息可能已经有所发展或是发生改变。

    三台服务器

    主 174 装 NGINX

    从 170,191 装 PHP

    三台服务器防火墙和 SELINUX 都已经关闭了

    主服务器装好 Nginx 后,直接访问能看到欢迎页面

    从服务器装好 php 后,直接用 PHP -S ip:port 启动,访问能看到正常页面

    三台服务器的 /home/wwwroot 目录下分别有几个相同的测试文件

    主服务器 174

    nginx/conf.d/default.conf

    upstream myServer{
        server 192.168.199.191:9000 max_fails=3 fail_timeout=10s;
        server 192.168.199.170:9000 max_fails=3 fail_timeout=10s;
    }
    server {
        listen       80;
        server_name  192.168.199.174;
        location / {
            root   /home/wwwroot;
            index  index.html index.htm;
        }
        location ~ \.php$ {
            root           /home/wwwroot;
            fastcgi_pass   myServer;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    

    从服务器 170 191

    /usr/local/etc/php-fpm.d/www.conf

    [www]
    ; 担心是用户权限问题,所以把 nobody 改成了 root
    ; 用 php-fpm -R 启动
    user = root
    group = root
    
    listen = [::]:9000
    listen.allowed_clients = any
    request_terminate_timeout = 0
    
    

    但最终结果是:

    2017/07/13 10:28:10 [error] 2199#2199: *10 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.191:9000", host: "192.168.199.174"
    2017/07/13 10:28:10 [warn] 2199#2199: *10 upstream server temporarily disabled while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.191:9000", host: "192.168.199.174"
    2017/07/13 10:28:10 [error] 2199#2199: *10 upstream prematurely closed connection while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.170:9000", host: "192.168.199.174"
    2017/07/13 10:28:10 [warn] 2199#2199: *10 upstream server temporarily disabled while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.170:9000", host: "192.168.199.174"
    

    求问,毛病出在那里?

    5 条回复    2017-07-13 11:32:20 +08:00
    iguoji
        1
    iguoji  
    OP
       2017-07-13 10:31:48 +08:00
    ```
    2017/07/13 10:31:15 [error] 2199#2199: *13 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.170:9000", host: "192.168.199.174"
    2017/07/13 10:31:15 [warn] 2199#2199: *13 upstream server temporarily disabled while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.170:9000", host: "192.168.199.174"
    2017/07/13 10:31:15 [error] 2199#2199: *13 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.191:9000", host: "192.168.199.174"
    2017/07/13 10:31:15 [warn] 2199#2199: *13 upstream server temporarily disabled while reading response header from upstream, client: 192.168.199.222, server: 192.168.199.174, request: "GET /1.php HTTP/1.1", upstream: "fastcgi://192.168.199.191:9000", host: "192.168.199.174"
    ```
    akrf
        2
    akrf  
       2017-07-13 10:43:01 +08:00
    listen = [::]:9000

    这是 ipv6 的 any ip 吧。。。用 0.0.0.0 的 ipv4 any ip 试试。。。
    iguoji
        3
    iguoji  
    OP
       2017-07-13 10:45:29 +08:00
    @akrf 原本写的是 listen = 192.168.199.xx:9000,后面发现不行才改成这样的
    oksbsb
        4
    oksbsb  
       2017-07-13 11:21:42 +08:00
    174 跑 php 修改 fastcgi_pass myServer; 指向本机试试
    iguoji
        5
    iguoji  
    OP
       2017-07-13 11:32:20 +08:00
    @oksbsb

    找到原因了,listen.allowed_clients = any,这个东西的锅。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1081 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 22:25 · PVG 06:25 · LAX 14:25 · JFK 17:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.