目录:
1、安装nginx
2、安装tomcat
3、nginx+tomcat实现代理
4、nginx+tomcat简单负载均衡
1、安装nginx
直接通过yum方式安装,默认centos7的基本源中没有nginx,需要先安装扩展源:epel
# yum -y install epel-release
# yum -y install nginx
安装完成!nginx配置文件目录为:/etc/nginx 下
启动nginx
# systemctl start nginx
启动成功!主机浏览器访问:http://ip 此时发现无法访问,这是因为centos防火墙的原因,需要将80端口加入到防火墙例外端口里面
添加80端口为例外
# firewall-cmd --zone=public --add-port=80/tcp --permanent
重启防火墙
# systemctl restart firewalld
主机浏览器再访问:http://192.168.0.101 成功进入nginx首页!
2、安装tomcat
在apache官网下载:apache-tomcat-7.0.78.tar.gz ,放到/opt 目录下
解压tomcat
# tar -zxvf apache-tomcat-7.0.78.tar.gz
启动tomcat
进入到解压出来的tomcat bin目录
# cd /opt/apache-tomcat-7.0.78/bin
执行启动命令
# ./startup.sh
启动成功!主机浏览器访问:http://192.168.0.101:8080 此时发现无法访问,同理添加8080端口为例外
3、配置nginx代理
编辑nginx配置文件
# vi /etc/nginx/nginx.conf
server {
listen 80;
server_name 192.168.0.101;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://192.168.0.101:8080;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
保存并退出:# wq!
重新加载nginx使更改生效
# nginx -s reload
主机浏览器访问:http://192.168.0.101 结果出现502错误,这是红帽和centos6.6版出现的问题,解决方案如下
# yum -y install policycoreutils-python
# cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
# semodule -i mynginx.pp
再访问:http://192.168.0.101 成功进入tomcat首页
4、多个tomcat实现负载均衡
upstream tomcats{
server 192.168.0.101:8080;
server 192.168.0.101:8081;
}
server {
listen 80;
server_name 192.168.0.101;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tomcats;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
更多nginx配置请见:nginx+tomcat 负载均衡配置