zoukankan      html  css  js  c++  java
  • nginx负载均衡向后台传递參数方法(后端也是nginxserver)

    做了一个站点是用nginx 做的负载均衡。后端也是多个nginxserver

    遇到了一个问题。当做SSL支持时 前端nginx分发到 后端nginx后就成 http形式了(这样后台php用$_SERVER[HTTPS] == "on" 就无法推断了,但后台还必需要知道这个參数)。假设改成https的话。还需要多个证书。

    所以就想让前端nginx 假设是 https 的话给后端传递一个參数。

    用在后台来推断和使用。

    首先前端nginx的 nginx.conf 文件里 用  proxy_set_header 设置一个參数urlprefix 值为 https(參数名最好避免使用下划线和中划线,下划线的參数会被nginx忽略掉,而中划线的參数会被nginx处理为下划线。为了避免混乱我们干脆就不用这俩个,他爱咋处理咋处理) 例如以下:

        location / {
            proxy_pass              http://myserver;
            proxy_set_header        X-Real-IP $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header        Host $http_host;
            proxy_set_header        urlprefix https;
        }

    然后 后端nginx server 配置中 开启读取header 和 设置php參数

    nginx.conf 文件里加入    underscores_in_headers on;

    继续

    设置php 參数,能够使用$_SERVER["..."]来获取的哦

     location ~ .php$ {
            root           htmll;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            fastcgi_param  URL_PREFIX   $http_urlprefix;
            fastcgi_connect_timeout 300;

            include fastcgi.conf;

    }

    解释:

      fastcgi_param  URL_PREFIX   $http_urlprefix;(前端设置的參数名是urlprefix,这里读取的是http_urlprefix,这是由于nginx在处理參数时全部參数名前面都给加了http_ ,另外fastcgi_param  URL_PREFIX 这个參数名,在php读取时不须要加不论什么前缀。并且大写和小写敏感,一定要注意。

    那么后台php 就能够用 $_SERVER['URL_PREFIX']  取出来我们前端设置的參数了。

    if (isset($_SERVER['URL_PREFIX']) && $_SERVER['URL_PREFIX'] == "https"){
        //巴拉巴扒拉
    }

  • 相关阅读:
    "Automation 服务器不能创建对象" 的解决方法
    让DataGrid拥有单击回传事件并带回指定字段的值
    SQL 和TSQL学习(一)
    数据写入XML
    DATALIST分页存储过程
    The target assembly contains no service types. You may need to adjust the Code Access Security policy of this assembly." 目标程序集不包含服务类型。可能需要调整此程序集的代码访问
    字符串转日期
    C# 日期和时间正则表达式
    js根据输入日期显示该月的最后一天的日期[转]
    JavaScript试题【解析+答案】
  • 原文地址:https://www.cnblogs.com/lytwajue/p/6892826.html
Copyright © 2011-2022 走看看