Using Nginx to build load balancing in Windows environment

Source: Internet
Author: User
Tags sendfile

Objective

Nothing recently, like to summarize and learn something! A few days ago to write a session sharing, then why do we need session sharing? Because our applications are distributed across multiple servers, in order to properly allocate users ' requests, we need to use load balancing technology (the request/data is "evenly distributed" across multiple operational units).

How to achieve load balancing?

1. Use F5 hardware to implement

2. Use the Nginx tool to build a.

Let's explain how to deploy nginx and common problems in a Windows environment.

One: Download Nginx

Go to the official website to download the latest Windows-1.11.10 and unzip it into the English catalogue .

Two: Nginx configuration

Locate the nginx.conf file in the Conf directory, configure Nginx

#user nobody; #指定nginx进程数worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; Events {# Number of connections worker_connections 1024;}    #设定http服务器, using its reverse proxy feature to provide load balancing support for HTTP {#设定mime类型, the type is defined by the Mime.type file 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 instruction Specifies whether Nginx calls the Sendfile function (zero copy mode) to output the file, for general applications, sendfile on;    #tcp_nopush on;    #连接超时时间 #keepalive_timeout 0;    Keepalive_timeout 65;    #开启gzip压缩 #gzip on;    #设定负载均衡的服务器列表 support multiple sets of load balancing, you can configure multiple upstream to serve different servers. #nginx upstream supports several ways of assigning #1), polling (default) each request is assigned to a different back-end server in chronological order, and can be automatically rejected if the backend server is down.     #2), weight specifies the polling probability, the weight is proportional to the access ratio, and is used in cases where the performance of the backend server is uneven.    With the above sample, the weights are specified.     #3), ip_hash each request according to the hash result of the access IP, so that each visitor fixed access to a back-end server, can solve the session problem. #4), fair #5), Url_hash #Urlhash upstream Mysvr {#weigth参数表示权值, the higher the weight, the greater the probability of being allocated #1. Down indicates that the server before the single           Do not participate in load #2. Weight by default, the larger the 1.weight, the greater the load weight. #3. Backup: When all other non-backup machines are down or busy, request the backup machine.        So the pressure on this machine is the lightest.      #server 192.168.1.116 down;      #server 192.168.1.116 Backup;      Server 192.168.1.121 weight=1;    Server 192.168.1.122 weight=2;        } #配置代理服务器的地址, the server address of the Nginx installation, the listening port, the default address server {#1. Listen 80 port Listen 80;                #对于server_name, if you need to reverse proxy multiple domain name requests, you can configure multiple server_name to meet the requirements server_name localhost;        #charset Koi8-r;        #access_log Logs/host.access.log Main;            The location/{# Default home directory is in the HTML subdirectory of the Nginx installation directory.            root HTML;                       Index index.html index.htm; Proxy_pass Http://mysvr;   #跟载均衡服务器的upstream对应}     #error_page 404/404.html; # REDIRECT Server error pages to the static page/50x.html # # Definition error Prompt page #error_page 502 503 504/50x.h        Tml        #location =/50x.html {# root HTML; #} # Proxy The PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ {# Pro        Xy_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; #    }    #}}
Three: Start Nginx

CMD into Nginx unzip directory to perform start Nginx start Nginx Service

How do I check if it started successfully after startup? Enter the command tasklist/fi "imagename eq nginx.exe" and see the following message stating that the launch was successful

All ready, access to the server configuration of the server_name is not redirected to the upstream configuration of the server, is not very simple!

Four: frequently asked questions

If the startup fails, you can see the error message in the Error.log file in the logs directory.

I encountered two errors in the first installation, but also the most easily encountered problems, listed here to facilitate people encounter the same problem quickly resolved.

1. Port occupancy Issues

My profile in the service is listening to the 80 port, because the machine is deployed on the iis,80 port is occupied by the default site, the site is closed, this problem is recorded in the error log.

Failed (10013:an attempt was made to access a socket-in-a-do forbidden by its access permissions)

2.Nginx is in the same directory in Chinese

The error log roughly outputs a bit of content

Failed (1113:no mapping for the Unicode character exists in the target Multi-Byte code page)

3. Enable Cache times Error

2015/01/15 17:26:50 [Emerg] 17068#20356:shared zone "Cache_one" has no equal addresses:02cf0000 vs 02A200002015/01/15 17 : 26:50 [Alert] 11536#11228:worker process 17068 exited with code 1

I have not found a solution, some people say restart the service, or cache settings a little bit more, I tried a bit useless, the official website is said, can only think windwos under no solution.

: the cache and other modules which require shared memory support do:not work in Windows Vista and later due to address Space Layout:randomization being enabled in these Windows versions.

Five. Common directives

1:start nginx Start-up service

2:nginx-s Reload Modify configuration after reload takes effect

3:nginx-s Reopen reopen log file

4:NGINX-T-c/path/to/nginx.conf Test nginx configuration file is correct

5:nginx-s Stop Quick Stop Nginx

6:tasklist/fi "imagename eq nginx.exe" View nginx demo Open

Have a reference to the article on the Internet, have their own understanding, welcome to correct me ...

Using Nginx to build load balancing in Windows environment

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.