天天看點

linux---nginx服務nfs服務nginx反向代理三台web

一:nginx服務

1、二進制安裝nginx包

[root@bogon ~]# systemctl disable firewalld        #關閉Firewalls自啟動
  Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.

[root@bogon ~]# systemctl stop firewalld            #關閉firework

[root@bogon ~]# yum install nginx -y                 #安裝nginx服務        

2、作為web服務修改配置檔案

[root@bogon ~]# egrep -v "^$|^#|#"  /etc/nginx/nginx.conf

user nginx;

worker_processes auto;

error_log /var/log/nginx/error.log;

pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {

    worker_connections 1024;

}

http {

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

                      '$status $body_bytes_sent "$http_referer" '

                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;

    tcp_nopush          on;

    tcp_nodelay         on;

    keepalive_timeout   65;

    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;

    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {

        listen       80 default_server;

        listen       [::]:80 default_server;

        server_name  _;

        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {

                root /data/www/html;

                index index.html;

        }

        error_page 404 /404.html;

            location = /40x.html {

        error_page 500 502 503 504 /50x.html;

            location = /50x.html {

    }

[root@bogon ~]# mkdir -p /data/www/html

[root@bogon ~]# echo "welcome oldgirl" > /data/www/html/index.html

3、讓配置生效,驗證配置

[root@bogon ~]# /usr/sbin/nginx -t   #檢查配置是否正确

  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

  nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@bogon ~]# systemctl start nginx  #啟動nginx服務

[root@bogon ~]# systemctl status nginx #檢視nginx啟動狀态

● nginx.service - The nginx HTTP and reverse proxy server

   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)

   Active: active (running) since Mon 2017-03-20 15:23:29 CST; 44s ago

  Process: 3072 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)

  Process: 3069 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)

  Process: 3068 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

 Main PID: 3075 (nginx)

   CGroup: /system.slice/nginx.service

           ├─3075 nginx: master process /usr/sbin/nginx

           └─3076 nginx: worker process

Mar 20 15:23:29 bogon systemd[1]: Starting The nginx HTTP and reverse proxy server...

Mar 20 15:23:29 bogon nginx[3069]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok

Mar 20 15:23:29 bogon nginx[3069]: nginx: configuration file /etc/nginx/nginx.conf test is successful

Mar 20 15:23:29 bogon systemd[1]: Failed to read PID from file /run/nginx.pid: Invalid argument

Mar 20 15:23:29 bogon systemd[1]: Started The nginx HTTP and reverse proxy server.

二:nfs服務

1、二進制安裝nfs

1 [root@bogon ~]# yum install rpcbind nfs-utils -y  #NFS服務端和用戶端都裝。

2、作為共享存儲挂載在三台web的網站根目錄下

[root@nfs-server ~]# mkdir /share  #建立挂載目錄

[root@nfs-server ~]# cat /etc/exports  #配置NFS SERVER

/share 192.168.152.0/24(rw,sync,fsid=0)

# 啟動開機自啟:

[root@nfs-server ~]# systemctl enable nfs-server.service

Created symlink from /etc/systemd/system/multi-user.target.wants/nfs-server.service to /usr/lib/systemd/system/nfs-server.service.

[root@nfs-server ~]# systemctl enable rpcbind.service

# 啟動服務(必須先啟動rpcbind服務):

[root@nfs-server ~]# systemctl start rpcbind.service

[root@nfs-server ~]# systemctl start nfs-server.service

# 确定NFS伺服器啟動成功:

[root@nfs-server ~]# rpcinfo

   program version netid     address                service    owner

    100000    4    tcp6      ::.0.111               portmapper superuser

    100000    3    tcp6      ::.0.111               portmapper superuser

    100000    4    udp6      ::.0.111               portmapper superuser

    100000    3    udp6      ::.0.111               portmapper superuser

    100000    4    tcp       0.0.0.0.0.111          portmapper superuser

    100000    3    tcp       0.0.0.0.0.111          portmapper superuser

    100000    2    tcp       0.0.0.0.0.111          portmapper superuser

    100000    4    udp       0.0.0.0.0.111          portmapper superuser

    100000    3    udp       0.0.0.0.0.111          portmapper superuser

    100000    2    udp       0.0.0.0.0.111          portmapper superuser

    100000    4    local     /var/run/rpcbind.sock  portmapper superuser

    100000    3    local     /var/run/rpcbind.sock  portmapper superuser

    100005    1    udp       0.0.0.0.78.80          mountd     superuser

    100005    1    tcp       0.0.0.0.78.80          mountd     superuser

    100005    1    udp6      ::.78.80               mountd     superuser

    100005    1    tcp6      ::.78.80               mountd     superuser

    100005    2    udp       0.0.0.0.78.80          mountd     superuser

    100005    2    tcp       0.0.0.0.78.80          mountd     superuser

    100024    1    udp       0.0.0.0.129.33         status     29

    100005    2    udp6      ::.78.80               mountd     superuser

    100024    1    tcp       0.0.0.0.171.119        status     29

    100024    1    udp6      ::.206.85              status     29

    100005    2    tcp6      ::.78.80               mountd     superuser

    100005    3    udp       0.0.0.0.78.80          mountd     superuser

    100024    1    tcp6      ::.207.20              status     29

    100005    3    tcp       0.0.0.0.78.80          mountd     superuser

    100005    3    udp6      ::.78.80               mountd     superuser

    100005    3    tcp6      ::.78.80               mountd     superuser

    100003    3    tcp       0.0.0.0.8.1            nfs        superuser

    100003    4    tcp       0.0.0.0.8.1            nfs        superuser

    100227    3    tcp       0.0.0.0.8.1            nfs_acl    superuser

    100003    3    udp       0.0.0.0.8.1            nfs        superuser

    100003    4    udp       0.0.0.0.8.1            nfs        superuser

    100227    3    udp       0.0.0.0.8.1            nfs_acl    superuser

    100003    3    tcp6      ::.8.1                 nfs        superuser

    100003    4    tcp6      ::.8.1                 nfs        superuser

    100227    3    tcp6      ::.8.1                 nfs_acl    superuser

    100003    3    udp6      ::.8.1                 nfs        superuser

    100003    4    udp6      ::.8.1                 nfs        superuser

    100227    3    udp6      ::.8.1                 nfs_acl    superuser

    100021    1    udp       0.0.0.0.220.240        nlockmgr   superuser

    100021    3    udp       0.0.0.0.220.240        nlockmgr   superuser

    100021    4    udp       0.0.0.0.220.240        nlockmgr   superuser

    100021    1    tcp       0.0.0.0.156.92         nlockmgr   superuser

    100021    3    tcp       0.0.0.0.156.92         nlockmgr   superuser

    100021    4    tcp       0.0.0.0.156.92         nlockmgr   superuser

    100021    1    udp6      ::.211.174             nlockmgr   superuser

    100021    3    udp6      ::.211.174             nlockmgr   superuser

    100021    4    udp6      ::.211.174             nlockmgr   superuser

    100021    1    tcp6      ::.171.11              nlockmgr   superuser

    100021    3    tcp6      ::.171.11              nlockmgr   superuser

    100021    4    tcp6      ::.171.11              nlockmgr   superuser

