Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在nginx中设置反向代理时,是否可以基于上游模块中的浏览器cookie将服务器参数作为变量?_Nginx_Nginx Reverse Proxy_Nginx Config - Fatal编程技术网

在nginx中设置反向代理时,是否可以基于上游模块中的浏览器cookie将服务器参数作为变量?

在nginx中设置反向代理时,是否可以基于上游模块中的浏览器cookie将服务器参数作为变量?,nginx,nginx-reverse-proxy,nginx-config,Nginx,Nginx Reverse Proxy,Nginx Config,我正在使用上游和服务器模块设置用于路由/负载分配的nginx反向代理,在这些模块中,相同的URL应该代理到运行Apache的5个不同服务器,而不更改浏览器中的URL,具体取决于cookie的值。如何在上游完成服务器参数,以根据cookie值传递变量 我尝试将cookie值映射到需要代理的服务器,然后将服务器ip用作“server_name”参数中的变量。我试图在上游使用相同的in“server”参数,但是nginx不支持这一点。我不想把所有5台服务器都放在上游,因为nginx正试图在它们之间实现

我正在使用上游和服务器模块设置用于路由/负载分配的nginx反向代理,在这些模块中,相同的URL应该代理到运行Apache的5个不同服务器,而不更改浏览器中的URL,具体取决于cookie的值。如何在上游完成服务器参数,以根据cookie值传递变量

我尝试将cookie值映射到需要代理的服务器,然后将服务器ip用作“server_name”参数中的变量。我试图在上游使用相同的in“server”参数,但是nginx不支持这一点。我不想把所有5台服务器都放在上游,因为nginx正试图在它们之间实现负载平衡,这不是这里的目标。目标是根据cookie值将负载分配到特定服务器

nginx.conf文件

http {
include /etc/nginx/conf.d/*.conf;

upstream su.la.com {
  server 100.0.8.2:443; 
}
}

    #The above works, making this $suhost, does not work.Any alternative 
    for this will be helpful.

    suhosts.conf file under /etc/nginx/conf.d

map $cookie_su_ID $suhost {
    default 100.0.8.2;
    1     100.0.8.2;
    3     100.0.8.6;
}

server {

  set $bypass 0;
  if ($remote_addr ~ "^(127.0.0.1)$") {
    set $bypass $http_secret_header;
  }

  listen 80;
  server_name su.la.com;

  return 301 https://$host$request_uri;
}

  server {

  set $bypass 0;
  if ($remote_addr ~ "^(127.0.0.1)$") {
    set $bypass $http_secret_header;
  }

  listen 443 ssl;
  server_name $suhost;
      ssl_certificate /etc/nginx/certs/client.crt;
      ssl_certificate_key /etc/nginx/certs/client.private.key;

    location / {
            proxy_pass https://su.la.com;
            proxy_cache_bypass $bypass;
      }
 }
预期成果: 根据上面描述的映射函数,如果su_id=1,上游$suhost中的服务器参数应取为100.0.8.2,并应路由到100.0.8.2,浏览器中无任何更改

实际结果:
nginx不允许服务器参数使用变量,因此需要其他替代方案或方法来实现这一点。如果为单个服务器的服务器参数指定了一个值,则此操作有效。我们需要根据cookie值使其能够路由到多台服务器,一次路由到一台服务器。

有没有人遇到过这个问题或可以提供帮助?有没有人遇到过这个问题或可以提供帮助?