windows下Nginx+Tomcat实现集群以及Nginx实现动静分离
最近学习了点Nginx的基础配置,是真的强大。
简单来说Nginx的作用:
- 负载均衡,实现项目的集群分布。意思就是同一个项目可以分别放在不同的服务器里,通过Nginx配置管理这多个服务器,这样子可以分担服务器压力,将流量按负载均衡算法分配给这些不同的服务器,但是访问的都是同一个项目。
- 动静分离,实现项目的静态资源和动态资源分离访问。Nginx访问静态资源的速度比Tomcat快,所以一般都是静态资源如html,css等放在Nginx服务器,而动态资源,如:jsp,json放在tomcat中访问。
最后实现的效果图:
解释一下:动静分离:
静态资源和动态资源的分离访问:静态资源通过Nginx直接返回,动态资源通过Nginx从Tomcat中获取。Tomcat集群:
随机或者有序访问Tomcat服务器群(同一个项目),从其中的一个服务器中获取动态资源。
首先:windows本地得创建多个Tomcat服务器然后启动,本次实验中我就创建两个。
然后:修改 Tomcat 的端口设置。分别进入两个 Tomcat 服务器的 conf 目录,打开 server.xml 配置文件并做修改。
其次:将 Tomcat1 的三个端口分别修改为 8081、8082、8083,浏览器访问 Tomcat1 使用的端口号为 8082,之后修改 Tomcat2 的三个端口号,分别为 8084、8085、8086,浏览器访问 Tomcat2 使用的端口号为 8085。
服务器配置完成,然后再来配置Nginx服务器,进入D:\nginx-1.12.2\conf打开nginx.conf修改配置文件:
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 HA-tomcat{
server localhost:8082;
server localhost:8085;
}
server {
listen 8888;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location ^~ /static/ {
## 匹配任何以 /static/ 开始的请求,并停止匹配 其它location
root D:\\nginx-1.12.2;
}
#以~或~*开头表示正则匹配,~*表示正则不区分大小写
#~ 波浪线表示执行一个正则匹配,区分大小写
#~* 表示执行一个正则匹配,不区分大小写
#^~ ^~表示普通字符匹配,如果该选项匹配,只匹配该选项,不匹配别的选项,一般用来匹配目录
location ~* \.(gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ {
# 匹配以 gif|jpg|jpeg|bmp|png|ico|txt|js|css结尾的请求.
# 但是所有 /static/ 目录的请求将由 /static/处理.
root D:\\nginx-1.12.2;
}
location / {
# 匹配任何请求,因为所有请求都是以"/"开始
# 但是更长字符匹配或者正则表达式匹配会优先匹配
root html;
index index.html index.htm;
proxy_pass http://HA-tomcat;
}
#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;
# }
#}
}
然后开启两个tomcat服务器和Nginx即可访问集群tomcat服务器
动态资源访问:
http://localhost:8888
静态资源访问:
http://localhost:8888/12.jpg
http://localhost:8888/static/1.jpg
我这里将默认端口80修改为8888
因为开启Nginx的时候它报错说我80端口被pid为4的程序占用,这个4是system管理内存的一个东西,无法强制删除,我只能修改nginx监听端口为8888.
最后看一下nginx的三个常用命令:
开启:
D:\nginx-1.12.2>start nginx
重启:
D:\nginx-1.12.2>nginx -s reload
关闭:
D:\nginx-1.12.2>nginx -s stop