Use nginx to implement Server Load balancer proxy (see the truth in the figure)

Source: Internet
Author: User
Tags epoll openssl library nginx reverse proxy

1. Like Apache, they are both HTTP server software and adopt Modular Design for function implementation. They all support common language interfaces. At the same time, they also support forward and reverse proxies, virtual hosts, and URL rewriting,CompressionTransmission, SSL encrypted transmission, and so on. However, in terms of execution efficiency, nginx processes faster and consumes less memory resources. In addition, nginx also supports fcgi, which is not supported by Apache.

2. nginx advantages: it processes static files and the efficiency of indexing files is very high. It can achieve non-Cache reverse proxy acceleration and improve the website running speed.Server Load balancerServer, supporting multiple fault tolerance and algorithms; efficient and stable performance.

3. Most nginx documents are in Russian, so there are currently few detailed Chinese documents. Today, I will give a rough talk about how to use nginx's reverse proxy function to build a Server Load balancer server.

The experiment topology is as follows:

A total of four machines:

Web1 and web2 are two Web servers respectively (they can be built using Apache, nginx, or IIS, which is not detailed here ). The gateway points to 192.168.1.5. The webpage content on the two web servers should be consistent in the actual working environment. To verify the effect, the webpage content on the two web servers is inconsistent. I am using links on proxy for convenience.CommandThe verification results are as follows:

Proxy is an nginx server installed.

1. Install nginx

Install the PCRE Library first to enable nginx to Support HTTP reWrite. Here I use the configured local yum for Installation

ExtractNginx

Install nginx


During installation, there are many optional parameters. Here I only use a few. For specific parameters, see the end of this document.

Configure nginx

To facilitate direct configuration, the content is as follows:

The specific content of nginx. conf is as follows:

Worker_processes 1; specify the number of nginx processes, which is usually consistent with the number of CPUs

Events {

Worker_connections 1024; specifies the maximum number of connections for each process. The default value is 1024.

}

HTTP {

UPSTrMyeam server {set network load balancing and name it myserver (whatever)

Server 192.168.1.4 Weight = 4; Address of the first web server, with a weight of 4

Server 192.168.1.3 Weight = 3; the higher the weight, the higher the chance of access

}

Server {

Listen 80; listening on the local port

SERVER_NAME 192.168.110.1 www.Ex. Com; which port of the local machine is used as the proxy function, why is the domain name?

Index index.html; default webpage File Read

LoCatIon/{path settings

Proxy_pass http: // myserver; specifies the proxy's backend server address and port

}

Error_page 500 502 503 x.html; error page file

Location =/50x.html {

Root HTML;

}

}

}

Start nginx

Note: Use nginxKillCommand to close

There is also a machine on the Internet, with the IP address of 192,168.110 .254, simulating Internet users.

Test:

Click More

Since then, all simple Load Balancing for nginx reverse proxy has been completed. Of course, nginx has many parameters and has very powerful functions. Here we just want to implement this function in a simple way, hoping to give readers some inspiration.

Additionally, the optional installation parameters are as follows:

The configure script determines some features of the system, especially the nginx method for handling connections. Then, it creates makeFileFile.

Configure supports the following options:

-- Prefix =<Path>-nginx installation path. If not specified, the default value is/usr/local/nginx.

-- Sbin-Path =<Path>-nginx Executable File Installation path. It can only be specified during installation. If it is not specified, the default value is <prefix>/sbin/nginx.

-- Conf-Path =<Path>-the default nginx. conf path is not specified under-C. If not specified, the default value is <prefix>/CONF/nginx. conf.

-- PID-Path =<Path>-if no PID command is specified in nginx. conf, the default nginx. PID path is used. If not specified, the default value is <prefix>/logs/nginx. PID.

-- Lock-Path =<Path>-nginx. Lock file path.

-- Error-log-Path =<Path>-the default error log path when no error_log command is specified in nginx. conf. If not specified, the default value is <prefix>/logs/error. log.

-- Http-log-Path =<Path>-the default access log path when no access_log command is specified in nginx. conf. If not specified, the default value is <prefix>/logs/access. log.

-- User =<User>-if no USER command is specified in nginx. conf, the default nginx user is used. If not specified, the default value is nobody.

-- Group =<Group>-the default group used by nginx when no USER command is specified in nginx. conf. If not specified, the default value is nobody.

-- BuilDdIR = dir-Specify the compiled directory

With-rtsig_moDuLe-Enable the rtsig Module

-- With-select_module -- without-select_module-Whether or notEnableThe select module. This module is enabled by default ifMore SuItable method such as kqueue, epoll, rtsig or/dev/poll is not discovered by configure.

// Select mode is allowed or not allowed. If configure does not find a more suitable mode, such as kqueue (Sun OS), epoll (Linux kenel 2.6 +), rtsig (real-time signal) or/dev/poll (a select-like mode, the underlying implementation is basically the same as the select, all adopt the Round Training Method) select mode will be the default installation mode

