Configure Nginx for reverse proxy and load balancing on Ubuntu

Source: Internet
Author: User

In the previous article (http://www.cnblogs.com/chenxizhang/p/4684260.html), I did an experiment that was using visual Studio, based on the Nancy Framework, A self-hosted (self-hosting) application was developed and then deployed to an Ubuntu virtual machine, which was successfully run through mono, which was achieved. NET application on the LIUNX system.

This article is to explain the further experiment, we all know nginx this server, it can be used to do reverse proxy server, can also do load balancing.

About Nginx (to be read as Engine X), interested friends can take a look at the official website: http://nginx.org/

Reverse proxy via Nginx

First, you need to install Nginx

sudo apt-get install nginx
然后,需要对Nginx进行配置
cd /etc/nginx/sites-available/
这个目录下面默认会有一个default的配置文件,内容如下
It is listening on port 80. You can modify this file directly, for example, I have actually set up all the requests forwarded (PROXY_PASS) to the site (8888 port) that we started before.
We can also create a separate configuration file on our own, and specify a different port

Cat > Nancydemo

Then enter the following content

server {
Listen 81;
server_name yourdomainname.com;
Root/var/www/nancydemo;

location/content/{
alias/var/www/nancydemo/content/;
Location ~* \. (Jpg|jpeg|png|gif|ico|css|js|ttf) $ {
Expires 365d;
}
}

Location/{
Proxy_pass http://127.0.0.1:8888;
}
}

Press CTRL +d to save the file and reload the configuration information to access the original 8888 port on port 81.

Sudo/etc/init.d/nginx Reload

There are children's shoes may ask, why should it be so troublesome? User direct access to port 8888 is it all right? Will there be performance degradation by accessing 81 and then accessing port 8888?

In fact, the key to this problem is that you have to understand what is called a reverse proxy. Reverse proxies Hide Real server information (such as addresses) on the one hand, and can also provide faster access.


Direct access (port 8888) and stress test results accessed via proxy (port 81)

Enable two Web servers to load balance via Nginx

Nginx can not only be used as a reverse proxy server, but also a very good load balancer server. In order to do this experiment, I used the previously written program on the server side, launched two Web sites, the ports are 8888 and 9999, respectively.

Next, let's look at how to configure Nginx to enable load balancing. The method is very simple, we need to edit Nancydemo this configuration file (please note the red marked part)


Upstream xizhang{
Server 127.0.0.1:8888;
Server 127.0.0.1:9999;

}
server {
Listen 81;
server_name yourdomainname.com;
Root/var/www/nancydemo;

location/content/{
alias/var/www/nancydemo/content/;
Location ~* \. (Jpg|jpeg|png|gif|ico|css|js|ttf) $ {
Expires 365d;
}
}

Location/{
Proxy_pass Http://xizhang;
}
}

After the configuration is complete, we need to reload the configuration information

Sudo/etc/init.d/nginx Reload

Test results indicate a significant increase in requests per second

We can see two nginx processes in the server, as well as the process of mono.

For load balancing with nginx, there are some details that interest you can refer to http://nginx.org/en/docs/http/load_balancing.html

Nginx can also be used as a static resource server, to achieve dynamic separation (and can be cached), further improve the performance and throughput of the Web server. This topic is not too much to expand, interested friends can refer to the official documents

Configure Nginx for reverse proxy and load balancing on Ubuntu

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.