博主做实验用的是nginx-1.9.4 window版本
如何启动nginx服务器
首先把nginx下到电脑中 打开doc创建 切换到nginx目录
输入 start nginx.exe,服务器启动,可以看到进程有如下进程
开始搭建集群环境
首先先简单了解集群的请求路径的流程是
用户发送相应报文----nginx反向代理服务器(可以有一个或者多个) ---nginx通过自身的算法发送给 web服务器---服务器接受响应处理业务
首先为了看清楚实验的效果建2个简单的web项目,然后分别在各自的index.jsp写上文件如下图
新建2个server,分别部署到不同的Tomcat上,并且修改2个tomcat的端口号,防止启动冲突
修改nginx的配置文件
在浏览器上输出localhost 可以访问反向代理服务器动态分配到test.com群组,然后根据weight大小来处理转发到哪个Tomcat上,weight越大越优先处理请求
=================================================================================================================
nginx配置如下
</pre><pre name="code" class="html">#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;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #长连接超时时间,单位是秒
gzip on;#启用Gizp压缩
#服务器的集群
upstream test.com { #服务器集群名字
#server 172.16.21.13:8081 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。
#server 192.168.1.186:8081 weight=1;
#server 172.16.1.14:8081 weight=2;
#server 172.16.1.15:8081 weight=1;
#server 172.16.1.15:80 weight=1;
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8080 weight=2;
}
#当前的Nginx的配置
server {
listen 80;#监听80端口,可以改成其他端口
server_name localhost;############## 当前服务的域名
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
location / {
proxy_pass http://test.com;
proxy_redirect default;
}
location ~ \.(jpg|png|jpeg|bmp|gif|swf|css)$
{
expires 30d;
root /nginx-1.4.7;#root:
break;
}
#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 HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}