nginx代理至tomcat負(fù)載均衡

Nginx+tomcat是目前主流的Java web架構(gòu),如何讓nginx+tomcat同時(shí)工作呢,也可以說如何使用nginx來反向代理tomcat后端均衡呢?直接安裝配置如下:

1、java JDK安裝

image.png

2、Nginx安裝

image.png

3、Tomcat安裝:

image.png

如果需要修改tomcat發(fā)布目錄為自己制定的目錄,需要做如下調(diào)整,創(chuàng)建兩個(gè)發(fā)布目錄:

mkdir -p /usr/webapps/{www1,www2}

1.編輯vi /usr/local/tomcat1/conf/server.xml 在最后</Host>前一行加下內(nèi)容:

<Context path="" docBase="/usr/webapps/www1" reloadable="false"/>

2.編輯vi /usr/local/tomcat2/conf/server.xml 在最后</Host>前一行加下內(nèi)容:

<Context path="" docBase="/usr/webapps/www2" reloadable="false"/>

3.tomcat1發(fā)布目錄內(nèi)容:

<html>
<body>
<h1>TOMCAT_1 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>

4.tomcat2發(fā)布目錄內(nèi)容:

<html>
<body>
<h1>TOMCAT_2 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>

然后訪問http://ip:8080、8081查看測(cè)試內(nèi)容。

5、Nginx+tomcat整合:

整合主要是修改nginx.conf配置,給一個(gè)完整的nginx.conf線上配置,部分參數(shù)可以自己根據(jù)實(shí)際需求修改:
user www www;
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
include mime.types;
default_type application/octet-stream;
fastcgi_intercept_errors on;
charset utf-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 4k;
large_client_header_buffers 4 32k;
client_max_body_size 300m;
sendfile on;
tcp_nopush on;

keepalive_timeout 60;

tcp_nodelay on;
client_body_buffer_size 512k;

proxy_connect_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 5;
proxy_buffer_size 16k;
proxy_buffers 4 64k;
proxy_busy_buffers_size 128k;
proxy_temp_file_write_size 128k;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $request_time $remote_addr';

upstream web_app {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}

chinaapp.sinaapp.com

server {
listen 80;
server_name chinaapp.sinaapp.com;
index index.jsp index.html index.htm;
#發(fā)布目錄/data/www
root /data/www;

location /   
{   
proxy_next_upstream http_502 http_504 error timeout invalid_header;   
proxy_set_header Host  $host;   
proxy_set_header X-Real-IP $remote_addr;   
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
proxy_pass http://web_app;   
expires      3d;   
}   

}

}

注* server段 proxy_pass定義的web_app需要跟upstream 里面定義的web_app一致,否則server找不到均衡。

如上配置,nginx+tomcat反向代理負(fù)載均衡配置完畢,如果要做動(dòng)靜分離,只需要在nginx添加如下配置就OK了。

配置Nginx動(dòng)靜分離

location ~ .*.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$

{

root /data/www;

expires定義用戶瀏覽器緩存的時(shí)間為3天,如果靜態(tài)頁(yè)面不常更新,可以設(shè)置更長(zhǎng),這樣可以節(jié)省帶寬和緩解服務(wù)器的壓力

expires 3d;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容