Nginx负载均衡在windows上的配置

时间:2021-12-26 19:17:01

一、下载Nginx windows部署包

http://nginx.org/en/download.html
下载后解压到c或d盘

二、命令启动服务

cmd: start nginx
nginx -s stop quick exit
nginx -s quit graceful quit
nginx -s reload changing configuration, starting a new worker, quitting an old worker gracefully
nginx -s reopen reopening log files

三、测试服务是否安装成功

在浏览器输入http://localhost,如果页面显示如下,则成功 Nginx负载均衡在windows上的配置
如果error.log中出现:2011/06/16 15:23:55 [emerg] 7136#4040: bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions),则未安装成功,80端口被占用了,在nginx.conf文件中把listen端口改成其他的端口比如8099.

四、实例搭建

    首选:我们要在我们的iis上面把我们做好的web应用部署上去,部署到不同的机器上面,设置好对应的ip和端口号,因为我在本机模拟出效果,所以我就在本机的iis上面部署了2个web应用,第一个web应用部署在localhost:8011端口,第二个应用部署为localhost:8012端口,同时为了看到演示效果,我们把里面的WebForm1.aspx页面做了一个标记,里面标记为:第几个web应用程序的页面,实际我们部署系统,不需要这样做,就是把我们的一个web应用部署到不同机器上面的服务器上,下面所示。
web应用1地址:http://localhost:8011/WebForm1.aspx
web应用2地址:http://localhost:8012/WebForm1.aspx
(1)启动Nginx服务
(2)修改Nginx配置项,具体配置说明,我们在参数设置部门说明,然后验证服务是否正常启动。
(3)访问地址http://localhost:8010/WebForm1.aspx观察结果
Nginx负载均衡在windows上的配置
Nginx负载均衡在windows上的配置

(4)这样我们就可以模拟出负载均衡效果了,ok

五、参数设置

配置C服务器(前端负载转发服务器)nginx的配置文件 nginx.conf
以下标红的就是需要配置的.其中ip_hash很重要(可以保证每个访客可以固定一个后端,保证session不会出问题)

#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 localhost
{
ip_hash;
server 192.168.0.1:81;
server 192.168.0.2:81;
}

server {
listen 80;
server_name localhost;


#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass http://localhost;
proxy_redirect default;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 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 ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;

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

}