试验环境:
两台redhat虚拟机
一台:192.168.1.20
二台:192.168.1.11
(1.20上面装nginx,jdk,tomcat ; 1.11上面装jdk,tomcat。)
1.安装jdk(这是必须的,网上版本很多,这里也不详细说明)
2.安装tomcat(不要问我tomcat怎么弄,特意转载了一个说的很详细的博客,如果你说看不懂我只能是醉了)
3.安装nginx
1.下载pcre (http://www.pcre.org/ 官网下载)
tar jxvf pcre-8.12.tar.bz2
cd pcre-8.12
./configure
make && make install
2.下载nginx
( http://nginx.org/官网下载)
3.解压 tar zxvf nginx-1.7.8.tar.gz
cd nginx-1.7.8
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
编译时报这样的错:
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
yum -y install openssl openssl-devel 安装完再重新编译就Ok了。
4. nginx+tomcat负载:
整合主要是修改nginx.conf配置,给一个完整的nginx.conf线上配置,部分参数可以根据实际需求修改。
#user nobody;
worker_processes 4;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
pid /usr/local/nginx/logs/nginx.pid;
events {
use epoll;
worker_connections 10240;
}
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;
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;
#tcp_nopush on;
#keepalive_timeout 0;
#gzip on
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;
upstream web {
server 192.168.1.20:8080 weight=1;
server 192.168.1.11:8080 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
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;
expires 3d;
}
#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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
这样就Ok了。
关闭其中一台的tomcat,去访问192.168.1.20也是能正常访问的。
注意:
访问192.168.1.11是不行的,nginx已经整合了,所以访问1.20就可以了。因为我对Nginx不是很熟悉,所以当我搭建完nginx+tomcat负载之后,我以为能得到的结果并没有出现,我以为我做的有问题,纠结了半天,最后只能呵呵了。
因为我的两个tomcat端口都被我改成了8080,所以我访问tomcat的时候是要加8080的,也就是http://192.168.1.20:8080 这样可以访问到应用,nginx+tomcat负载完成之后,访问http://192.168.1.20 也可以直接访问到应用,其实这样就对了,你可以试着把20上面的tomcat关了,继续访问,也是可以访问到的。