Simulation of Nginx reverse proxy and load balancing under different ports

Source: Internet
Author: User
Tags nginx reverse proxy

Nginx is a lightweight WEB server, reverse proxy server and e-mail server, this article describes the use of different ports in the virtual machine to simulate different servers, and the use of Nginx reverse proxy function to achieve server load balancing effect.


Create a new two sites

First, create two WEB directories as sites under different servers, one is website-a, and the other is website-b. These two site directories should be on different servers, and the code is the same, here to facilitate the simulation of a virtual machine on a different server, and the two site directory to make a few changes to the home page, to differentiate access to different directories (servers) under the site. Two sites have only a simple index.html home page, the contents of which are "this is website a".

vagrant@homestead:~/html/load-balance$ ls
Website-a Website-b

Configuring Nginx reverse proxy and load balancing

Create a new file in the/etc/nginx/sites-enabled directory, such as: Test-load-balance, as follows:

Upstream Tanteng_me {
Server localhost:1031;
Server localhost:1032;
}

server {
Listen 80;
server_name tanteng2.me www.tanteng2.me;

Location/{
#设置主机头和客户端真实地址 so that the server gets the client-side real IP
Proxy_set_header Host $host;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_pass Http://tanteng_me;
}
}
This reverse proxy server listens on port 80 and defines a server load balancing module, where two different ports represent different servers (because they are in a virtual machine and do not have multiple virtual machines installed, so they are modeled with different ports).

Nginx WEB Server Configuration

Under the/etc/ngxin/conf.d/new load-balance.conf, the contents are as follows:

server {
Listen 1031;
server_name tanteng2.me;
Root/usr/share/nginx/html/load-balance/website-a;
Index index.html index.htm;

#charset Koi8-r;
#access_log/var/log/nginx/log/host.access.log Main;

Location/{
Try_files $uri $uri//index.php $query _string;
}
}

server {
Listen 1032;
server_name tanteng2.me;
Root/usr/share/nginx/html/load-balance/website-b;
Index index.html index.htm;

#charset Koi8-r;
#access_log/var/log/nginx/log/host.access.log Main;

Location/{
Try_files $uri $uri//index.php $query _string;
}
}

There are two servers configured here, and the site root directory is website-a and website-b by listening to different ports, and after the above configuration, bind the virtual machine host and access it through the browser.

You can see the Refresh page, the content is also changing, the description through load balancing configuration, to achieve the polling server effect.

This paper introduces the simulation Nginx reverse proxy and load balancing under different ports, not only to understand the basic Nginx reverse proxy and load-balanced configuration, but also to simulate different servers with this different port to do another thing, and then talk about it later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.