-- With-poll_module -- without-poll_module-Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or/dev/poll is not discovered by configure.

With-http_ssl_module-Enable ngx_http_ssl_module. enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-Dev.

// Enable the http ssl module so that nginx can support HTTPS requests. OpenSSL must be installed in this module, which is libssl-dev On Debian.

With-http_realip_module-Enable ngx_http_realip_module

With-http_addition_module-Enable ngx_http_addition_module

With-http_sub_module-Enable ngx_http_sub_module

With-http_dav_module-Enable ngx_http_dav_module

With-http_flv_module-Enable ngx_http_flv_module

-- With-http_stub _StatUs_module-Enable the "server status" Page

Without-http_charSet_ Module-Disable ngx_http_charset_module

Without-http_gZip_ Module-Disable ngx_http _Gzip_ Module. If enabled, zlib is required.

Without-http_ssi_module-Disable ngx_http_ssi_module

Without-http_userid_module-Disable ngx_http_userid_module

Without-http_access_module-Disable ngx_http_access_module

Without-http_auth_basic_module-Disable ngx_http_auth_basic_module

Without-http_autoindex_module-Disable ngx_http_autoindex_module

Without-http_geo_module-Disable ngx_http_geo_module

Without-http_map_module-Disable ngx_http_map_module

Without-http_referer_module-Disable ngx_http_referer_module

Without-http_rewrite_module-Disable ngx_http_rewrite_module. PCRE is required if ngx_http_rewrite_module is enabled.

Without-http_proxy_module-Disable ngx_http_proxy_module

Without-http_fastcgi_module-Disable ngx_http_fastcgi_module

Without-http_meMCAched_module-Disable ngx_http_memcached_module

Without-http_limit_zone_module-Disable ngx_http_limit_zone_module

Without-http_empty_gif_module-Disable ngx_http_empty_gif_module

Without-http_browser_module-Disable ngx_http_browser_module

Without-http_upstream_ip_hash_module-Disable ngx_http_upstream_ip_hash_module

With-http_perl_module-Enable ngx_http_perl_module

-- With-perl_modules_path = path-Specify the path of the Perl Module

-- With-perl = path-Specify the path of the Perl execution File

-- Http-log-Path = path-Set path to the HTTP access log

-- Http-client-body-temp-Path = path-Set path to the HTTP client request body temporary files

-- Http-proxy-temp-Path = path-Set path to the HTTP Proxy temporary files

-- Http-FastCGI-temp-Path = path-Set path to the HTTP FastCGI temporary files

-- Without-HTTP-Disable HTTP Server

-- With-mail-Enable IMAP4/POP3/SMTP proxy Module

With-mail_ssl_module-Enable ngx_mail_ssl_module

-- With-CC = path-Specify the path of the C compiler.

---CPP = path-Specify the path of the C Preprocessor

-- With-CC-opt = options-Additional ParametersWhichWill be added to the variable cflags. with the use of the system library PCRE in FreeBSD, it is necessary to indicate -- With-CC-opt = "-I/usr/local/INCLude ". If we are using select () and it is necessary to increase the number of file descriptors, then thisLsO can be assigned here: -- With-CC-opt = "-D fd_setsize = 2048 ".

-- With-LD-opt = options-Additional parameters pasSedTo the linker. with the use of the system library PCRE in FreeBSD, it is necessary to indicate -- With-LD-opt = "-l/usr/local/lib ".

-- With-CPU-opt = CPU-For Specific CPU compilation, valid values include: Pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64

-- Without-PCRE-Disable the use of the PCRE Library. The HTTP rewrite module is also disabled. In the "location" configuration commandRegular ExpressionThe expression also needs PCRE.

-- With-PCRE = dir-Specifies the path of the source code of the PCRE Library.

-- With-PCRE-opt = options-Set additional options for PCRE building.

-- With-md5 = dir-Set path to MD5 library sources.

-- With-md5-opt = options-Set additional options for MD5 building.

With-md5-asm-Use MD5 receiver sources.

-- With-sha1 = dir-Set path to sha1 library sources.

-- With-sha1-opt = options-Set additional options for sha1 building.

With-sha1-asm-Use sha1 extends er sources.

-- With-zlib = dir-Set path to zlib library sources.

-- With-zlib-opt = options-Set additional options for zlib building.

-- With-zlib-ASM = CPU-Use zlib assembler sources optimized for specified CPU, valid values are: Pentium, pentiumpro

-- With-OpenSSL = dir-Set path to OpenSSL library sources

-- With-OpenSSL-opt = options-Set additional options for OpenSSL Building

-- With-Debug-Enable debugging logs

-- Add-module = path-Add in a third-party module found in directory path

In different versions, the options may be slightly changed. Please always use./Configure -- HelpCommand to check the list of current options.

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.