环境centos-7.6(1台proxy,2台web服务器)
Nginx:主流的web服务器,高性能的反向代理服务器
反向代理:可以屏蔽内部服务器的真实IP地址
部署网站服务器(2台配置一样)
安装软件包:yum -y install httpd
测试页面(web1、web2):echo "<marquee><font color=red><hl>web1<marquee>" > /var/www/html/index.html
echo "<marquee><font color=red><hl>web2<marquee>" > /var/www/html/index.html
启动服务:systemctl start httpd
如果启动不成功,请查看进程systemctl status httpd,有httpd进程就pkill httpd,然后再查看端口lsof -i:80,把其他服务关掉,再启动httpd服务
部署调度器(proxy代理服务器)
安装Nginx
源码安装Nginx
安装依赖包: yum -y install gcc pcre-devel openssl-devel
创建普通用户: useradd –s /sbin/nologin nginx
解压Nginx源码包: tar -xf nginx-1.10.3.tar.gz
进入源码包路径:cd nginx-1.10.3
编译安装Nginx
检测环境:./configure --user=nginx --group=nginx --with-http_ssl_module
编译源码:make
安装:make install
启动服务:/usr/local/nginx/sbin/nginx
配置Nginx服务器,添加服务池,实现反向代理功能
vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
.. ..
upstream webserver { //定义集群
server 192.168.234.99:80;
server 192.168.234.100:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webserver; //调用集群
}
配置upstream服务器集群属性
weight可以设置后台服务器的权重值,默认为1
max_fails可以设置后台服务器的最大连接失败次数
fail_timeout 可以设置后台服务器的失败超时时间,单位为秒
down标记服务器已关机,不参与集群调度
upstream webserver {
server 192.168.234.99:80 weight=2 max_fails=1 fail_timeout=30;
server 192.168.234.100:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.0.63 down;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webserver;
root html;
index index.html index.htm;
}
upstream调度算法
Nginx目前支持的调度算法
—轮询(默认):逐一循环调度,可通过weight执行轮询几率
—ip_hash:根据客户端IP分配固定的后端服务器
upstream webserver {
ip_hash; //设置调度算法
server 192.168.0.12:80 weight=2 max_fails=1 fail_timeout=30;
server 192.168.0.120:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.0.63 down;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://webserver;
root html;
index index.html index.htm;
}
HTTP状态码
返回码 状态描述
200 一切正常
301 永久重定向
302 临时重定向
401 用户名或密码错误
403 禁止访问(客户端的IP地址被拒绝)
404 文件不存在
414 请求的Url头部过长
500 服务器内部错误
502 Bad Gateway