How to Use nginx to configure virtual servers and load balancing

Source: Internet
Author: User
1 cause

I have never caught a cold in some internationally renowned hot terms such as reverse proxy. To be humble, you cannot. I don't think we can use these concepts at all, and there is no need to waste time learning. As a result, nginx is a software that makes Nb so easy to use and has been ignored by me all the time.

When expected to go online, you need to use the server to ensure that the original system can be accessed normally and the expected domain name can be obtained accordingly. I thought of it again.

2 Problems

A. Problems with the server: The domain name www.aaa.com is resolved to the Internet IP address 123.123.123.123, and the domain name www.aaa.net is also resolved to the Internet IP address 123.123.123.123. At the same time, there is a service that cannot be stopped on the Intranet address 10.10.10 of the Internet IP address. The Intranet must continue to use this Intranet address to use the original system. Both are on port 80.

B. performance problem: assume that the web system name WOS has extended the other two Web servers due to performance problems, and the original one-day server becomes three. How can we solve the issue of publishing, how to solve the problem of Server Load balancer.

3. Understand the concept of reverse proxy

No, the world is a genius.

    • Someone found a proxy server to bypass the Internet, that is, a machine that can access the Internet in the LAN. Other machines use this server as a proxy to access the Internet. In this way, all machines in the LAN can access the Internet. This is a forward proxy;

    • Someone invented a reverse proxy for service resources, that is, a machine in the LAN that can be accessed by the Internet, requests on the Internet are forwarded to Web applications on other machines that do not have requests in the LAN through the proxy, and then the results are returned to the requested Internet. In this way, web applications with only one machine can be accessed by the Internet, and all web applications provided in the LAN can be accessed by the Internet. This is the reverse proxy.

I didn't like the term "reverse proxy", just as I didn't like "IOC" to control flip. I am BS and think it is a text game without actually experiencing the power of concepts. However, please note that once these concepts are verified by the development and management practices, I will fully agree with and respect them. Make up for it.

4. Solve the problem and rub the server resources (IP resources, that is, use one server as two servers)

Yijing says, "One, two, three" are born of everything. Not to mention the third-party post. So I will talk about how to get rid of it, and add second-level or multi-level nginx or virtual servers internally. The same is true.

For the sake of simplicity, I am using the Windows version of nginx, and I have not carefully considered the performance. Since it is an official version, I believe it is always better than IIS. When I write these words, the latest version is 1.3.13. We recommend that you use a stable version 1.2.7: http://nginx.org/download/nginx-1.2.7.zip. The software package is very small, 1.14 m. If someone remembers the disk, a general disk can be fully accommodated.

Decompress the package to a directory. Since we are not research-oriented, we can simply upload a nginx.exe and conf directory.

The focus of nginx is to configure the conf file. After nginx is started, the reverse proxy is completed based on the configuration in the/CONF/nginx. conf file.

The configuration of nginx. conf is as follows:

 Worker_processes  1  ;  # The general configuration is consistent with the number of CPU cores to obtain the maximum performance  Events  { Worker_connections  1024  ;  }  HTTP  {  Include mime  .  Types  ;  Default_type Application  /  Octet  -  Stream  ;  Sendfile on  ;  Keepalive_timeout  65  ; Gzip on  ;  Gzip_disable  "MSIE [1-6] \. (?!. * Sv1 )"  ;  Include youliaoo  .  Conf  ;  # Write the server configuration in an independent file and load it in the nginx main configuration file  } 

