Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have this NGINX config

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;
  }
}

Is there a way I can combine these using a variable for port. I tries using server_port variable but with no success

share|improve this question
add comment

1 Answer

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;
  }
}

That should do the trick.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.