==================nginx 负载均衡====================
实现nginx负载均衡的效果,并运用nfs服务共享目录,使所有nginx服务拥有共同的http目录
nginx安装:http://www.cnblogs.com/alwaysInMe/p/6924859.html
nfs安装:NFS 是Network File System的缩写,即网络文件系统。一种使用于分散式文件系统的协定。
===> 环境配置及软件安装
注:本次安装用的是centos7系统光盘自带的rpm文件进行安装,已提前将光盘镜像路径加载到了repo文件中。
[root@localhost ~]# iptables -F # 清除防火墙配置 [root@localhost ~]# systemctl stop firewalld # 关闭防火墙 [root@localhost ~]# setenforce 0 # 关闭策略组,临时
[root@localhost ~]# vim /etc/sysconfig/selinux # 文件中关闭策略组 [root@localhost ~]# systemctl status firewalld # 查看防火墙状态
[root@bogon ~]# yum -y install rpcbind nfs-utils # 安装rpcbind、nfs-utils。其中nfs依赖于rpcbind 软件包 rpcbind-0.2.0-32.el7.x86_64 已安装并且是最新版本 # 这里提示已经安装,不需要处理 软件包 1:nfs-utils-1.3.0-0.21.el7.x86_64 已安装并且是最新版本 无须任何处理
====> 文件配置
[root@bogon ~]# mkdir /share # 创建共享目录 [root@bogon ~]# vim /etc/exports # 设定nfs配置文件,如下:
/share *(rw,sync,fsid=0) #<输出目录> [客户端1 选项(访问权限,用户映射,其他)]
====> 启动服务
[root@bogon ~]# systemctl start nfs # 启动服务-这里演示的事二进制的 [root@bogon ~]# systemctl status nfs # 查看文件启动情况 ● nfs-server.service - NFS server and services Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled) Active: active (exited) since Thu 2017-06-01 03:32:51 PDT; 1min 6s ago Process: 11099 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS) Process: 11098 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS) Main PID: 11099 (code=exited, status=0/SUCCESS) CGroup: /system.slice/nfs-server.service Jun 01 03:32:51 bogon systemd[1]: Starting NFS server and services... Jun 01 03:32:51 bogon systemd[1]: Started NFS server and services.
[root@bogon ~]# exportfs # 查看nfs服务所开放的文件夹及开放给谁
/share <world>
====> 测试功能
注:测试需要用另外一台linux系统进行挂载链接,所有测试的机器中需要安装nfs,但不需要启动,安装方法见前面。
[root@bogon ~]# mount 192.168.128.181:/share /opt/ # 将共享的文件挂载在/opt 上,如果没有这个目录,可以先使用mkdir命另创建这个文件夹 [root@bogon ~]# df # 查看是否挂载成功 文件系统 1K-块 已用 可用 已用% 挂载点 /dev/sda3 18555904 3797620 14758284 21% / devtmpfs 486144 0 486144 0% /dev tmpfs 500664 88 500576 1% /dev/shm tmpfs 500664 7224 493440 2% /run tmpfs 500664 0 500664 0% /sys/fs/cgroup /dev/sda1 303788 146768 157020 49% /boot tmpfs 100136 16 100120 1% /run/user/0 /dev/sr0 4227724 4227724 0 100% /media 192.168.128.181:/share 18555904 3797632 14758272 21% /opt
我这里一共用了四台电脑,重复以上操作,分别进行连接
下面进行nginx负载均衡文件的配置
注:我这里是先配置web服务器(工作的),测试没问题后再配置代理服务器(分配任务的)
[root@bogon ~]# vim /usr/local/nginx/conf/nginx.conf # 修改nginx配置文件,由于我用的是源码安装,所以我自定义了路径 /usr/local/nginx
1 #user nobody; 2 worker_processes 1; 3 4 #error_log logs/error.log; 5 #error_log logs/error.log notice; 6 #error_log logs/error.log info; 7 8 #pid logs/nginx.pid; 9 10 11 events { 12 worker_connections 1024; 13 } 14 15 16 http { 17 include mime.types; 18 default_type application/octet-stream; 19 20 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 '$status $body_bytes_sent "$http_referer" ' 22 '"$http_user_agent" "$http_x_forwarded_for"'; 23 # 日志功能 24 access_log logs/access.log main; 25 26 sendfile on; 27 tcp_nopush on; 28 29 #keepalive_timeout 0; 30 keepalive_timeout 65; 31 32 #gzip on; 33 34 server { 35 listen 8084; # 修改软件使用端口为 8084 36 server_name localhost; 37 38 #charset koi8-r; 39 40 #access_log logs/host.access.log main; 41 42 location / { 43 root /opt; # 修改默认的 html 文件路径 44 index index.html index.htm; 45 } 46 47 #error_page 404 /404.html; 48 49 # redirect server error pages to the static page /50x.html 50 # 51 error_page 500 502 503 504 /50x.html; 52 location = /50x.html { 53 root html; 54 } 55 56 # proxy the PHP scripts to Apache listening on 127.0.0.1:80 57 # 58 #location ~ \.php$ { 59 # proxy_pass http://127.0.0.1; 60 #} 61 62 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 63 # 64 #location ~ \.php$ { 65 # root html; 66 # fastcgi_pass 127.0.0.1:9000; 67 # fastcgi_index index.php; 68 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; 69 # include fastcgi_params; 70 #} 71 72 # deny access to .htaccess files, if Apache's document root 73 # concurs with nginx's one 74 # 75 #location ~ /\.ht { 76 # deny all; 77 #} 78 } 79 80 81 # another virtual host using mix of IP-, name-, and port-based configuration 82 # 83 #server { 84 # listen 8000; 85 # listen somename:8080; 86 # server_name somename alias another.alias; 87 88 # location / { 89 # root html; 90 # index index.html index.htm; 91 # } 92 #} 93 94 95 # HTTPS server 96 # 97 #server { 98 # listen 443 ssl; 99 # server_name localhost; 100 101 # ssl_certificate cert.pem; 102 # ssl_certificate_key cert.key; 103 104 # ssl_session_cache shared:SSL:1m; 105 # ssl_session_timeout 5m; 106 107 # ssl_ciphers HIGH:!aNULL:!MD5; 108 # ssl_prefer_server_ciphers on; 109 110 # location / { 111 # root html; 112 # index index.html index.htm; 113 # } 114 #} 115 116 }