文章目录
一.Nginx安装
确保虚拟机可以联网,安装nginx需要其他依赖
# 解压pcre
tar -zxvf pcre-8.37.tar.gz
# 进入解压目录
cd pcre-8.37
# 执行
./configure
# 编译并安装
make && make install
# 检查
pcre-config --version
# 安装其他依赖
yum -y install zlib zlib-devel openssl openssl-devel libtool make
configure: error: no acceptable C compiler found in $PATH
# 在执行./configure报错,提示需要c编辑器
yum -y install gcc gcc-c++
# 解压,执行,编译,安装
tar -xzvf nginx-1.23.1.tar.gz
cd nginx-1.23.1/
./configure && make && make install
# 检查
cd /usr/local/nginx/sbin
# 添加环境变量
vim /etc/profile
# 刷新
source /etc/profile
/etc/profile添加
#nginx
NGINX_HOME=/usr/local/nginx
export PATH=$PATH:${NGINX_HOME}/sbin
# 查看开放端口
firewall-cmd --list-all
# 设置开放端口号
firewall-cmd --add-port=80/tcp --permanent
# 重启防火墙
systemctl restart firewalld.service
二.Nginx常用命令
# 查看版本
nginx -v / -V
# 启动
nginx
# 停止
nginx -s stop
# 重新加载
nginx -s reload
# 测试配置
ngixn -t
三.反向代理
3.1.tomcat安装
JDK: https://repo.huaweicloud.com/java/jdk/
Tomcat: https://downloads.apache.org/tomcat/tomcat-8/
# 解压jdk
tar -zxvf jdk-8u202-linux-x64.tar.gz
# 移动&&重命名
mv jdk1.8.0_202/ /usr/local/jdk
# 解压tomcat
tar -zxvf apache-tomcat-8.5.82.tar.gz -C /usr/local
# 重命名
mv apache-tomcat-8.5.82/ tomcat
# 添加环境变量
vim /etc/profile
# 刷新
source /etc/profile
/etc/porfile配置
#jdk
JAVA_HOME=/usr/local/jdk
export PATH=$PATH:${JAVA_HOME}/bin
export CLASSPATH=$CLASSPATH:${JAVA_HOME}/lib
export JRE_HOME=${JAVA_HOME}/jre
# 开放8080端口
firewall-cmd --add-port=8080/tcp --permanent
# 重启防火墙
firewall-cmd --reload
补充
# 后续对防火墙关闭,不嫌麻烦可以开放端口
systemctl disable firewalld.service
systemctl stop firewalld.service
3.2.反向代理一
-
C:\Windows\System32\drivers\etc\hosts 配置域名映射
# 虚拟机ip 域名 192.168.199.101 www.123.com
-
配置nginx
# 切换到nginx配置目录
cd /usr/local/nginx/conf
vim nginx.conf
nginx.conf配置
server {
listen 80;
server_name www.123.com;
location / {
proxy_pass http://127.0.0.1:8080;
index index.html index.htm;
}
}
3.3.反向代理二
效果: http://192.168.199.101:9001/abc 跳转到 8081端口
http://192.168.199.101:9001/def 跳转到 8082端口
3.3.1.准备工作 - 安装tomcat,分别为8080,8081
# 解压tomcat
tar -zxvf apache-tomcat-8.5.82.tar.gz -C /usr/local
# 重命名
mv apache-tomcat-8.5.82/ tomcat8081
# 复制
cp -rp tomcat8081/ tomcat8082
# 在 ./conf 修改server.xml
server.xml配置修改 Tomcat/8.5.82
<Server port="8015" shutdown="SHUTDOWN"> <!--将port修改为8015,8025,与原有port不同即可-->
<Connector port="8081" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /> <!--修改port分别为8081,8082-->
分别为tomcat8081,tomcat8082创建页面
# tomcat8081
cd /usr/local/tomcat8081/webapps
mkdir abc
vim abc/index.html
# 添加内容
This is abc/index.html, port is 8081
tomcat8082同理
# tomcat8082
cd /usr/local/tomcat8082/webapps
mkdir def
vim def/index.html
# 添加内容
This is def/index.html, port is 8082
通过浏览器进行初步测试,注意在修改完成后对tomcat进行重新启动
http://192.168.199.101:8081/abc/index.html
http://192.168.199.101:8082/def/index.html
3.3.2.反向代理
# 配置nginx.conf
cd /usr/local/nginx/conf
vim nginx.conf
相关配置
server {
listen 9001;
server_name 192.168.199.101;
location /abc {
proxy_pass http://127.0.0.1:8081/abc;
index index.html;
}
location /def {
proxy_pass http://127.0.0.1:8082/def;
index index.html;
}
}
# 测试配置是否成功
nginx -t
# 重启nginx
nginx -s reload
测试反向代理
http://192.168.199.101:9001/abc 跳转到 8081端口
http://192.168.199.101:9001/def 跳转到 8082端口
四.负载均衡
准备工作见3.3,需要两台tomcat.
创建相同的工程,以abc工程为例
# 复制tomcat8081的abc到tomcat8082
cd /usr/local/tomcat8081/webapps
cp -pr abc /usr/local/tomcat8082/webapps
vim abc/index.html
#添加内容
This is abc/index.html, port is 8082 ----------
# 配置nginx.conf
cd /usr/local/nginx/conf
vim nginx.conf
相关配置
#配置反向代理
upstream tomcatservers {
server 192.168.199.101:8081;
server 192.168.199.101:8082;
}
server {
listen 80;
server_name 192.168.199.101;
location /abc {
proxy_pass http://tomcatservers;
proxy_connect_timeout 10;
}
}
# 测试配置
nginx -t
# 重启
nginx -s reload
五.动静分离
5.1.准备工作
# 创建目录 - /data
mkdir -p /data/www /data/image
# 分别上传一些图片,网页
5.2.具体配置
# 进入配置目录
cd /usr/local/nginx/conf/
vim nginx.conf
相关配置
server {
listen 80;
server_name 192.168.199.101;
location /www/ {
root /data/;
}
location /image/ {
root /data/;
}
}
六.高可用集群
准备工作
服务器: 192.168.199.101 192.168.199.102
安装nginx,keepalived
# 安装keepalived
yum -y install keepalived
# 检查
rpm -qa keepalived
1.配置192.168.199.101 - /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.199.101
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_http_port {
script "/usr/local/src/nginx_check.sh"
interval 2 #(检测脚本执行的间隔)
weight 2
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface ens33 //网卡
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.199.100 // VRRP H 虚拟地址
}
}
192.168.199.102备份机,修改配置文件的state为 BACKUP,priority值小于主机即可
2.编写脚本 /usr/local/src/nginx_check.sh
#!/bin/bash
A=`ps -C nginx –no-header |wc -l`
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
# 发送给备份机
scp -r nginx_check.sh root@192.168.199.102:/usr/local/src/
3.启动测试
# 启动Keepalived
systemctl start keepalived.service
# 启动nginx
nginx
浏览器输入虚拟IP地址 - 192.168.199.100
# 发现nginx欢迎页面后
# 关闭主机Keepalived
systemctl stop keepalived.service
# 关闭主机nginx
nginx -s stop
此时刷新页面,依旧是nginx欢迎页面,表示搭建成功