一、实验环境
服务器 | IP地址 | 软件版本 |
tomcat | 172.16.88.1 | apache-tomcat-7.0.42 |
nginx | 172.16.88.4 | nginx-1.4.7 |
nginx安装参考指南:http://584014981.blog.51cto.com/8605371/1403791
tomcat安装参考指南:http://584014981.blog.51cto.com/8605371/1409482
二、nginx反向代理
1、修改nginx配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf #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; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { proxy_pass http://172.16.88.1:8080/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
注:(在tomcat上设置默认虚拟主机为www.gulong.com,
# vim /usr/local/tomcat/conf/server.xml <Engine name="Catalina" defaultHost="www.gulong.com">)
2、nginx做图片缓存
修改nginx配置文件/etc/nginx/nginx.conf
#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; sendfile on; keepalive_timeout 65; proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G; //新建缓存路径与相关属性 upstream backend { //建立后端tomcat服务器集群 server 172.16.88.1:8080; } server { listen 80; server_name localhost; location / { proxy_pass http://backend; //转发给后端服务器 } location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" { //缓存图片与静态内容 proxy_pass http://backend; proxy_cache first; proxy_cache_valid 200 24h; //200状态缓存24小时 proxy_cache_valid 302 10m; //302状态缓存10分钟 add_header X-Cache-Status $upstream_cache_status;}//在http头部增加一个字段显示是否命令缓存 error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
创建缓存目录:
[root@localhost ~]# mkdir -pv /nginx/cache [root@localhost ~]# service nginx restart
测试:
大家可以看到我们访问的所有的静态内容都是命中的,X-Cache-Status: HIT,下面们来看一下缓存的目录:
3、实现动静分离
目的:将静态内容缓存在nginx服务器上,让用户请求的静态内容到nginx去取,动态内容到tomcat服务器上去取,
编辑nginx配置文件/etc/nginx/nginx.conf
#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; sendfile on; keepalive_timeout 65; proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G; upstream backend { server 172.16.88.1:8080; } server { listen 80; server_name localhost; location / { root html ; rewrite ^/ http://172.16.88.1:8080/index.jsp last; //重定向后端jsp页面 } location ~* "\.(jsp|do)"{ //访问jsp等页面,转向tomcat proxy_pass http://backend; } location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" { proxy_pass http://backend; proxy_cache first; proxy_cache_valid 200 24h; proxy_cache_valid 302 10m; add_header X-Cache-Status $upstream_cache_status;} error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
测试,游览器输入172.16.88.4
从上图看静态数据从nginx缓存中取得!
jsp页面还是从后端tomcat取得!
到此,nginx的反向代理已经介绍完毕!下次介绍下apache对tomcat做反向代理!