一、實驗環境
服務器 | 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做反向代理!