溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

發(fā)布時(shí)間:2022-04-29 16:08:28 來源:億速云 閱讀:143 作者:iii 欄目:大數(shù)據(jù)

本篇內(nèi)容主要講解“Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡”吧!

一.nginx簡介:

nginx一個高性能的http和反向代理服務(wù)器, 具有很高的穩(wěn)定性和支持熱部署、模塊擴(kuò)展也很容易。當(dāng)遇到訪問的峰值,或者有人惡意發(fā)起慢速連接時(shí),也很可能會導(dǎo)致服務(wù)器物理內(nèi)存耗盡頻繁交換,失去響應(yīng),只能重啟服務(wù)器,nginx采取了分階段資源分配技術(shù),處理靜態(tài)文件和無緩存的反向代理加速,實(shí)現(xiàn)了負(fù)載均衡和容錯,在這樣高并發(fā)的訪問情況下,能經(jīng)受起高并發(fā)的處理。

二.nginx安裝與配置

第一步:下載nginx 安裝包  

第二步:在linux上安裝nginx

#tar zxvf nginx-1.7.8.tar.gz //解壓

#cd nginx-1.7.8

#./configure --with-http_stub_status_module --with-http_ssl_module//啟動server狀態(tài)頁和https模塊

會報(bào)缺少pcre library錯誤,如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

這時(shí)先執(zhí)行第三步安裝pcre ,然后在3執(zhí)行一下,這就可以了

4.make && make install //編譯并安裝

5.測試一下安裝配置是否正確,nginx安裝在/usr/local/nginx

#/usr/local/nginx/sbin/nginx -t,如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

第三步:在linux上安裝pcre

#tar zxvf pcre-8.10.tar.gz //解壓

cd pcre-8.10

./configure

make && make install//編譯并安裝

三.nginx +tomcat 實(shí)現(xiàn)動靜態(tài)分離

 動靜態(tài)分離就是nginx處理客戶端的請求的靜態(tài)頁面(html頁面)或者圖片,tomcat處理客戶端請求的動態(tài)頁面(jsp頁面),因?yàn)閚ginx處理的靜態(tài)頁面的效率高于tomcat。

第一步:我們要配置nginx文件

 #vi /usr/local/nginx/conf/nginx.conf

 #user nobody; 
worker_processes 1; 
error_log logs/error.log; 
pid    logs/nginx.pid; 
 
events { 
  use epoll; 
  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; 
  sendfile    on; 
keepalive_timeout 65; 
gzip on;  
gzip_min_length 1k;  
gzip_buffers   4 16k;  
gzip_http_version 1.0;  
gzip_comp_level 2;  
gzip_types text/plain application/x-javascript text/css application/xml;  
gzip_vary on;  
  server { 
    listen    80 default; 
    server_name localhost; 
    <span style="color:#ff0000;"> location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ //由nginx處理靜態(tài)頁面</span> 
       {  
         root  /usr/tomcat/apache-tomcat-8081/webapps/root;  
          expires   30d; //緩存到客戶端30天 
        }  
    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; 
    } 
     <span style="color:#ff0000;"> location ~ \.(jsp|do)$ {//所有jsp的動態(tài)請求都交給tomcat處理 </span> 
      <span style="color:#ff0000;"> proxy_pass http://192.168.74.129:8081; //來自jsp或者do的后綴的請求交給tomcat處理</span> 
      proxy_redirect off; 
      proxy_set_header host $host;  //后端的web服務(wù)器可以通過x-forwarded-for獲取用戶真實(shí)ip 
      proxy_set_header x-real-ip $remote_addr; 
      proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
      client_max_body_size 10m;  //允許客戶端請求的最大單文件字節(jié)數(shù) 
      client_body_buffer_size 128k; //緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù) 
       proxy_connect_timeout 90;  //nginx跟后端服務(wù)器連接超時(shí)時(shí)間 
       proxy_read_timeout 90;   //連接成功后,后端服務(wù)器響應(yīng)時(shí)間 
       proxy_buffer_size 4k;   //設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 
       proxy_buffers 6 32k;    //proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 
      proxy_busy_buffers_size 64k;//高負(fù)荷下緩沖大?。╬roxy_buffers*2) 
      proxy_temp_file_write_size 64k; //設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳 
    } 
    
  }  
 
}

第二步:在tomcat 下的webapps/root下新建index.html靜態(tài)頁面,如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

第三步:啟動nginx服務(wù)

 #sbin/nginx   如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

第四步:我們頁面訪問 能正常顯示正常的內(nèi)容,如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

第五步:測試nginx 和tomcat高并發(fā)的情況下處理靜態(tài)頁面性能如何?

采用了 linux  ab網(wǎng)站壓力測試命令來測試一下性能

1.測試一下nginx 處理靜態(tài)頁面的性能

ab -c 100 -n 1000

 這個表示同時(shí)處理100個請求并運(yùn)行1000次index.html文件,如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

2.測試一下tomcat處理靜態(tài)頁面的性能

ab -c 100 -n 1000