[root@nfs-server ~]#

[root@nfs-server ~]# exportfs

/share          192.168.152.0/24

# 預設使用showmount -e 檢視自己共享的服務,需要DNS能解析,不然容易報錯。

# 此處添加HOSTS本地解析。

[root@nfs-server ~]# echo "192.168.152.134     nfs-server" >> /etc/hosts

[root@nfs-server ~]# showmount -e 

Export list for nfs-server:

/share 192.168.152.0/24

# 用戶端挂載:

[root@web01 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/

[root@web02 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/

[root@web03 ~]# mount -t nfs 192.168.152.134:/share /data/www/html/

[root@nfs-server ~]# showmount -a  #顯示已經與用戶端連接配接上的目錄資訊

All mount points on nfs-server:

192.168.152.135:/share

192.168.152.136:/share

192.168.152.137:/share

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 luchuangao" > /data/www/html/test.html

[root@web02 ~]# cat /data/www/html/test.html

welcome luchuangao

[root@web03 ~]# cat /data/www/html/test.html                      

三:nginx反向代理三台web

(1)

Nginx反向代理:實作基于輪詢的方式

http {

    upstream  pythonweb{

       server 192.168.2.3:80;

       server 192.168.2.4:80;

       server 192.168.2.5:80;

  server{

   location / {

     proxy_pass http://pythonweb;

   }

  }

[root@bogon ~]# systemctl reload nginx.service 

[root@bogon ~]# systemctl restart nginx.service 

(2)

Nginx反向代理:實作基于權重的方式

       server 192.168.2.3:80  weight=3;

(3)

       ip_hash;

四:nginx反向代理+三台web+nfs共享存儲實作叢集配置

# 在各個web伺服器上挂載nfs

[root@web01 ~]# mount -t nfs 192.168.152.134:/share

/data/www/html/

[root@web02 ~]# mount -t nfs 192.168.152.134:/share

[root@web03 ~]# mount -t nfs 192.168.152.134:/share

[root@web01 ~]# echo "nfs ok" >

/data/www/html/index.html

# 浏覽器通路進行驗證

linux---nginx服務nfs服務nginx反向代理三台web

五:源碼安裝nginx,并按照作業一描述的那樣去測試使用

#編譯安裝nginx

[root@nfs-server ~]# yum install gcc-* glibc-* openssl

openssl-devel pcre pcre-devel zlib zlib-devel -y

[root@nfs-server ~]# wget -q

http://nginx.org/download/nginx-1.10.3.tar.gz

[root@nfs-server ~]# ls nginx-1.10.3.tar.gz

nginx-1.10.3.tar.gz

[root@nfs-server ~]# tar -xf nginx-1.10.3.tar.gz

[root@nfs-server ~]# cd nginx-1.10.3/

[root@nfs-server nginx-1.10.3]# ./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

[root@nfs-server nginx-1.10.3]# make && make

install

# 配置nginx.conf

[root@nfs-server ~]# cat

/usr/local/nginx/nginx.conf|egrep -v "^$|^#|#"

worker_processes  1;

    worker_connections  1024;

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

        listen       80;

        server_name  localhost;

            root   /data/www/html;

            index  index.html index.htm;

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

# 驗證

[root@nfs-server ~]# /usr/local/nginx/nginx -t

nginx: the configuration file

/usr/local/nginx/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/nginx.conf

test is successful

[root@nfs-server ~]# /usr/local/nginx/nginx  #啟動nginx服務

[root@nfs-server ~]# echo 'goodbye oldboy' >

/data/www/html/index.html 

#使用浏覽器進行驗證

linux---nginx服務nfs服務nginx反向代理三台web

繼續閱讀