You need to put the youliaoo. conf file in the same directory as nginx. conf. The content is as follows:

 # First Service  Server  {  Listen  80  ;  # Listening to port 80  SERVER_NAME 10.10  .  10.10  ;  # The request header contains 10.10.10.10  Location  /  {  # Configure address ing. If you configure/, all requests will be forwarded.  Proxy_pass HTTP  :  // 10.10.10.10: 8800; # The target address. I changed the original web application on port 80 of the 10 host to port 8800. Of course, it can also be an address that can be accessed by other local networks.  Proxy_redirect off  ;  Proxy_set_header  Host  $ Host  ;  Proxy_set_header x -  Real  -  IP $ remote_addr  ;  Proxy_set_header x  -  Forwarded  -  For  $ Proxy_add_x_forwarded_for  ;  }  }  # Second service  Server  {  Listen  80  ;  # Listening to port 80 # For SERVER_NAME, You need to reverse proxy requests for multiple domain names. Therefore, you can configure multiple SERVER_NAME to meet the requirements.  SERVER_NAME WWW  .  Youliaoo  .  Com  ;  SERVER_NAME WWW  .  Youliaoo  .  Net  ;  SERVER_NAME youliaoo  .  Com  ;  SERVER_NAME youliaoo  .  Net ;  Location  /  {  # Proxy of all addresses. If the application is large, you can also transfer registration to one server for processing, transfer logon to one server for processing, and statically route the registration to one server for processing.  Proxy_pass HTTP  :  // 10.10.10.10: 8080; # youliaoo system service address, lan address, I still use 10 machines, you can also configure other addresses in the LAN  Proxy_redirect off  ;  Proxy_set_header  Host  $ Host  ;  Proxy_set_header x  -  Real  - IP $ remote_addr  ;  Proxy_set_header x  -  Forwarded  -  For  $ Proxy_add_x_forwarded_for  ;  }  } 

In this way, all requests using 10.10.10.10 will be forwarded to the system running on 10.10.10.10: 8800. Requests using www.youliaoo.com or other domain names will be forwarded to 10.10.10.10: 8080 for processing.

When nginx.exeis run after this configuration, two nginx.exe files are generated. Because the worker_processes we configured is 1, if it is 2, there will be three processes.

For Internet visitors, the service is transparent and still has the same effect as the two independent servers. Note that for the second service configuration, you can refer to configure the third and fourth services. You can name SERVER_NAME differently.

5. Solve the B problem and use nginx for load balancing to horizontally expand resources to improve performance

On the web application system WOS that we assume, we have the conditions to add other server resources in the LAN. Question 1: these servers can only be accessed from the Intranet. Question 2, users can choose to manually publish static link addresses. To solve these problems, one must solve the problem of hiding other horizontally extended server addresses and publishing only one address, and also solve the Load Balancing Problem of these services. I suppose we have four other servers running the same web application, including 11, 12, 13, and 14, in addition to 10 servers.

The nginx configuration remains unchanged. We still Configure the server in the included manner. Note the include youliaoo. conf mentioned above. Configure the youliaoo. conf file for the purpose.

 Upstream otherres  {  # Define the Server Load balancer server group. The addresses are all Intranet addresses. In fact, it doesn't matter as long as the address form can be accessed by assembling nginx server.  Ip_hash  ;  # Note that some application systems use session to implement identity authentication, so this must be added.  Server  10.10  .  10.10  Weight  =  1 ;  # Configure the address. weight is the weight allocated when Server Load balancer is used. The larger the weight is allocated to process requests, the higher the chance.  Server  10.10  .  10.11  Weight  =  5  ;  # Configure the address, configure 1, 5, or 10, or other values, which have no practical significance and are only used to describe the weight distribution. No upper limit  Server  10.10  .  10.12  Weight  =  10  ;  Server  10.10 .  10.13  Weight  =  10  ;  Server  10.10  .  10.14  Weight  =  10  ;  }  Server  {  Listen  80  ;  # Listening to port 80  SERVER_NAME 10.10  .  10.10  ;  # Use 10. All corresponding requests  Location  /  {  # Proxy all request addresses  Proxy_pass HTTP  :  // Otherres; # distribute the requests to the defined server group and distribute the requests by the Server Load balancer configuration.  Proxy_redirect off  ;  Proxy_set_header  Host  $ Host  ;  Proxy_set_header x  - Real  -  IP $ remote_addr  ;  Proxy_set_header x  -  Forwarded  -  For  $ Proxy_add_x_forwarded_for  ;  }  } 

OK. If you have already run nginx, end the process that occupies a small amount of memory in the task manager. Run nginx.exe again. Of course, you can also use the command line nginx-s restart. It seems that it is more convenient to start a command line in Windows to directly end the task.

When the performance of a server is insufficient, the same configuration can be used to expand server resources in parallel. From the perspective of external access, the system has not changed, but the processing capability is stronger. In addition, we can allocate weight based on the actual resources of the server. Of course, nginx also balances the access time and sets the number of failures to indicate the host and other functions. You can carefully check the implementation of nginx manual. For general applications, the above configuration is sufficient.

Bingo!

[original reproduced] http://www.youliaoo.com/post/26

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.