nginx+tomcat集群
一、nginx安装
1、安装nginx需要的依赖包
yum -y install gcc zlib zlib-devel pcre-devel openssl openssl-devel
2、下载nginx安装包
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar -xvf nginx-1.16.1.tar.gz
3、nginx安装
cd ./nginx-1.16.1/
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
//编译安装
make && make install
4、nginx配置文件路径
/usr/local/nginx/conf/nginx.conf
5、检测是否安装成功
/usr/local/nginx/sbin/nginx
命令 | 作用 |
---|---|
nginx | 启动服务 |
nginx -s reload | 重载服务 |
nginx -s stop | 停止服务 |
二、tomact安装
1、下载
cd /usr
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.96/bin/apache-tomcat-7.0.96.tar.gz
//解压缩
tar -xvf apache- tomcat-7.0.96.tar.gz
//重命名为 tomcat7
mv apache-tomcat-7.0.96 tomcat7
//启动测试
/usr/tomcat7/bin/startup.sh
//访问localhost:8080 ,测试是否安装成功
2、集群tomcat的配置
vi /usr/tomcat7/webapps/ROOT/index.jsp
//复制一份tomcat
cp -r tomcat7 ./tomcat7-1
vi ./tomcat7-1/webapps/ROOT/index.jsp
修改Home为8081,方便标识
修改tomcat7-1的配置文件(共三处)
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
三、nginx配置 http集群
1、修改nginx 配置文件
vi /usr/local/nginx/conf/nginx.conf //添加如下信息
#gzip on;
upstream mytomcats{
server localhost:8080 weight=1; #weight权重
server localhost:8081 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://mytomcats;
}
2、重载nginx
访问localhost 测试是否可以达到负载 刷新页面,Home8080或者Home8081交替变化,(默认负载均衡策略:轮询)四、同时支持http和https
证书需手动申请,放置到服务器目录下,可用 acme.sh 申请免费证书
server {
listen 443 ssl;
server_name 你的域名或ip;
ssl_certificate /ca/server/server.crt;
ssl_certificate_key /ca/server/server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols SSLv2 SSLv3 TLSv1;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
proxy_pass http://mytomcats;
}
}
五、强制https
server {
listen 80;
server_name 域名或ip;
#rewrite ^(.*) https://$server_name$1 permanent;
rewrite ^(.*)$ https://$host$1 permanent;
}
六、关闭404无用日志
在server{…}添加如下内容
location = /favicon.ico {
log_not_found off;
access_log off;
}
七、参考内容
https://www.cnblogs.com/saneri/p/5391821.html
https://www.cnblogs.com/fengyuduke/p/11232662.html