Nginx常見模塊的使用
安裝好nginx後或者搭建完lnmp環境後爲了實現一些功能,則需要使用各種各樣的模塊
rewrite的使用
[root@wang nginx]# vim nginx.conf
添加rewrite的選項
意思是訪問/*.bmp會轉爲訪問/index.php
[root@wang nginx]# vim nginx.conf
[root@wang nginx]# nginx -t
[root@wang nginx]# service nginxd restart
訪問
和
一樣,實現了重定定向
如果將配置文件更改爲
則實現了允許192.168.2.100訪問,不允許192.168.2.0網段其他的訪問
access模塊:來源控制
vim /etc/nginx/nginx.conf 在sever站點裏面添加內容
service nginx restart
允許192.168.88.10訪問,阻止網段其他的地址訪問
打開一臺PC機,地址設爲192.168.88.10,進行訪問
再把地址修改成192.168.88.0網段內的其他地址,進行訪問
實現了來源控制!
auth模塊:身份驗證
因爲httpd-tools帶有htpasswd 工具,所以先安裝httpd-tools
1.vim /etc/nginx/nginx.conf 在sever站點裏面添加內容
htpasswd -c /etc/nginx/.htpasswd zhangsan創建帳號,並輸入密碼
service nginxd restart重啓
打開瀏覽器進行測試
實現了身份驗證!
ssl模塊:安全加密
cd /etc/pki/tls/
vim openssl.cof
更改文件,爲其他機構也能頒發證書
更改默認值
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端口,加密訪問
打開瀏覽器訪問https://192.168.88.100
顯示證書鏈
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
查看出現下面幾個文件
打開瀏覽器訪問https://192.168.88.100,再點擊查看證書
證書路徑,就能看到證書鏈
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 可以查看打完補丁都多出了
--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.50和2.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臺服務器壞掉,能夠立即發現,不發送代理
先訪問本機查看是否能夠成功代理
實現了輪詢的功能
應該探測的是
文件,沒有這個文件所以顯示down,下面建g個.health文件測試下
第一個服務區建了個.health文件,顯示ok