關於nginx,你不可不知的幾大特色功能
nginx,輕量級的http服務與反向代理服務器軟件,由於其併發能力較強,並且體積很小,所以被稱爲輕量級http服務軟件。
nginx的特色功能有:
(1).URL rewrite:URL重寫
(2).reverse proxy:反向代理
(3). 做緩存服務器
(4). 實現對web服務的負載均衡
(5). 安裝第三方插件,實現健康狀態監測
(6).其他功能(這裏不一一列舉了)
本實驗示意圖如下:
說明:本實驗是在虛擬機環境下實驗,所需要3臺虛擬linux主機,分別爲:
nginx 172.16.22.1
web1 172.16.22.2
web2 172.16.22.2
前提:
配置好yum源,具體配置過程請看:http://lihuan.blog.51cto.com/4391550/800845,這裏不再贅述。
web1和web2上:
# yum install httpd –y
在windows下:
C:\Windows\System32\drivers\etc,修改hosts文件,增加如下三行:
172.16.22.1 www.lihuan.com
172.16.22.2 www.lh.com
172.16.22.3 www.lhlh.com
具體實驗配置過程(在nginx上):
所需軟件包如下:
nginx-1.0.14.tar.gz
下載地址:http://www.nginx.org/
healthcheck_nginx_upstreams.zip
下載地址:https://github.com/cep21/healthcheck_nginx_upstreams
這裏默認軟件包下載到/root下
1.安裝nginx並打入健康狀態監測補丁
- # yum groupinstall "Development Tools" –y
- # yum groupinstall "Development Libraries" –y
- # yum install pcre-devel –y
- # groupadd -r nginx
- # useradd -r -g nginx -s /bin/false -M nginx
- # tar xvf nginx-1.0.14.tar.gz
- # unzip healthcheck_nginx_upstreams.zip
- # cd nginx-1.0.14
- # patch -p1 < /root/cep21-healthcheck_nginx_upstreams-16d6ae7/nginx.patch
- # ./configure \
- --prefix=/usr \
- --sbin-path=/usr/sbin/nginx \
- --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= /root/cep21-healthcheck_nginx_upstreams-16d6ae7
- # make && make install
爲nginx提供服務腳本:
- # vim /etc/rc.d/init.d/nginx
- #!/bin/sh
- #
- # nginx - this script starts and stops the nginx daemon
- #
- # chkconfig: - 85 15
- # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
- # proxy and IMAP/POP3 proxy server
- # processname: nginx
- # config: /etc/nginx/nginx.conf
- # config: /etc/sysconfig/nginx
- # pidfile: /var/run/nginx.pid
- # Source function library.
- /etc/rc.d/init.d/functions
- # Source networking configuration.
- . /etc/sysconfig/network
- # Check that networking is up.
- [ "$NETWORKING" = "no" ] && exit 0
- nginx="/usr/sbin/nginx"
- prog=$(basename $nginx)
- NGINX_CONF_FILE="/etc/nginx/nginx.conf"
- [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
- lockfile=/var/lock/subsys/nginx
- make_dirs() {
- # make required directories
- user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
- options=`$nginx -V 2>&1 | grep 'configure arguments:'`
- for opt in $options; do
- if [ `echo $opt | grep '.*-temp-path'` ]; then
- value=`echo $opt | cut -d "=" -f 2`
- if [ ! -d "$value" ]; then
- # echo "creating" $value
- mkdir -p $value && chown -R $user $value
- fi
- fi
- done
- }
- start() {
- [ -x $nginx ] || exit 5
- [ -f $NGINX_CONF_FILE ] || exit 6
- make_dirs
- echo -n $"Starting $prog: "
- daemon $nginx -c $NGINX_CONF_FILE
- retval=$?
- echo
- [ $retval -eq 0 ] && touch $lockfile
- return $retval
- }
- stop() {
- echo -n $"Stopping $prog: "
- killproc $prog -QUIT
- retval=$?
- echo
- [ $retval -eq 0 ] && rm -f $lockfile
- return $retval
- }
- restart() {
- configtest || return $?
- stop
- sleep 1
- start
- }
- reload() {
- configtest || return $?
- echo -n $"Reloading $prog: "
- killproc $nginx -HUP
- RETVAL=$?
- echo
- }
- force_reload() {
- restart
- }
- configtest() {
- $nginx -t -c $NGINX_CONF_FILE
- }
- rh_status() {
- status $prog
- }
- rh_status_q() {
- rh_status >/dev/null 2>&1
- }
- case "$1" in
- start)
- rh_status_q && exit 0
- $1
- ;;
- stop)
- rh_status_q || exit 0
- $1
- ;;
- restart|configtest)
- $1
- ;;
- reload)
- rh_status_q || exit 7
- $1
- ;;
- force-reload)
- force_reload
- ;;
- status)
- rh_status
- ;;
- condrestart|try-restart)
- rh_status_q || exit 0
- ;;
- *)
- echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
- exit 2
- esac
而後爲此腳本賦予執行權限,添加至服務管理列表,並讓其開機自動啓動,在啓動服務測試:
- # chmod +x /etc/rc.d/init.d/nginx
- # chkconfig --add nginx
- # chkconfig nginx on
- # service nginx start
2.實現nginx URL重寫,實例域名跳轉
- # vim /etc/nginx/nginx.conf #實例如下
- server {
- listen 80;
- server_name www.lihuan.com;
- root html;
- index index.html index.htm;
- rewrite ^/ http://www.lh.com/;
- }
- # service nginx restart
說明:在windows下當你訪問http://www.lihuan.com/的時候,自動跳轉到http://www.lh.com/的服務上了。
3.實現反向代理
- # vim /etc/nginx/nginx.conf
- server {
- listen 80;
- server_name www.lihuan.com;
- root html;
- index index.html index.htm;
- proxy_pass http://www.lh.com;
- }
- # service nginx restart
說明:當你訪問www.lihuan.com的服務時,此時www.lihuan.com並沒有提供web服務,而是反向代理到www.lh.com的web服務上了。
4.實現緩存服務器
- # vim /etc/nginx/nginx.conf
- http {
- proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
- server {
- location / {
- proxy_pass http:// www.lh.com;
- proxy_set_header Host $host;
- proxy_cache STATIC;
- proxy_cache_valid 200 302 10m;
- proxy_cache_valid 301 1h;
- proxy_cache_valid any 1m;
- proxy_cache_use_stale error timeout invalid_header updating
- http_500 http_502 http_503 http_504;
- }
- }
- }
- # service nginx restart
說明:當你訪問www.lihuan.com反向代理到www.lh.com的時候,明顯比直接訪問www.lh.com的速度快,這就是緩存服務的作用。
5.做負載均衡
- # vim /etc/nginx/nginx.conf
- upstream loadbalance {
- server www.lh.com weight=5;
- server www.lhlh.com;
- }
- server {
- listen 80;
- server_name www.lihuan.com;
- location / {
- include proxy.conf;
- proxy_pass http:// loadbalance;
- }
- }
- # service nginx restart
說明:在windows主機上當你訪問www.lihuan.com的時候,刷新幾次會是不同的頁面,這就說明負載均衡實現了(如果想明確看到實驗結果,可以讓web1和web2的頁面不一樣,但實際工作中,二者的web頁面數據是完全一致的)
6.實現健康狀態監測
- # vim /etc/nginx/nginx.conf
- http {
- upstream loadba {
- server 172.16.22.2:80;
- server 172.16.22.3:80;
- healthcheck_enabled;
- healthcheck_delay 1000;
- healthcheck_timeout 1000;
- healthcheck_failcount 1;
- healthcheck_send "GET /.health HTTP/1.0";
- }
- server {
- listen 80;
- location / {
- proxy_set_header Host $http_host;
- proxy_pass http://172.16.22.2;
- proxy_connect_timeout 3;
- }
- location /stat {
- healthcheck_status;
- }
- }
- }
- # service nginx restart
說明:當你在地址欄裏輸入:http://172.16.22.1/stat,即可看到web1和web2 web服務的健康狀況的。