nginx 模拟动静分离:
基于负载均衡和nginx静态代理来实现
动态资源由tomcat来调用,静态资源由nginx来调用
步骤:
1.准备三个tomcat,端口号分别为8081、8181、8281
2.将项目部署在响应的tomcat中 ,注意:需要将静态资源放在Linux 的其他目录下,三个tomcat共享一个静态资源文件夹(这里放 在/opt/static)
3.配置/usr/local/nginx/conf/nginx.conf文件
配置文件:# 配置动态负载均衡:server myserver {192.168.113.129:8080 weight=5;192.168.113.129:8180 weight=2;192.168.113.129:8280 weight=3;}
配置静态负载均衡:
server mystaticserver {192.168.113.129:89 weight=1;192.168.113.129:90 weight=1;}
定义动态tomcat服务器
server {
listen : 80 ; # 监听80端口
server_name ; # 域名或者ip
location /Easyui_maven/ { # 设置动态资源拦截路径
proxy_pass http://myserver; # 将请求交给响应的负载处理
}
location / { # 只输入域名访问欢迎界面
root html;
index index.html index.htm;
}
location ~ .*/() { # 静态资源
proxy_pass http://mystaticserver; # 交给静态资源处理
}
}
定义静态nginx服务器,
server {
listen : 89 ;# 监听端口
server_name ; # 网站别名
# 拦截路径: 正则表达式,所有后缀为以下资源的路径都被拦截
location ~ .*/(jpg|image|images|js|jsp|png|css|) {
# 文件路径
root /opt/static
}
}
server {
listen : 90; 监听端口:90
root /opt/static;
}
4.启动nginx(在/usr/local/nginx/sbin/ 下执行nginx)
5.启动三个tomcat(在tomcat的bin目录下执行start.sh文件)
6.打开浏览器输入地址:
-------------------------------------------------------------我是分界线------------------------------------------------------------------------------------
下面贴上代码块
#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;# upstream myserver{# server 192.168.113.129:8080;# }upstream myserver { # 动态server 192.168.113.129:8080 weight=5;server 192.168.113.129:8180 weight=2;server 192.168.113.129:8280 weight=3;}upstream mystaticserver { # 静态资源,均衡的名字中不要含有_ 否测可能出错server 192.168.113.129:89 weight=1; # 这里的端口号手自己定义的,不需要和tomcat的同一,建议最好不要和tomcat的端口号重复server 192.168.113.129:90 weight=1; }sendfile on;#tcp_nopushon;#keepalive_timeout 0;keepalive_timeout 65;#gzip on; server {listen80; #监听的端口server_name ; # 域名或ip#charset koi8-r;# access_log logs/host.access.log main;# location / { # 访问路径配置# proxy_pass http://myserver;# root html; # 根目录#index index.html index.htm; #默认首页# } http://192.168.113.129:8080/EasyUI_maven/user/getuslocation / {root html;index index.html index.htm;}location /easyui/ { # 配置动态资源路径proxy_pass http://myserver;index index.html index.htm;}#匹配静态资源路径location ~ .*/(css|js|img|images|png) {proxy_pass http://mystaticserver;}# root /opt/static;# index 黑客帝国.html;#}#location ~ .*\.(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { #root /opt/static;#}#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;#}#}server{ # 静态资源访问路径listen 89;server_name ;location ~ .*/(css|js|img|images|png) {root /opt/static ;} }server{ #静态资源访问路径listen 90;server_name ;location ~ .*/(css|js|img|images|png) { root /opt/static ;}}# 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_certificatecert.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;# }#}}