Nginx+windows Load Balancing Configuration method _nginx

Source: Internet
Author: User
Tags epoll switches what is nginx nginx load balancing
First, download Nginx
Http://nginx.org/download/nginx-1.2.5.zip
Extract to C:\nginx directory
Second, on the two servers to build a website:
s1:192.168.16.35:8054
s2:192.168.16.16:8089
Second, find the directory
C:\nginx\conf\nginx.conf
Open nginx.conf
The configuration is as follows:

Copy Code code as follows:

#使用的用户和组, not specified under window
#user nobody;
#指定工作衍生进程数 (generally equal to twice times the total number of CPUs or sums, such as two quad-core CPUs, the sum of which is 8)
Worker_processes 1;
#指定错误日志文件存放路径, error log level can be selected as "Debug|info|notice|warn|error|crit"
#error_log Logs/error.log;
#error_log Logs/error.log Notice;
Error_log Logs/error.log Info;
#指定pid存放路径
#pid Logs/nginx.pid;

#工作模式及连接数上限
Events {
#使用网络I/o model, Linux system recommended the use of Epoll model, FreeBSD system recommended use Kqueue;window not specified
#use Epoll;
#允许的连接数
Worker_connections 1024;
}

#设定http服务器, using his reverse proxy function to provide load balancing support
HTTP {
#设定mime类型
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;
Log_format Main ' $remote _addr-$remote _user [$time _local] '
' $request ' $status $bytes _sent '
"$http _referer" "$http _user_agent" "$http _x_forwarded_for" "
' $gzip _ratio ';
Log_format Download ' $remote _addr-$remote _user [$time _local] '
' $request ' $status $bytes _sent '
"$http _referer" "$http _user_agent"
' $http _range ', ' $sent _http_content_range ';

#设定请求缓冲
Client_header_buffer_size 1k;
Large_client_header_buffers 4 4k;

#设定access Log
Access_log Logs/access.log Main;
Client_header_timeout 3m;
Client_body_timeout 3m;
Send_timeout 3m;

Sendfile on;
Tcp_nopush on;
Tcp_nodelay on;
#keepalive_timeout 0;
Keepalive_timeout 65;

#开启gzip模块
gzip on;
Gzip_min_length 1100;
Gzip_buffers 4 8k;
Gzip_types text/plain application/x-javascript text/css application/xml;

Output_buffers 1 32k;
Postpone_output 1460;

Server_names_hash_bucket_size 128;
Client_max_body_size 8m;

Fastcgi_connect_timeout 300;
Fastcgi_send_timeout 300;
Fastcgi_read_timeout 300;
Fastcgi_buffer_size 64k;
Fastcgi_buffers 4 64k;
Fastcgi_busy_buffers_size 128k;
Fastcgi_temp_file_write_size 128k;
Gzip_http_version 1.1;
Gzip_comp_level 2;
Gzip_vary on;

#设定负载均衡的服务器列表
Upstream localhost {
#根据ip计算将请求分配各那个后端tomcat, you can solve the session problem
Ip_hash;
#同一机器在多网情况下, routing switches, IP may be different
#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.
#server localhost:8080 weight=1;
#server localhost:9080 weight=1;
Server 192.168.16.35:8054 max_fails=2 fail_timeout=600s;
Server 192.168.16.16:8089 max_fails=2 fail_timeout=600s;
}

#设定虚拟主机
server {
Listen 80;
server_name 192.168.16.16;

#charset Koi8-r;
CharSet UTF-8;
#设定本虚拟主机的访问日志
Access_log Logs/host.access.log Main;
#假如访问/img/*,/js/*,/css/* Resources, take the local document directly, not through squid
#假如这些文档较多, this approach is not recommended because the cache effect through squid is better
#location ~ ^/(img|js|css)/{
# root/data3/html;
# expires 24h;
# }
#对 "/" Enable load balancing
Location/{
root HTML;
Index index.html index.htm index.aspx;

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;
#允许客户端请求的最大单个文件字节数
Client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数, it can be understood that first save to local and then pass to the user
Client_body_buffer_size 128k;
#跟后端服务器连接超时时间 initiate handshake waiting for response timeout
Proxy_connect_timeout 12;
#连接成功后 waiting for back-end server response time is actually in the back-end queue waiting to be processed
Proxy_read_timeout 90;
#后端服务器数据回传时间 is that the backend server must pass all the data within the specified time
Proxy_send_timeout 90;
#代理请求缓存区 This cache interval will save the user's header information altogether nginx rules processing generally as long as can save the head information
Proxy_buffer_size 4k;
#同上 tell Nginx how much space to save for a single buffer.
Proxy_buffers 4 32k;
#如果系统很忙的时候可以申请国内各大的proxy_buffers Official recommendation *2
Proxy_busy_buffers_size 64k;
#proxy the size of the cache temp file
Proxy_temp_file_write_size 64k;
Proxy_next_upstream Error timeout Invalid_header http_500 http_503 http_404;
Proxy_max_temp_file_size 128m;

Proxy_pass http://localhost;
}

#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$ {
# Proxy_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
#
#location ~/\.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;
# server_name localhost;

# SSL on;
# ssl_certificate Cert.pem;
# Ssl_certificate_key Cert.key;

# ssl_session_timeout 5m;

# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers high:!anull:! MD5;
# ssl_prefer_server_ciphers on;

# location/{
# root HTML;
# index index.html index.htm;
# }
#}

}

Double-click the C:\nginx\nginx.exe file to start the Nginx.
Five, open the browser:
Enter http://192.168.16.16 for access
Test: Turn off the website on the S1, then refresh the browser access, turn off the S2 on the site, open the S1 website, refresh the browser access.

Core Code 1: join in http{}
Copy Code code as follows:

#设定负载均衡的服务器列表
Upstream localhost {
#根据ip计算将请求分配各那个后端tomcat, you can solve the session problem
Ip_hash;
#同一机器在多网情况下, routing switches, IP may be different
#weigth参数表示权值, the higher the weight, the greater the chance of being assigned.
#server localhost:8080 weight=1;
#server localhost:9080 weight=1;
Server 192.168.1.98:8081 max_fails=2 fail_timeout=600s;
Server 192.168.1.98:8082 max_fails=2 fail_timeout=600s;


Core Code 2: adding in server {}
Copy Code code as follows:

#对 "/" Enable load balancing
Location/{
root HTML;
Index index.html index.htm index.aspx;

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;
#允许客户端请求的最大单个文件字节数
Client_max_body_size 10m;
#缓冲区代理缓冲用户端请求的最大字节数, it can be understood that first save to local and then pass to the user
Client_body_buffer_size 128k;
#跟后端服务器连接超时时间 initiate handshake waiting for response timeout
Proxy_connect_timeout 12;
#连接成功后 waiting for back-end server response time is actually in the back-end queue waiting to be processed
Proxy_read_timeout 90;
#后端服务器数据回传时间 is that the backend server must pass all the data within the specified time
Proxy_send_timeout 90;
#代理请求缓存区 This cache interval will save the user's header information altogether nginx rules processing generally as long as can save the head information
Proxy_buffer_size 4k;
#同上 tell Nginx how much space to save for a single buffer.
Proxy_buffers 4 32k;
#如果系统很忙的时候可以申请国内各大的proxy_buffers Official recommendation *2
Proxy_busy_buffers_size 64k;
#proxy the size of the cache temp file
Proxy_temp_file_write_size 64k;
Proxy_next_upstream Error timeout Invalid_header http_500 http_503 http_404;
Proxy_max_temp_file_size 128m;
Proxy_pass http://localhost;
}


here are some additional tools:

Nginx load Balancing is a very magical technology, many people do not have a good grasp of this technology, today we are here to give you a detailed introduction of the Nginx load balancing problem. Today, a little try nginx load balance, really cool ah! What is Nginx?

Nginx ("Engine X") is a high-performance HTTP and reverse proxy server and a IMAP/POP3/SMTP proxy server. Nginx was developed by Igor Sysoev, the second rambler.ru site for Russian traffic, which has run over 2.5 at the site. Igor releases the source code in the form of a BSD-like license. Although still beta, Nginx has been known for its stability, rich feature sets, sample configuration files, and low system resource consumption.

First of all, the configuration is very simple, and the function is very powerful. It's encounter. Let's take a look at the configuration file.

Copy Code code as follows:

Worker_processes 1;
Events {
Worker_connections 1024;
}
http{
Upstream MyProject {
#这里指定多个源服务器, IP: port, 80 port can write or write
Server 192.168.43.158:80;
Server 192.168.41.167;
}
server {
Listen 8080;
Location/{
Proxy_pass Http://myproject;
}
}
}


What are the functions of nginx load Balancing?

If one of the following servers is broken, it can automatically identify, more cattle is it good after nginx can immediately identify the server A and B, if a response time of 3,b response time is 1, then Nginx will automatically adjust access to B is 3 times times the probability of a true nginx load balanced well, The installation is complete. I made a mistake in make, saying that there was a problem with the HTTP Rewrite module and I

./configure–without-http_rewrite_module
Then you can make,make install.

Once installed, create a new profile, copy the contents of the configuration file, and, of course, modify your IP, save it for example load_balance.conf and start:

/usr/local/nginx/sbin/nginx-c load_balence.conf

Because the author of Nginx is Russian, so the English document is not so perfect, for me, the biggest advantage of nginx or simple configuration, powerful. I used to be a apache-jk, and that's really not the average person to match. It's too complicated and can only be used to do Tomcat's nginx load balancing.

Nginx does not have this limitation, for it is what the server behind is fully named. Nginx is a bit upset that it itself is not currently running under Windows. Wrote a whole bunch, haha. ~ ~ That's not right, everybody points out ha
Related Article

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.