linux基础(5)- nginx服务、nfs服务

一、nginx服务

源码安装:

yum install gcc-* glibc-* openssl openssl-devel pcre pcre-devel zlib zlib-devel -y
ls
tar xvf nginx-1.10.3.tar.gz 
cd nginx-1.10.3
ls
./configure
./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --without-http_rewrite_module #--without-http_rewrite_module代表不使用重写功能,即不会引用pcre库,如果安装过程中出现pcre相关问题,可以指定该参数
make
make install

说明:

pcre: 用来作地址重写的功能。

zlib:nginx 的gzip模块,传输数据打包,省流量(但消耗资源)。

openssl:提供ssl加密协议。

安装:http://www.cnblogs.com/crazyacking/p/5138146.html

相关参数:http://blog.csdn.net/xifeijian/article/details/20956605

location:http://www.tuicool.com/articles/Jr63qy

正向代理,反向代理,透明代理:http://blog.csdn.net/u012572955/article/details/49357219

代理:A访问B,A把自己的请求让C代理,C去访问B

正向代理:A明确地知道自己的代理C的存在,A明确地指定自己的代理地址是C,让C去访问B,拿到结果再给A,通常用来FQ

反向代理:用户请求发给nginx后,由nginx负载给代理的机器,拿到结果,再由nginx返回给客户端(如此,nginx便把一堆自己代理的机器隐藏到后端,外界访问的就是nginx)

透明代理:用户不知道代理的存在,用户->交换机->路由器->代理服务器,代理服务器可以控制用户的上网行为,比如限制用户可以访问和不可以访问的网站,多用于用户行为管理

LB:http://nginx.org/en/docs/http/load_balancing.html

(1)二进制安装nginx包

切换到yum源下,cd /etc/yum.repos.d 

先使用yum 来安装 nginx 服务,基于epel-release平台的nginx需要epel的支持,所以要先安装epel

epel安装完成后,在yum 源中,可以看到epel.repo文件

使用yum 安装 nginx 服务

(2)作为web服务修改配置文件

对Nginx的配置文件进行编辑,让它为我们提供服务,它的配置文件默认是/ect/nginx/nginx.conf,

先忽略其他不计,这里只看location / 我们将这里写入想要展示的页面路径和文件:

location / 是局部页面路径配置,如果location / 不写入局部页面路径,此时会向上找全局页面路径配置,找到root  /usr/nginx/html;这一行。

 

在这里我们更改一下局部页面路径root配置和首页文件

 这样我们回到/etc/nginx目录创建/data/www/html目录,并创建文件index.html写入内容welcome to hangzhou

(3)让配置生效,验证配置

1.systemctl disable firewalld  #关闭Firewalld自启动

2.systemctl stop firewalld  #关闭firewalld

3.systemctl start nginx  #启动nginx服务

5.systemctl status nginx #查看nginx启动状态

使用浏览器访问IP地址,成功。

 整个过程总结:输入浏览器完整网页地址http://192.168.255.129:80/index.html

1.http://---->服务端server端基于http协议给客户端client发数据的

2.192.168.255.129:80---->定义全世界范围内唯一一款软件是谁,此次操作中定位到局部页面路径location下的root  /data/www/html

3./index.html,'/'表示根目录/data/www/html。‘/index.html’此时定位/data/www/html/index.html。页面显示就是最终找到的文件内容。

http://192.168.255.129:80/index.html---->nginx---->nginx.conf---->/data/www/html/index.html---->http

二、nfs服务

(1)二进制安装nfs

nfs为网络文件服务的意思。

(2)作为共享存储挂载在三台web的网站根目录下

创建克隆,web1,web2,web3。

分别开启主虚拟机,web1,web2,web3,获取ip地址分别是192.168.255.129,192.168.255.130,192.168.255.132,192.168.255.133。

服务端安装nfs,已安装过。

[root@nfs-server ~]# mkdir /share  #创建挂载目录

[root@nfs-server ~]# cat /etc/exports  #配置NFS SERVER
/share 192.168.255.0/24(rw,sync,fsid=0)