這個表示同時(shí)處理100個請求并運(yùn)行1000次index.html文件,如圖所示:

Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡

相同的處理靜態(tài)文件,nginx處理的靜態(tài)性能比tomcat 好。nginx每秒能請求5388次,而tomcat只請求2609次。

總結(jié):我們在nginx配置文件中,配置靜態(tài)交給nginx處理,動態(tài)請求交給tomcat,提供了性能。

四.nginx +tomcat 負(fù)載均衡與容錯

我們在高并發(fā)的情況下,為了提高服務(wù)器的性能,減少了單臺服務(wù)器的并發(fā)壓力,我們采用了集群部署,還能解決為了避免單臺服務(wù)器掛掉,服務(wù)不能訪問這種情況下,處理容錯問題。

 第一步:我們這邊部署了兩天tomcat服務(wù)器,192.168.74.129:8081和192.168.74.129:8082

 第二步:nginx作為了代理服務(wù)器,客服端請求服務(wù)器端時(shí),采用了負(fù)載均衡來處理,這樣就能平均的把客服端請求分發(fā)到每一天服務(wù)器,這樣減少服務(wù)器端的壓力。配置nginx下的nginx.conf文件。

  #vi /usr/local/nginx/conf/nginx.conf

 #user nobody; 
worker_processes 1; 
error_log logs/error.log; 
pid    logs/nginx.pid; 
 
events { 
  use epoll; 
  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; 
  sendfile    on; 
keepalive_timeout 65; 
gzip on;  
gzip_min_length 1k;  
gzip_buffers   4 16k;  
gzip_http_version 1.0;  
gzip_comp_level 2;  
gzip_types text/plain application/x-javascript text/css application/xml;  
gzip_vary on;  
<span style="color:#ff0000;">upstream localhost_server { 
    ip_hash; 
    server 192.168.74.129:8081; 
    server 192.168.74.129:8082; 
  }</span> 
 
  server { 
    listen    80 default; 
    server_name localhost; 
    <span style="color:#ff0000;"> location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ //由nginx處理靜態(tài)頁面</span> 
       {  
         root  /usr/tomcat/apache-tomcat-8081/webapps/root;  
          expires   30d; //緩存到客戶端30天 
        }  
    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; 
    } 
     <span style="color:#ff0000;">location ~ \.(jsp|do)$ {//所有jsp的動態(tài)請求都交給tomcat處理 </span> 
      <span style="color:#ff0000;">proxy_pass http://localhost_server; //來自jsp或者do的后綴的請求交給tomcat處理</span> 
      proxy_redirect off; 
      proxy_set_header host $host;  //后端的web服務(wù)器可以通過x-forwarded-for獲取用戶真實(shí)ip 
      proxy_set_header x-real-ip $remote_addr; 
      proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; 
      client_max_body_size 10m;  //允許客戶端請求的最大單文件字節(jié)數(shù) 
      client_body_buffer_size 128k; //緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù) 
       proxy_connect_timeout 90;  //nginx跟后端服務(wù)器連接超時(shí)時(shí)間 
       proxy_read_timeout 90;   //連接成功后,后端服務(wù)器響應(yīng)時(shí)間 
       proxy_buffer_size 4k;   //設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 
       proxy_buffers 6 32k;    //proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 
      proxy_busy_buffers_size 64k;//高負(fù)荷下緩沖大?。╬roxy_buffers*2) 
      proxy_temp_file_write_size 64k; //設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳 
    } 
    
  }  
 
}

說明:

1.upstream 中的server是指向服務(wù)器的ip(域名)和端口,后面還可以帶參數(shù)

1)weight :設(shè)置服務(wù)器的轉(zhuǎn)發(fā)權(quán)重 默認(rèn)值是1。

2)max_fails : 是與fail_timeout配合使用,是指在fail_timeout時(shí)間段內(nèi),如果服務(wù)器轉(zhuǎn)發(fā)失敗次數(shù)超過max_fails設(shè)置的值,這臺服務(wù)器就不                     可用,max_fails默認(rèn)值是1

3)fail_timeout :表示在該時(shí)間段內(nèi)轉(zhuǎn)發(fā)失敗多少次就認(rèn)為這臺服務(wù)器不能用。

4)down:表示這臺服務(wù)器不能用。

 5)backup:表示使ip_hash設(shè)置的針對這臺服務(wù)器無效,只有在所有非備份的服務(wù)器都失效后,才會向服務(wù)器轉(zhuǎn)發(fā)請求。

 2.ip_hash 設(shè)置是在集群的服務(wù)器中,如果同一個客戶端請求轉(zhuǎn)發(fā)到多個服務(wù)器上,每臺服務(wù)器可能緩存同一份信息,這會造成資源的浪費(fèi),采用的ip_hash設(shè)置會把同一個客戶端第二次請求相同的信息時(shí),會轉(zhuǎn)發(fā)到第一次請求的服務(wù)器端。但ip_hash不能和weight 同時(shí)使用。

到此,相信大家對“Nginx與Tomcat怎么實(shí)現(xiàn)動靜態(tài)分離和負(fù)載均衡”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI