Question

Configuring DNS for nginx and confirming load balancing.

Hi, I’m testing out nginx round-robin load balancing and am fairly new to this.

1)Can I configure DNS so that I can access the webserver via a domain name instead of IP? 2)How can I check if load balancing is working? I have 2 web servers and want to know which server gets the request when a client sends the request.

This is my nginx code(very basic)

    upstream web_backend {
    server 10.11.12.51;
    server 10.11.12.52;
    }

    server {
    listen 80;


    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://web_backend;
    }
}

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

To bind domain(s) to your server block you need to add the server_name directive. Below is an updated config.

Adding least_conn; in your upstream block allows for NGINX to more fairly distribute the load across your upstream server(s). More information on the different algorithms can be found here – http://nginx.org/en/docs/http/load_balancing.html

The first server block redirects the non-www version of the domain to the www version.

The second block handles the www request and passes that onto your upstream server(s).

upstream web_backend {
    least_conn;
    server 10.11.12.51;
    server 10.11.12.52;
}

server {
    listen 80;

    server_name yourdomain.com;

    location / {
        return 301 http://www.$server_name$requrst_uri;
    }
}

server {
    listen 80;

    server_name www.yourdomain.com;

    location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_pass http://web_backend;
    }
}