先为rpcbind和nfs做开机启动

启动(必须先启动rpcbind服务)

确认nfs服务器启动成功

[root@nfs-server ~]# exportfs
/share          192.168.255.0/24
# 默认使用showmount -e 查看自己共享的服务,需要DNS能解析,不然容易报错。 

# 此处添加HOSTS本地解析。
[root@nfs-server ~]# echo "192.168.255.129     nfs-server" >> /etc/hosts
[root@nfs-server ~]# showmount -e 
Export list for nfs-server:
/share 192.168.255.0/24
 
# 客户端挂载:
[root@web01 ~]# mount -t nfs 192.168.255.130:/share /data/www/html/
[root@web02 ~]# mount -t nfs 192.168.255.131:/share /data/www/html/
[root@web03 ~]# mount -t nfs 192.168.255.132:/share /data/www/html/
 
 #显示已经与客户端连接上的目录信息

[root@nfs-server ~]# showmount -a  #显示已经与客户端连接上的目录信息

All mount points on nfs-server:
192.168.255.130:/share
192.168.255.131:/share
192.168.255.132:/share

  

 

  web1 终端中:vim /etc/nginx/nginx.conf   ,若没有权限,需修改权限。

 

/data/www/html目录,并创建文件index.html写入内容web1保存,systemctl restart nginx。参考上一题截图。

web2终端中重复,写入内容“web2”保存;systemctl restart nginx。

web3终端中重复,写入内容“web3”保存;systemctl restart nginx。

输入IP地址,发现是一个安全机制搞的鬼。web终端中输入setenforce 0;再次输入ip地址可以正常打开各个网站。

 (3)实现,在任意一台web上修改的结果,其余两台都可以看到

# others用户添加w权限
[root@nfs-server ~]# chmod -R o+w /share/
[root@nfs-server ~]# ll -d /share/
drwxr-xrwx 2 root root 23 Mar 20 16:38 /share/
 
# 测试结果
[root@web01 ~]# echo "welcome xuyaping" > /data/www/html/test.html
[root@web02 ~]# cat /data/www/html/test.html
welcome xuyaping
[root@web03 ~]# cat /data/www/html/test.html                      
welcome xuyaping

  

三、nginx反向代理三台web

(1)实现基于轮询的方式调度三台web,并验证结果

nfs服务器,web1,web2,web3,获取ip地址分别是192.168.255.129,192.168.255.130,192.168.255.132,192.168.255.133。

 均安装nginx,输入命令“yum install nginx -y”

 

  

 

nfs服务器客户端中:

systemctl start nginx 启动Nginx

chmod -R o+w /etc/ginx 设置权限

vim /etc/nginx/nginx.conf 修改代理主机的配置文件

添加三台web ip地址

添加服务器配置

 浏览网址http://192.168.255.129/ ,第一次连接到的是web1,刷新连接web2,web3...验证了轮询。

(2)实现基于权重的方式调度三台web,并验证结果

vim /etc/nginx/nginx.conf 修改代理主机的配置文件

浏览网址http://192.168.255.129/ ,连接到的是web1,刷新连接到的还是web1,验证了权重调度。

(3)实现基于hash的方式调用三台web,并验证结果

 浏览网址http://192.168.255.129/ ,连接到的是web1,刷新连接到的还是web1,验证了hash方式调用。

 四、nginx反向代理+三台web+nfs共享存储实现集群配置

  # 在各个web服务器上挂载nfs

[root@web01 ~]# mount -t nfs 192.168.255.129:/share /data/www/html/
[root@web02 ~]# mount -t nfs 192.168.255.129:/share /data/www/html/
[root@web03 ~]# mount -t nfs 192.168.255.129:/share /data/www/html/
[root@web01 ~]# echo "nfs ok" > /data/www/html/index.html

浏览网址http://192.168.255.129/,页面显示:nfs ok 

 

原文地址:https://www.cnblogs.com/xuyaping/p/6591282.html