实现 Nginx 和多个 Tomcat 进程负载均衡
实现 Nginx 和多个 Tomcat 进程负载均衡,静态文件和媒体文件,还有不完善的地方,但总的思路是通用的:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
use epoll;
}
http
{
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
#多个tomcat进程负载均衡
upstream www_domain_com {
server localhost:8081 max_fails=3 weight=1 fail_timeout=60s;
server localhost:8082 max_fails=3 weight=1 fail_timeout=60s;
ip_hash;
keepalive 60;
}
server
{
listen 80;
server_name www.domain.com domain.com;
index /index;
location / {
proxy_pass http://www_domain_com ;
proxy_redirect off;
proxy_set_header Cookie $http_cookie;
proxy_set_header Host $host;
proxy_set_header Remote_Addr $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 80m;
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;
}
location ~ ^/static/ {
root /var/apache-tomcat-8.0.28/myproject1/ROOT/;
expires 30d;
}
location ~ ^/media/ {
root /var/apache-tomcat-8.0.28/myproject1/ROOT/;
expires 30d;
}
}
}
静态文件和媒体文件,按理来说,应该独立一个 Server 和 域名,实现独立的分离。现在是只取一个副本myproject 的静态文件和媒体文件做公共用途了。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。