• 鱼
    2019-03-27
    老师说即使服务器挂了,也要把请求转到8012的服务器上。这样要做什么额外的操作吗?因为在看Nginx文档的时候,写的是:

    > The method ensures that requests from the same client will always be passed to the same server except when this server is unavailable. In the latter case client requests will be passed to another server. Most probably, it will always be the same server as well.

    个人理解是8012的服务不可用了,请求是会被转到其他服务上去。
    在`nginx version: nginx/1.14.2`操作的时候,也是发现会被转到其他服务上。
    展开

    作者回复: 不需要,对于服务器挂了后导致的超时,默认是自动开启next upstream功能的,参见proxy_next_upstream指令的默认值:
    Default:proxy_next_upstream error timeout;

    
     1
  • liu
    2019-01-25
    老师好,请教一个问题,我们通常为了使用更多的文件描述符在nginx.conf中配置如下:
    worker_rlimit_nofile 131070; ##注意是65535的2倍!
    我centos中/etc/security/limits.conf 文件中有如下的设置:
    * soft nofile 65535
    * hard nofile 65535

    * soft nproc 65535
    * hard nproc 65535
    这样的话,一个nginx进程最多使用65535个文件描述符,
    我在nginx.conf中加入的worker_rlimit_nofile 131070; 有效吗?
    莫非真的能超过ulimit -n的限制?
    还是为了使得worker_rlimit_nofile 131070; 有效,limits.conf中也要设置的不比该数值小?
    谢谢!
    展开

    作者回复: Linux上设置最大描述符数量,有三个维度:全局、用户、进程,其中自上而下的关系中,进程自然不能超过用户和全局的限制。如果超过了,会以最小的为准。请参见第124课。

    
     1
  • Panda
    2019-01-14
    ip_hash 适合有状态的服务

    作者回复: 是的

    
     1
  • 明明懂
    2019-11-13
    老师,我在 upstream 所配置的服务器中返回加了 $remote_addr,可是每次返回的值都是 127.0.0.1,我的配置是这样的:
    server {
        listen 8011;
        default_type text/plain;
        return 200 '8011 server response realip is $remote_addr $realip_remote_addr.\n';
    }

    server {
        listen 8012;
        default_type text/plain;
        return 200 '8012 server response realip is $remote_addr $realip_remote_addr .\n';
    }

    server {
        listen 80;
        set_real_ip_from 122.51.137.225; # 结合课程50,设置可信地址
        set_real_ip_from 127.0.0.1;
        real_ip_recursive on;
        real_ip_header X-Forwarded-For;
        error_log logs/myiphash.log info;
        access_log logs/upstream_access.log varups;

        location / {
            proxy_pass http://iphashs;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
        }
    }

    执行路由:curl 127.0.0.1 -H "X-Forwarded-For:4.4.4.4"
    返回:8011 server response realip is 127.0.0.1 127.0.0.1.

    执行:curl 127.0.0.1:80 -H "X-Forwarded-For:120.88.88.88"
    返回:8012 server response realip is 127.0.0.1 127.0.0.1 .

    麻烦老师有空帮忙解答下
    展开

    作者回复: 1、抓包看下,发给upstream的HTTP请求中,X-Forwarded-For:的值是什么,TCP的source ip address是什么;
    2、在upstream上的set_real_ip_from必须匹配source ip address,才能把X-Forwarded-For中的IP作为$remote_addr取出来

    
    
  • 史秀玉
    2019-10-17
    老师,ip_hash算法中113和6271是什么意思呢

    作者回复: 联想不出这两个数字的出处,能再详细点吗?

    
    
  • Carey
    2019-06-24
    老师,我的iphash没有生效:
    log_format varups '$upstream_addr $upstream_connect_time $upstream_header_time $upstream_response_time'
                            '$upstream_response_length $upstream_bytes_received '
                            '$upstream_status $upstream_http_server $upstream_cache_status';

    upstream iphashups{
            ip_hash;
            server 127.0.0.1:8011 weight=2 max_conns=2 max_fails=2 fail_timeout=5;
            server 127.0.0.1:8012;
    }

    server{
            set_real_ip_from 221.239.129.176; #主机ip
            real_ip_recursive on;
            real_ip_header X-Forwarded-For;
            server_name rrups.hiroop.com;#在/etc/hosts有映射:127.0.0.1 rrups.hiroop.com
            error_log logs/iphash.log debug;
            access_log logs/upstream_access.log varups;

            location /{
                    proxy_buffering off;
                    proxy_pass http://iphashups;
                    proxy_http_version 1.1;
                    proxy_set_header Connection "";
            }
    }

    访问效果:
    [root@linuxprobe conf]# curl -H 'X-Forwarded-For:100.200.200.100' rrups.hiroop.com
    8012 server response
    [root@linuxprobe conf]# curl -H 'X-Forwarded-For:100.200.200.100' rrups.hiroop.com
    8011 server response
    [root@linuxprobe conf]# curl -H 'X-Forwarded-For:100.200.200.100' rrups.hiroop.com
    8011 server response
    [root@linuxprobe conf]# curl -H 'X-Forwarded-For:100.200.200.100' rrups.hiroop.com
    8012 server response
    看日志文件logs/iphash.log和logs/upstream_access.log,是空的。为什么呢??
    展开

    作者回复: 如果error和access日志为空,说明server {}块未进入,原因很可能是被其他server {}匹配中了curl请求。除了经验式解决外,你可以尝试打开全部debug日志 (包括http{}下的),看看这条请求被哪个server/location匹配中处理了。

    
    
  • 初音未来
    2019-05-20
    老师,
    使用ip_hash模式,hash表记录了每个客户端IP和后端服务器的对应关系,客户端每次都访问都是同一个后端服务器响应,是这样的吗

    作者回复: 是的,除非某台后端服务器宕机

    
    
我们在线,来聊聊吧