提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
两台tomcat 一台nginx
nginx 192.168.116.50
tomcat1 192.168.116.30
tomcat2 192.168.116.40
systemctl stop firewalld.service
systemctl disable firewalld.service
setenforce 0
yum install -y pcre-devel zlib-devel openssl-devel gcc gcc-c++ make
useradd -M -s /sbin/nologin nginx
cd /opt
tar zxvf nginx-1.15.9.tar.gz -C /opt/
cd nginx-1.15.9/
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_flv_module \
--with-http_ssl_module
make && make install
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile =/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
chmod 754 /lib/systemd/system/nginx.service
systemctl start nginx.service
systemctl enable nginx.service
解压apache-tomcat软件包
cd /opt
rpm -ivh jdk-8u201-linux-x64.rpm
tar zxvf apache-tomcat-9.0.16.tar.gz
mv apache-tomcat-9.0.16 /usr/local/tomcat
vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/jdk1.8.0_201-amd64
export CLASSPATH=.:$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar
export PATH=$JAVA_HOME/bin:$PATH
source /etc/profile.d/java.sh
java -version
优化管理
ln -s /usr/local/tomcat/bin/startup.sh /usr/local/bin/
ln -s /usr/local/tomcat/bin/shutdown.sh /usr/local/bin/
开启服务
startup.sh
netstat -natp | grep 8080
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp #动态页面的配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test1 page</title>
</head>
<body>
<% out.println("动态页面 1,http://www.test1.com");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml
删除原host端和valve端
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" /> #新增
---》wq
shutdown.sh
startup.sh
mkdir /usr/local/tomcat/webapps/test
vim /usr/local/tomcat/webapps/test/index.jsp #动态页面的配置
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP test2 page</title>
</head>
<body>
<% out.println("动态页面 2,http://www.test2.com");%>
</body>
</html>
vim /usr/local/tomcat/conf/server.xml #修改配置文件
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true" xmlValidation="false" xmlNamespaceAware="false">
<Context docBase="/usr/local/tomcat/webapps/test" path="" reloadable="true" />
</Host>
#新增
shutdown.sh
startup.sh
#准备静态页面
echo 'this is zhb static!
' > /usr/local/nginx/html/index.html
vim /usr/local/nginx/conf/nginx.conf
配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
#gzip on;
upstream tomcat_server {
server 192.168.116.30:8080 weight=1;
server 192.168.116.40:8081 weight=1;
}
server {
listen 80;
server_name www.zhb.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location ~ .*\.jsp$ {
proxy_pass http://tomcat_server;
proxy_set_header HOST $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
也可以加上图片
#gzip on;
#配置负载均衡的服务器列表,weight参数表示权重,权重越高,被分配到的概率越大
upstream tomcat { ##名称中不要加"_"
server 192.168.116.30:8080 weight=1;
server 192.168.116.40:8080 weight=1;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#配置Nginx处理动态页面请求,将 .jsp 文件请求转发到Tomcat 服务器处理
location ~ .*\.jsp$ {
proxy_pass http://tomcatserver; #这里不要加“_”
#设置后端的 Web 服务器可以获取远程客户端的真实IP #设定后端的Web服务器接收到的请求访问的主机名(域名或IP、端口),默认host的值为proxy_pass指令设置的主机名
proxy_set_header HOST $host;
#把$remote_addr赋值给X-Real-IP(自定义),来获取源IP
proxy_set_header X-Real-IP $remote_addr;
#在Nginx作为代理服务器时,设置的IP列表,会把经过的机器ip,代理机器ip都记录下来
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* \.(gif|jpg|swf|jpeg|bmp|ico)$ { #识别图片
root /usr/local/nginx/html/img;
expires 1d;
}
location / {
root html;
index index.html index.htm;
}
下面我就用上面的配置文件简单做了一下
测试访问
访问:192.168.116.50
访问http://192.168.116.50/index.jsp
http://192.168.116.50/index.jsp 刷新一下