Nginx+Tomcat實現反向 Proxy與動靜分離

來源:互聯網
上載者:User


1. 什麼是動靜分離 所謂動靜分離就是通過nginx(或apache等)來處理使用者端請求的靜態頁面,tomcat(或weblogic)處理動態網頁面,從而達到動靜頁面訪問時通過不同的容器來處理。2. 為什麼做動靜分離  Nginx處理靜態頁面的效率遠高於tomcat,所有我們只讓tomcat幹自己擅長的事情,以事實說話,我們通過ab進行對一個靜態頁面訪問做壓力測試。
  www.2cto.com  1) 做動靜分離後nginx處理靜態頁面的壓力測試:# ab -n 1000 -c 200 http://10.10.11.91/index.html輸送量為:1.2M
  www.2cto.com  2) 未做動靜分離而是直接由tomcat處理同一個靜態頁面的壓力測試:# ab -n 1000 -c 200 http://10.10.11.91:8080/index.html輸送量為:0.08M通過上面的壓力測試結果,所有請求由tomcat處理與動靜分離相比毫無疑問動靜分離才是王道!3. 動靜分離邏輯圖4. Nginx與tomcat部署由一鍵安裝middleware_install_v.1.0.3.tar.gz進行部署,這裡不再贅述。5. Nginx配置nginx需要配置的地方如下:
  www.2cto.com  server {listen 80;server_name www.test.com 10.10.11.91;index index.html index.htm index.jsp index.doroot /usr/local/nginx/html; #由nginx處理靜態頁面location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {expires 30d;}location ~ .*\.(js|css)?$ {expires 1h;}location ~ (\.jsp)|(\.do)$ {proxy_pass http://10.10.11.91:8080; #以jsp和do請求的頁面經過nginx反向 Proxy由tomcat處理proxy_redirect off;proxy_set_header HOST $host;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;client_max_body_size 10m;client_body_buffer_size 128k;proxy_connect_timeout 90;proxy_send_timeout 90;proxy_read_timeout 90;proxy_buffer_size 4k;proxy_buffers 4 32k;proxy_busy_buffers_size 64k;proxy_temp_file_write_size 64k;}} 6. 驗證動靜分離的效果我在tomcat的根目錄ROOT下建立了index.html、test.jsp、test.do三個測試檔案:我在nginx的根目錄/usr/local/nginx/html下建立測試首頁index.html直接存取tomcat的靜態首頁:直接存取tomcat的jsp頁面:直接存取tomcat的.do頁面:現在進行通過nginx進行訪問:Nginx的首頁:
  www.2cto.com  查看nginx處理jsp頁面的請求: 查看nginx處理.do頁面的請求:至此,nginx與tomcat已實現了反向 Proxy動靜分離。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.