1. 编辑nginx/conf/nginx.conf
关于nginx的安装,请查看【Linux编译安装Nginx】
假设nginx安装的目录在 /etc/softwares/nginx
cd /etc/softwares/nginx/conf/
vi nginx.conf
- nginx.conf
worker_processes 1;
events {
#工作模式及连接数上限
use epoll;
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
include mime.types;
default_type application/octet-stream;
#代理
include proxy.conf;
#设定请求缓冲
server_names_hash_bucket_size 128;
client_header_buffer_size 32K;
large_client_header_buffers 4 32k;
# client_max_body_size 8m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
#启动gzip压缩
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;
#此处为你tomcat的地址,可以写多个tomcat地址,这里假设两个tomcat都在同一台服务器的不同端口
upstream tomcat_pool {
server 192.168.1.7:8081 weight=4 max_fails=2 fail_timeout=30s;
server 192.168.1.7:8082 weight=4 max_fails=2 fail_timeout=30s;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.jsp index.html index.htm;
}
#所有JSP的页面均交由tomcat处理
location ~ \.(jsp|jspx|dp)?$ {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://tomcat_pool;
}
#设定访问静态文件直接读取不经过tomcat
location ~ .*\.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
expires 30d;
}
location ~ .*\.(js|css)?$ {
expires 1h;
}
#设定访问日志的存放路径
#access_log logs/study.log main;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
- proxy.conf
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 10m;
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;
2. 安装tomcat并且配置相关的内容
安装tomcat的教程,请查看【Linux安装Tomcat】
- 安装完成后,复制一份tomcat出来
cp -r /etc/softwares/tomcat9 /etc/softwares/tomcat8081
- 进入目录
cd tomcat8081/webapps/
- 删除非必要文件夹/文件(这部分,其实可以不用管,如果不了解该操作可能引起的问题,建议不要进行操作哦)
rm -rf docs examples host-manager manager
cd ROOT
rm -f *.png *.svg *.ico *.txt *.css
- 修改index.jsp
vi index.jsp
<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
</head>
<body>
<h1>8081</h1>
</body>
</html>
- 修改server.xml
cd /etc/softwares/tomcat8081
vi conf/server.xml
server port 修改
<Server port="8006" shutdown="SHUTDOWN">
connector port 修改
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
- 复制一份tomcat8081到tomcat8082
cp /etc/softwares/tomcat8081 /etc/softwares/tomcat8082
然后按照上方的内容,对index.jsp,server.xml进行修改类似的修改,如果还需要更多的tomcat。则复制更多的tomcat出来即可
3. 启动并且测试
- 启动tomcat8081 和 tomcat8082
cd /etc/softwares
tomcat8081/bin/startup.sh
tomcat8082/bin/startup.sh
启动nginx(如果没有启动的话)
/etc/softwares/nginx/sbin/nginx
重新加载nginx(如果已经启动)
/etc/softwares/nginx/sbin/nginx -s reload
- 测试
curl -i http://192.168.1.7/index.jsp
轮询
如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~
网友评论