1.原理
2.准备工作
下载nginx和tomcat,并放入到各自目录下,准备至少2份tomcat
3.修改Nginx配置文件
#user nobody; worker_processes 1;#工作进程的个数,一般与计算机的cpu核数一致 #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; gzip on;#启用Gizp压缩 #服务器的集群 upstream netitcast.com { #服务器集群名字 server 127.0.0.1:18080 weight=1;#服务器配置 weight是权重的意思,权重越大,分配的概率越大。 server 127.0.0.1:28080 weight=2; #集群的服务器列表,最终的请求会被传到这里执行 } #当前的Nginx的配置 server { listen 80;#监听80端口,可以改成其他端口 server_name localhost;#当前服务的域名 #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://netitcast.com; # 如果请求为localhost:80 则交给名称为netitcast.com的Nginx集群来处理 proxy_redirect default; } #location / { # root html; # index index.html index.htm; #} #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 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; # } #} }
4.修改tomcat配置文件
把各自对应的:8005,8080,8009 三个端口修改为1/2/3 8005,1/2/3 8080,1/2/3 8009
5.修改tomcat的webapps/ROOT/index.jsp文件添加标记,区分tomcat
6.启动nginx和tomcat
7.访问页面,查看效果
http://localhost/index.jsp
多次刷新页面,会按照权重访问tomcat1/2/3中的一个。
8.停止nginx
nginx.exe -s stop
nginx.exe -s quit
补充:
server { listen 81; server_name localhost; #charset koi8-r; access_log logs/access_ota.log main; location ^~ /static/fileuploads/ { alias /data/nfs/static/fileuploads/; } location ^~ /mypro/ { proxy_set_header Host $host:3899; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #proxy_pass http://localhost:8082; proxy_pass http://172.77.111.12:8080; proxy_redirect off; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }
把请求为nginx服务器IP地址, http:IP/mypro/访问请求分发到(转到)http://172.77.111.12:8080执行
也就是说,我们可以通过一台Nginx服务器,将不同的请求分发到不同的IP上。
我们可以在不同的IP上部署Tomcat项目,然后通过Nginx服务器统一域名进行管理,通过端口+项目名 灵活的分发请求。
参考:http://blog.csdn.net/wang379275614/article/details/47778201