Nginx的特色功能
nginx由于积很小,并发能力较强,因此被称为轻量级http服务软件,那关于nginx的特色功能你知道多少呢?现在我们就跟爱站小编一起去看看Nginx的特色功能介绍吧。
关于nginx,你不可不知的几大特色功能
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://www.linuxidc.com/Linux/2012-04/58929.htm,这里不再赘述。
web1和web2上:
# yum install httpd –y
在windows下:
C:\Windows\System32\drivers\etc,修改hosts文件,增加如下三行:
172.16.22.1 www.linuxidc.net
172.16.22.2 www.linuxidc.com
172.16.22.3 www.lhlinuxidc.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.linuxidc.net;
- root html;
- index index.html index.htm;
- rewrite ^/ http://www.linuxidc.com/;
- }
- # service nginx restart
说明:在windows下当你访问http://www.linuxidc.net/的时候,自动跳转到http://www.linuxidc.com/的服务上了。
3.实现反向代理
- # vim /etc/nginx/nginx.conf
- server {
- listen 80;
- server_name www.linuxidc.net;
- root html;
- index index.html index.htm;
- proxy_pass http://www.linuxidc.com;
- }
- # service nginx restart
说明:当你访问www.linuxidc.net的服务时,此时www.linuxidc.net并没有提供web服务,而是反向代理到www.linuxidc.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.linuxidc.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.linuxidc.net反向代理到www.linuxidc.com的时候,明显比直接访问www.linuxidc.com的速度快,这就是缓存服务的作用。
5.做负载均衡
- # vim /etc/nginx/nginx.conf
- upstream loadbalance {
- server www.linuxidc.com weight=5;
- server www.lhlinuxidc.com;
- }
- server {
- listen 80;
- server_name www.linuxidc.net;
- location / {
- include proxy.conf;
- proxy_pass http:// loadbalance;
- }
- }
- # service nginx restart
说明:在windows主机上当你访问www.linuxidc.net的时候,刷新几次会是不同的页面,这就说明负载均衡实现了(如果想明确看到实验结果,可以让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服务的健康状况的。
以上就是关于Nginx的特色功能介绍,看完本文内容后你是否对Nginx有了更深一步的理解呢?
上一篇:推荐用Ubuntu服务器的原因
下一篇:Linux实现待机和休眠的方法