Nginx常見模塊的使用

Nginx常見模塊的使用

安裝好nginx後或者搭建完lnmp環境後爲了實現一些功能,則需要使用各種各樣的模塊

 rewrite的使用

[root@wang nginx]# vim nginx.conf

DF17E2A9EAAA4BB89C3C48740704A6B6 

 

添加rewrite的選項

意思是訪問/*.bmp會轉爲訪問/index.php

[root@wang nginx]# vim nginx.conf

[root@wang nginx]# nginx -t

[root@wang nginx]# service nginxd restart

訪問9EDF4F0EAB8448AF9428260A022627EF

1F57F9C7B2834A9EAE3A28CF3B0054BE 

5FB8E7ED8558438B813552435EDB1F76

一樣,實現了重定定向

如果將配置文件更改爲

AB151664FC0D41F3ACC409189364807A 

則實現了允許192.168.2.100訪問,不允許192.168.2.0網段其他的訪問

access模塊:來源控制

  1. vim /etc/nginx/nginx.conf 在sever站點裏面添加內容

76CDFDE1B84941E6AACC701ADFC6C99E 

service nginx restart

允許192.168.88.10訪問,阻止網段其他的地址訪問

打開一臺PC機,地址設爲192.168.88.10,進行訪問

81E79F5D598C4CA3AD8AC3AA6AE622EB 

 

再把地址修改成192.168.88.0網段內的其他地址,進行訪問

4BF5A4785F464ED6B026BE440C895E70 

實現了來源控制!

auth模塊:身份驗證

因爲httpd-tools帶有htpasswd 工具,所以先安裝httpd-tools

1.vim /etc/nginx/nginx.conf 在sever站點裏面添加內容

B2EEC39878FC4AB088A35BA04E00E987 

 

  1. htpasswd -c /etc/nginx/.htpasswd zhangsan創建帳號,並輸入密碼

  2. service nginxd restart重啓

打開瀏覽器進行測試

60D1C27DBF9A469FB11D818919F2DFB9 

 

實現了身份驗證!

ssl模塊:安全加密

  1. cd /etc/pki/tls/

  2. vim openssl.cof 

更改文件,爲其他機構也能頒發證書

0C190D2F87D14653958A8E8F0D1D9DDA 

更改默認值

9CC6021BDA9F4A6AA31E1FD6F27EC313 

3.echo "01">serial  記錄序號

4.openssl genrsa 1024 >../CA/private/cakey.pem 產生私鑰

5.cd /etc/pki/CA/

6.chmod 600 private/cakey.pem改變權限

7.cd /etc/nginx/certs/

8.openssl ca -in nginx.req -out nginx.cert爲自身產生證書

9.mkdir /etc/nginx/certs 創建目錄存放證書

10.cd /etc/nginx/certs/

11.openssl genrsa 1024 >nginx.key 產生私鑰

12.chmod 600 nginx.key

13.openssl req -new -key nginx.key -out nginx.req

14.openssl ca -in nginx.req -out nginx.cert

15.vim /etc/nginx/nginx.conf  打開443端口,加密訪問

05594A00B7304D2FA11454CBD0335D63 

打開瀏覽器訪問https://192.168.88.100

3DDAF14A995F491E840ED19058BF3935 

顯示證書鏈

16.cd /etc/nginx/certs

17.cp /etc/pki/CA/cacert.pem ./

18.cp nginx.cert ./nginx.cert.bak

19.cat nginx.cert.bak cacert.pem >nginx.cert

查看出現下面幾個文件

3EF90ABB5B414A4E84C358364F7EF8FE 

打開瀏覽器訪問https://192.168.88.100,再點擊查看證書

AB68EEC40FED40AA97EBB41E583BFD07 7BA16610EF264413B9BCA5D0B8F8C518

證書路徑,就能看到證書鏈

 

 

 

 

 

upstream  health-check的使用

安裝health-check模塊需要安裝低版本的nginx,高版本的nginx+需要付費才能使用,這裏我使用的是1.0.11版本

[root@wang ~]# tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src/  拆解源碼包

[root@wang ~]# unzip healthcheck_nginx_upstreams-master   拆解healthcheck模塊補丁

[root@wang healthcheck_nginx_upstreams-master]# cd /usr/local/src/nginx-1.0.11/

[root@wang nginx-1.0.11]# patch -p1 </root/healthcheck_nginx_upstreams-master/nginx.patch  將health補丁打到nginx

[root@wang nginx-1.0.11]# ./configure --help |grep add  可以查看打完補丁都多出了

D25FE00F0CD6493EBABC456CFB3F3C2F 

--add-moudle=PATH

然後在安裝nginx之前需要建組和用戶

[root@wang nginx-1.0.11]# groupadd -r nginx

[root@wang nginx-1.0.11]# useradd -r -g nginx nginx

[root@wang nginx-1.0.11]#./configure \

--conf-path=/etc/nginx/nginx.conf \

--error-log-path=/var/log/nginx/error.log \

--http-log-path=/var/log/nginx/access.log \

--pid-path=/var/run/nginx/nginx.pid \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=nginx \

--with-http_ssl_module \

--with-http_flv_module \

--with-http_stub_status_module \

--with-http_gzip_static_module \

--http-client-body-temp-path=/var/tmp/nginx/client/ \

--http-proxy-temp-path=/var/tmp/nginx/proxy/ \

--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \

--with-pcre \

--add-module=/usr/local/src/healthcheck_nginx_upstreams-master

進行配置

[root@wang nginx-1.0.11]# make && make install 配置安裝

(需要安裝pcre-devel包,openssl-devel包)

[root@wang nginx-1.6.0]# vim /etc/profile  加入路徑

[root@wang nginx-1.6.0]# . /etc/profile

[root@wang nginx-1.6.0]# nginx -t   測試缺少文件

[root@wang nginx-1.6.0]# mkdir -pv /var/tmp/nginx/client/

[root@wang nginx-1.6.0]# nginx -t

[root@wang nginx-1.6.0]# nginx

[root@wang nginx-1.6.0]# netstat -tupln |grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      27414/nginx  

控制腳本寫入

[root@wang mysql]# cd /etc/init.d/

[root@wang init.d]# vim nginxd

[root@wang init.d]# chmod a+x nginxd  加入可執行權限

prog=/usr/local/nginx/sbin/nginx

lockfile=/var/lock/nginx.lock

# description: the nginx web server

# chkconfig: 2345 88 44

. /etc/init.d/functions

start(){

   if [ -e $lockfile ];then

      echo "the nginx web server is started"

      else

      echo -n "the nginx web server is starting....."

      sleep 1

      $prog && echo "ok" && touch $lockfile || echo "failer"

      fi

}

stop(){

   if [ ! -e $lockfile ];then

       echo "the nginx web server is stoped"

       else

       echo -n "the nginx web server is stoping....."

       killproc nginx && echo "ok" && rm -rf $lockfile || echo "failer"

   fi

 

 

}

 

case "$1" in

start)

     start

     ;;

stop)

     stop

     ;;

restart)

     stop

     start

     ;;

*)

echo "Usage: start|stop|restart"

     ;;

 esac

編輯腳本如此,保存退出

進行測試

[root@wang init.d]# pkill -9 nginx   殺死進程

[root@wang init.d]# chkconfig --add nginxd  加入啓動

[root@wang init.d]# chkconfig --list |grep nginx  差看

[root@wang init.d]# service nginxd start

[root@wang ~]# cd /etc/nginx/

[root@wang nginx]# vim nginx.conf 編輯配置文件

 upstream backend {

 35             server 192.168.2.50;   

 36             server 192.168.2.101;       //對2.502.101進行服務

 37             healthcheck_enabled;

 38             healthcheck_delay 1000;

 39             healthcheck_timeout 1000;

 40             healthcheck_failcount 1;

 41            # healthcheck_expected 'I_AM_ALIVE';

 42             healthcheck_send "GET /.health HTTP/1.0" ;     編輯爲如此

                location / {

 54             root   html;

 55             index  index.html index.htm;

 56         proxy_pass http://backend;         //根爲html,對backend的池做代理

 57         }

[root@wang nginx]# nginx -t

[root@wang nginx]# pkill -9 nginx

[root@wang nginx]# nginx

[root@wang nginx]service iptables stop

[root@wang nginx]setenforce 0

現在實現了輪詢的功能,即請求分別發送至2.50 2.100,還有健康探測功能,即如果1臺服務器壞掉,能夠立即發現,不發送代理

先訪問本機查看是否能夠成功代理

0CF02D899F3F4A968E549B28D44B7907 

81E0C01754FE430C8BD20D9D3383CC69 

實現了輪詢的功能

9A92CF2F82634A07BC0589007BC5ED44 

應該探測的是

6A6E25DA95C446C4A6A9DC3E0942FB76文件,沒有這個文件所以顯示down,下面建g.health文件測試下

03F59F495B034284B946F9277509C3C7 

第一個服務區建了個.health文件,顯示ok


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章