One of Nginx load balancing practices: basic implementation

Source: Internet
Author: User
Tags nginx load balancing

As the current Web site architecture is growing, Internet-based users are also growing, so the traditional standalone version of the server has gradually become unable to adapt to the needs of the times. Recently, in the process of contact with other enterprises, found that the experience of the Internet is particularly important, so-called Internet experience, in fact, refers to high concurrency, large data processing capacity. When it comes to high concurrency, load balancing is certainly not less. I will describe this process on my own machine by building a single version of the load Balancer implementation.

Installing Nginx

First of all, we download the latest version from the official website Http://nginx.org/download/nginx-1.9.9.zip, and then extract to the folder, I am here for the sake of convenience, the path I set as: E:\nginx. After decompression, in the cmd window, we use the following command to open the Nginx service:

Start Nginx       --turn on Nginxnginx-s stop     --close nginxnginx-s Reload   --After modifying the configuration file, reload the Nginx

It is important to note that if you run the Nginx–s Stop command and then run the nginx–s reload command, you will throw an error message, so run the following start Nginx command again at any time after you run the Stop command, and then perform additional command operations.

Once the Nginx service is started, we can see that two processes exist in the Task Manager:

Then we can see the information that Nginx has successfully accessed by opening the http://127.0.0.1 access address in the browser. This means that our nginx installation is normal and correct.

Configure Nginx

Next, we'll start with an example to demonstrate how to load balance a single machine.

First, we open VS2013 and create two webapplication with names of Webapplication1,webapplication2 respectively. The body part of these two webapplication will have different flag information to indicate which of the pages currently visited belongs to either of the two.

We then publish the Web site, open IIS, create two site instances respectively, and then point to the binding address set to: 127.0.0.1:8100 and 127.0.0.1:8200

Finally, we enter the Nginx installation directory, open the Conf folder under the Nginx.conf file, the following configuration:

1. Add a upstream node to mark the DB cluster:

  Upstream  test.var{                server    127.0.0.1:8100 weight=1;        Server    127.0.0.1:8200 weight=1;      

2. Under the server node, modify the location node, access mode:

        #对aspx后缀的进行负载均衡请求 location          /{              root   /wwwroot; #定义服务器的默认网站根目录位置            index index.aspx index.html index.htm ; #定义首页索引文件的名称              proxy_pass  Http://test.var;          Proxy_redirect default;          Proxy_set_header x-real-ip $remote _addr;       }

After this configuration, the basic is complete, I enclose all the configuration:

#user nobody;worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid;events {worker_connections 1024;}    HTTP {include mime.types;    Default_type Application/octet-stream;  #log_format Main ' $remote _addr-$remote _user [$time _local] "$request" ' # ' $status $body _bytes_sent    "$http _referer" ' # ' "$http _user_agent" "$http _x_forwarded_for";    #access_log Logs/access.log Main;    Sendfile on;    #tcp_nopush on;    #keepalive_timeout 0;    Keepalive_timeout 65;     #gzip on;        Upstream test.var{server 127.0.0.1:8100 weight=1;      Server 127.0.0.1:8200 weight=1;        } server {Listen 80;        server_name localhost;        #charset Koi8-r;        #access_log Logs/host.access.log Main;        #location/{# root HTML; # index Default.aspx index.html index.htm;       #} #对aspx后缀的进行负载均衡请求 location/{root/wwwroot;   #定义服务器的默认网站根目录位置 index index.aspx index.html index.htm;          #定义首页索引文件的名称 Proxy_pass Http://test.var;          Proxy_redirect default;         Proxy_set_header X-real-ip $remote _addr;        } #error_page 404/404.html;        # REDIRECT Server error pages to the static page/50x.html # Error_page 502 503 504/50x.html;        Location =/50x.html {root html; } # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ {# ProX        Y_pass http://127.0.0.1;        #} # Pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ {        # root HTML;        # Fastcgi_pass 127.0.0.1:9000;        # Fastcgi_index index.php; # Fastcgi_param Script_filename/scripts$fastcgi_script_namE        # include Fastcgi_params; #} # Deny access to. htaccess files, if Apache ' s document Root # concurs with Nginx ' s one # #l        ocation ~/\.ht {# deny all;       #}} # Another virtual host using mix of ip-, name-, and port-based configuration # #server {# listen    8000;    # Listen somename:8080;    # server_name somename alias Another.alias;    # location/{# root HTML;    # index index.html index.htm;    #} #} # HTTPS Server # #server {# listen 443 SSL;    # server_name localhost;    # ssl_certificate Cert.pem;    # Ssl_certificate_key Cert.key;    # Ssl_session_cache shared:ssl:1m;    # ssl_session_timeout 5m; # ssl_ciphers high:!anull:!    MD5;    # ssl_prefer_server_ciphers on;    # location/{# root HTML;    # index index.html index.htm; #    }    #}}

Access test

Once the configuration is complete, we can access the 127.0.0.1来 Access:

We can see that the first time we visited was WebApplication2, and then we refreshed the page:

After the page is refreshed, we see that the access is WebApplication1.

In this way, when different users to access our server, due to nginx load balancing, so that different users access to the server is different, which also to a certain extent, greatly alleviate the pressure on the server.

Next, I will explain the cache of static resources, session sharing and so on, please look forward to.

Reference Resources

"Large-scale website Technology Practice" entry: Using Nginx to build a reverse proxy server

One of Nginx load balancing practices: basic implementation

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.