端口相关的 NGINX 代理配置

端口相关的 NGINX 代理配置

我有这个 NGINX 配置

upstream example-servers-80 {
    server 192.168.10.5:80;
    server 192.168.10.6:80;
}

upstream example-servers-8080 {
    server 192.168.10.5:8080;
    server 192.168.10.6:8080;
}

server {
  server_name  fb.example.com;
  listen 80;

  location /  {
    proxy_set_header Host             $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://example-servers-80;
  }
}

server {
  server_name  fb.example.com;
  listen 8080;

  location /  {
    proxy_set_header Host             $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://example-servers-8080;
  }
}

有没有办法使用端口变量来组合它们?我尝试使用 server_port 变量,但没有成功

答案1

upstream example-servers-80 {
    server 192.168.10.5:80;
    server 192.168.10.6:80;
}

upstream example-servers-8080 {
    server 192.168.10.5:8080;
    server 192.168.10.6:8080;
}

server {
  server_name  fb.example.com;
  listen 80;
  listen 8080;
  location /  {
    proxy_set_header Host             $host;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://example-servers-$server_port;
  }
}

这应该够了吧。

相关内容