Nginx+Tomcat负载均衡、动静分离群集(群集)
以下是一个基于Nginx和Tomcat的负载均衡和动静分离的配置示例。
- Nginx配置文件(nginx.conf):
user nginx;
worker_processes 1;
events {
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;
#tcp_nopush on;
keepalive_timeout 65;
# 设置动静分离
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
proxy_connect_timeout 600;
proxy_read_timeout 600;
proxy_send_timeout 600;
proxy_buffer_size 16k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
# 静态文件缓存设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css;
gzip_vary on;
# 静态文件目录
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
# 处理静态文件请求
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|css|js)$ {
expires 30d;
}
}
# 负载均衡配置
upstream backend {
server tomcat1.example.com:8080;
server tomcat2.example.com:8080;
}
# 动态请求代理到Tomcat服务器
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
- 在这个配置中,Nginx作为反向代理服务器接收来自客户端的HTTP请求,并根据请求的URL将其转发到后端的Tomcat服务器群集。它同时处理静态文件请求,并且将静态内容缓存以提高性能。
- 确保Nginx配置文件中的
upstream
模块指定了所有Tomcat服务器的地址
评论已关闭