Ali Gonabadi
Ali Gonabadi

Reputation: 944

Test load balancing on single server with nginx and IIS

I want to test load balancing on single server with nginx and IIS. I set nginx to listen to localhost:90 and two websites on IIS to listen localhost:81 and localhost:82

This is my nginx.conf:

events {
worker_connections  1024;
}

http {
  upstream backend {
    server localhost:81;
    server localhost:82;
  }

  server {
    listen 90;
    server_name backend;
    location / {
      proxy_pass http://localhost:80;
    }
  }
}

When I open http://localhost:90 on browser it returns 504 Gateway Time-out.

In fact requests will send to port 80 (which I set in proxy_pass) not port 81 and 82

I know load balancing is for when we have multiple server. But is there any way to test load balancing on single server with multiple ports?

Upvotes: 2

Views: 1606

Answers (1)

Farhad Farahi
Farhad Farahi

Reputation: 39407

Change your config to this:

events {
worker_connections  1024;
}

http {
  upstream backend {
    server localhost:81;
    server localhost:82;
  }

  server {
    listen 90;
    server_name _;
    location / {
      proxy_pass http://backend;
    }
  }
}

Now open localhost on port 90

Explaination:

To use the upstream specified in http block you need to use its name.

proxy_pass http://backend;

Its also a good practice to set following headers when doing reverse proxy (so backend get client information instead of reverse proxy server):

proxy_set_header   X-Real-IP        $remote_addr;
proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
proxy_set_header   X-Forwarded-User  $remote_user;

Upvotes: 1

Related Questions