nginx是一个轻量级高性能的http和反向代理web服务器,优点:占用内存少,并发能力强
实验主机:192.168.200.141 192.168.200.142
1.虚拟主机
1.1基于域名:一台服务器,一个端口,部署多个网站
在nginx.conf中增加2个虚拟主机配置如下,nginx -t &&nginx -s reload
server {
listen 80;
# listen [::]:80 default_server;
server_name www.a.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html/a.com;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
# listen [::]:80 default_server;
server_name www.b.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html/b.com;
index index.html index.htm;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
然后再另一台机器上(192.168.200.142)增加hosts:
192.168.200.141 www.a.com
192.168.200.141 www.b.com
最后测试结果如下:
1.2 基于端口:一个服务器,2个端口,2个网站,不是经常用,访问方式:域名:端口
在conf.d下新增2个文件7081.conf和8081.conf
[root@k8s-master conf.d]# cat 7081.conf
server {
listen 7081;
# listen [::]:80 default_server;
server_name www.test.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html/7081;
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
[root@k8s-master conf.d]# cat 8081.conf
server {
listen 8081;
# listen [::]:80 default_server;
server_name www.test.com;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root html/8081;
index index.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
确认主配置文件nginx.conf配有此项:
[root@k8s-master conf.d]# cat ../nginx.conf|grep include
include /usr/share/nginx/modules/*.conf;
include /etc/nginx/mime.types;
# See http://nginx.org/en/docs/ngx_core_module.html#include
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/default.d/*.conf;
include /etc/nginx/default.d/*.conf;
include /etc/nginx/conf.d/*.conf;
重启服务:nginx -t&&nginx -s reload
在第二台机器上面验证:
1.3基于ip:针对于多网卡,目前本机只有1个网卡,无法实验
2.反向代理:
反向代理,是对客户端无感知的,暴露反向代理服务器ip,隐藏后端真实服务器IP。
正向代理,是客户端主动去配置,由代理服务器访问后端web服务器,隐藏客户端,暴露代理服务器
3.反向代理实验:(先安装tomcat)
3.1 去官网下载tomcat: Apache Tomcat® - Apache Tomcat 8 Software Downloads
3.2 上传tomcat包到目录,然后直接解压即可
mkdir /usr/local/tomcat
tar -zxvf apache-tomcat-8.5.92.tar.gz
cp -r apache-tomcat-8.5.92 /usr/local/tomcat/
增加tomcat服务为系统自带服务
1.修改tomcat配置文件
cd /usr/local/tomcat/apache-tomcat-8.5.92/bin
vim catalina.sh 在153行增减一下内容
#增加tomcat开机自启动
CATALINA_PID="/usr/local/tomcat/apache-tomcat-8.5.92/bin/tomcat.pid"
2.增加tomcat服务
先查看jre环境变量:find / -name jre
vim /usr/lib/systemd/system/tomcat.service
[Unit]
Description=tomcat-server
After=network.target
[Service]
Type=forking
# 因为服务启动时,是不会读取全局变量的(/etc/profile),因此全局变量中配置的属性是读取不到的需要注意。
# 所以需要在这里配置Tomcat 启动需要的JDK,指定JDK路径
Environment="JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.312.b07-1.el8_4.x86_64/jre"
# 此文件是在第一步时配置的,如果type是后台运行,建议将此属性加上,指定pid。
PIDFile=/usr/local/tomcat/apache-tomcat-8.5.92/bin/tomcat.pid
# 需要注意的就是下面这一行,如果路径错了,那就无法自动启动,下面会有图文解释
# 整个 ExecStart 脚本,相当于是让系统开机时自动帮你在终端输入等号后面的命令
ExecStart=/usr/local/tomcat/apache-tomcat-8.5.92/bin/startup.sh
ExecReload=/usr/local/tomcat/apache-tomcat-8.5.92/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
保存推出,直接运行systemct start tomcat.service即可
3.3修改nginx配置文件
增加反向代理配置:
location / {
root html;
proxy_pass http://192.168.200.142:8080;
index index.html index.htm;
}
然后systemct restart nginx
访问http://192.168.200.141
附:
nginx的守护进程(手写)
[root@k8s-node1 shell]# cat nginx_daemon.sh
#!/bin/bash
#
this_pid=$$
while true
do
ps -ef|grep nginx|grep -v grep|grep -v $this_pid
if [ $? -eq 0 ];then
echo "nginx is running well!"
else
echo "nginx is down,starting it..."
systemctl restrat nginx.service
fi
done