1、简介
nginx是一个web服务器,反向代理服务器、开源并且高性能,社区里面有很多工程师在维护这个项目。可以在官网(Index of /download/)下载组件。而且nginx可以用来做流量转发,也是是负载均衡功能,分散单台服务器的处理负担,在现在的互联网行业中使用很常见。
转发流量,分散负担
2、主机网络配置
网络规划
主机类型 | ip | 业务端口 |
---|---|---|
master | 192.168.0.100 | 80 |
slave | 192.168.0.101 | 80 |
master | 192.168.0.100 | 88 |
将两台主机模拟成服务器,负载均衡放在master,监听88端口,分发slave和master的流量。
网卡设备能通百度就行。
两个主机都操作
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet PROXY_METHOD=none BROWSER_ONLY=no BOOTPROTO=static DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33 UUID=0e7eb8d7-7ec0-4fb3-8895-1fd19f2f5af3 DEVICE=ens33 ONBOOT=yes IPADDR=192.168.0.100 NETMASK=255.255.255.0 GATEWAY=192.168.0.1 DNS1=114.114.114.114 DNS2=8.8.8.8
3、安装apache服务
两台主机都操作
[root@localhost ~]# yum install httpd -y
启动并设置开机启动 [root@localhost ~]# systemctl restart httpd && systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
去浏览器看看网页
关闭防火墙并开机关闭
[root@localhost ~]# systemctl stop firewalld [root@localhost ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
出现这个页面说明apache安装成功
4、安装编译组件
nginx是C语言开发的,需要c语言来解析。
master节点上运行
[root@localhost ~]# yum install -y gcc pcre pcre-devel zlib zlib-devel
5、安装nginx
去官网上(Index of /download/)下载一个nginx-1.21.6.tar.gz组件,找不到就直接百度,应该可以找到。
解压缩:
[root@localhost ~]# tar -zxvf nginx-1.21.6.tar.gz
[root@localhost ~]# cd nginx-1.21.6
指定安装路径
[root@localhost nginx-1.21.6]# ./configure --prefix=/usr/local/nginx
编译并且安装
[root@localhost nginx-1.21.6]# make && make install
没报错,没红色的字说明成功了。
[root@localhost nginx-1.21.6]# cd /usr/local/nginx/sbin
80端口被占用,应该改改端口
[root@localhost sbin]# cd .. [root@localhost nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@localhost nginx]# cd conf [root@localhost conf]# ls fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default fastcgi_params koi-win nginx.conf scgi_params.default win-utf [root@localhost conf]# vi nginx.conf
#把80改成88 server { listen 88; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main;
[root@localhost conf]# cd .. [root@localhost nginx]# cd sbin [root@localhost sbin]# ls nginx [root@localhost sbin]# ./nginx [root@localhost sbin]# 运行成功了
这样启动总是要找地方,不太方便,准备一个脚本,可以用系统命令去调用
[root@localhost sbin]# vi /usr/lib/systemd/system/nginx.service
[Unit] Description=nginx - web server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/usr/local/nginx/sbin/nginx -s reload #重启nginx ExecStop=/usr/local/nginx/sbin/nginx -s stop #停止 ExecQuit=/usr/local/nginx/sbin/nginx -s quit #优雅停止nginx PrivateTmp=true [Install] WantedBy=multi-user.target
[root@localhost sbin]# systemctl daemon-reload [root@localhost sbin]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@localhost sbin]# systemctl restart nginx
报了一个错误,我们看看日志
[root@localhost ~]# cd /usr/local/nginx [root@localhost nginx]# ls client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp [root@localhost nginx]# cd logs [root@localhost logs]# ls access.log error.log
2022/12/14 18:23:38 [emerg] 4453#0: bind() to 0.0.0.0:88 failed (98: Address already in use)
需要把nginx服务杀掉,再重启
[root@localhost logs]# ps -ef |grep nginx root 4403 1 0 18:20 ? 00:00:00 nginx: master process ./nginx nobody 4404 4403 0 18:20 ? 00:00:00 nginx: worker process root 4463 1567 0 18:25 pts/0 00:00:00 grep --color=auto nginx
安装killall命令
[root@localhost logs]# yum install -y psmisc
[root@localhost logs]# killall -9 nginx
[root@localhost logs]# systemctl restart nginx 重启就好了
6、配置负载均衡
配置的参数有多种多样,有连接数,权重,等等,在本次实操中,我们配置监听的端口,及ip地址,来做负载均衡。
大概配这么多就行了
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
upstream loabd{ server 192.168.0.100:80; server 192.168.0.101:80; } server { listen 88; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { # root html; # index index.html index.htm; proxy_pass http://loadb; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; }
但是现在还观察不到负载均衡现象,需要在两台主机中配置apache的网页。
[root@localhost ~]# cd /var/www/ [root@localhost www]# ls cgi-bin html [root@localhost www]# cd html/ [root@localhost html]# ls [root@localhost html]# vi index.html hello,this is master,my ipaddr is 192.168.0.100
[root@localhost ~]# vi /var/www/html/index.html hello,everyone,this is slave ,my ipaddr 192.168.0.101
两台主机都重启httpd服务
9、观察负载均衡现象
在浏览器中输入master的地址加端口,及192.168.0.100:88,刷新观察负载均衡现象。
还没成功,我们看下日志
配置文件有点错误
2022/12/14 18:36:34 [error] 4479#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.10, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.0.100:88", referrer: "http://192.168.0.100:88/" 2022/12/14 18:37:20 [emerg] 4515#0: host not found in upstream "loadb" in /usr/local/nginx/conf/nginx.conf:49
db和bd写反了
重启服务
在两台主机上布置的网页,成功通过nginx进行了流量转发,本次实验到此为止。