运维项目实训 - Nginx Web服务应用+Nginx实现负载均衡

nginx

源码安装程序过程:
·1.下载并且解压源码包
2.进入解压好的源码包,并且执行configure文件
安装nginx包

[root@server1: ~]# tar zxf nginx-1.12.0.tar.gz 
[root@server1: ~]# cd nginx-1.12.0 
[root@server1: nginx-1.12.0]# cd /mnt/nginx-1.12.0 
[root@server1: nginx-1.12.0]# vim src/core/nginx.h
[root@server1: nginx-1.12.0]# vim /cc/gcc 
171 # debug
172 #CFLAGS="$CFLAGS -g"
[root@server1: nginx-1.12.0 ]# cd /usr/local/ 
[root@server1: local]# useradd -M -d /usr/local/lnmp/nginx -s /sbin/nologin -u 800 nginx 
[root@server1: local]# yum install pcre-devel openssl-devel -y ##检查并且安装Nginx的两个基础依赖包 
[root@server1: local]# ./configure –prefix=/usr/local/lnmp/nginx –user=nginx –group=nginx –with-
threads –with-file-aio –with-http_ssl_module –withhttp_stub_status_module 
[root@server1: local]# make && make install ./nginx 

测试:

[root@server1: local]# curl -I 172.25.51.1
HTTP/1.1 200 OK
Server: nginx/1.12.0
Date: Mon, 25 Jun 2018 22:12:03 GMT
Content-Type: text/html; charset=UTF-8
Content-Length: 17
Connection: keep=alive
Last-Mondified: Mon, 25 Jun 2018 22:09:21 GMT
ETag:  "5fe10-12-26f6lea112a71"
Accept-Ranges:bytes

打开浏览器输入安装Nginx服务器的ip地址,出现welcome to nginx也表示配置成功


nginx实现负载均衡

实验环境:
server1:172.25.51.1 :nginx服务器
server2:172.25.51.2 :Apache服务器
server3:172.25.51.3 :Apache服务器
一、 server1: 修改配置文件

[root@server1 local]# vim /usr/local/nginx/conf/nginx.conf
17 http {
18     include          mime.types;
19     default_type  application/octet-stream;
20 upstream  westos{
21     server 172.25.51.3:80;   
22     server 172.25.51.2:80;
23 }
38     server {
39         listen       80;
40         server_name  www.westos.org;
41
42         #charset koi8-r;
43         
44         #access_log  logs/host.access.log  main;
45
46         location / {
47             proxy_pass http://westos;
48         }

二、重新加载nginx服务

[root@server1 ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server1 ~]# nginx -s reload

三、apache服务器上启动http服务
1.下载http服务并写入内容

server3

[root@server3: ~]# yum install httpd -y
[root@server3: ~]# /etc/init.d/httpd start
[root@server3: ~]# cat /var/www/html/index.html 
<h1>server3</h1>
[root@server3:~]# /etc/init.d/httpd    restart

server2

[root@server2: ~]# yum install httpd -y
[root@server2: ~]# /etc/init.d/httpd start
[root@server2: ~]# cat /var/www/html/index.html 
<h1>server2</h1>
[root@server2: ~]# /etc/init.d/httpd    restart

2.apache服务器的网关指向nginx服务

server2

[root@server2: network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE="eth4"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR=172.25.51.2
PREFIX=24
GATEWAY=172.25.51.1
DNS1=114.114.114.114

server3

[root@server3: network-scripts]# cat /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE="eth4"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR=172.25.51.3
PREFIX=24
GATEWAY=172.25.51.1
DNS1=114.114.114.114

测试:物理机

[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server3</h1>
[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server2</h1>
[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server3</h1>
[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server2</h1>
[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server3</h1>
[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server2</h1>
[kiosk@oundation51 ~]$ curl 172.25.254.151
<h1>server3</h1>

猜你喜欢

转载自blog.csdn.net/lx543733371/article/details/80807498