手机版
你好,游客 登录 注册
背景:
阅读新闻

Nginx不可不知的几大特色功能

[日期:2012-04-20] 来源:Linux社区  作者:lihuan [字体:大 中 小]

为nginx提供服务脚本:

  1. # vim /etc/rc.d/init.d/nginx 
  2. #!/bin/sh 
  3. # 
  4. # nginx - this script starts and stops the nginx daemon 
  5. # 
  6. # chkconfig:   - 85 15 
  7. # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \ 
  8. #               proxy and IMAP/POP3 proxy server 
  9. # processname: nginx 
  10. # config:      /etc/nginx/nginx.conf 
  11. # config:      /etc/sysconfig/nginx 
  12. # pidfile:     /var/run/nginx.pid 
  13.   
  14. # Source function library. 
  15.  /etc/rc.d/init.d/functions 
  16.  
  17.   
  18.  
  19. # Source networking configuration. 
  20.  
  21. . /etc/sysconfig/network 
  22.  
  23. # Check that networking is up. 
  24. [ "$NETWORKING" = "no" ] && exit 0 
  25.  
  26. nginx="/usr/sbin/nginx" 
  27. prog=$(basename $nginx) 
  28.   
  29. NGINX_CONF_FILE="/etc/nginx/nginx.conf" 
  30.  
  31. [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx 
  32.  
  33. lockfile=/var/lock/subsys/nginx 
  34.  
  35. make_dirs() { 
  36.    # make required directories 
  37.    user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 
  38.    options=`$nginx -V 2>&1 | grep 'configure arguments:'` 
  39.    for opt in $options; do 
  40.        if [ `echo $opt | grep '.*-temp-path'` ]; then 
  41.            value=`echo $opt | cut -d "=" -f 2` 
  42.            if [ ! -d "$value" ]; then 
  43.                # echo "creating" $value 
  44.                mkdir -p $value && chown -R $user $value 
  45.            fi 
  46.        fi 
  47.    done 
  48. } 
  49.   
  50. start() { 
  51.     [ -x $nginx ] || exit 5 
  52.     [ -f $NGINX_CONF_FILE ] || exit 6 
  53.     make_dirs 
  54.     echo -n $"Starting $prog: " 
  55.     daemon $nginx -c $NGINX_CONF_FILE 
  56.     retval=$? 
  57.     echo 
  58.     [ $retval -eq 0 ] && touch $lockfile 
  59.     return $retval 
  60. } 
  61.   
  62. stop() { 
  63.     echo -n $"Stopping $prog: " 
  64.     killproc $prog -QUIT 
  65.     retval=$? 
  66.     echo 
  67.     [ $retval -eq 0 ] && rm -f $lockfile 
  68.     return $retval 
  69. }  
  70.  
  71. restart() { 
  72.     configtest || return $? 
  73.     stop 
  74.     sleep 1 
  75.     start 
  76. } 
  77.   
  78. reload() { 
  79.     configtest || return $? 
  80.     echo -n $"Reloading $prog: " 
  81.     killproc $nginx -HUP 
  82.     RETVAL=$? 
  83.     echo 
  84. } 
  85.   
  86. force_reload() { 
  87.     restart 
  88. } 
  89.   
  90. configtest() { 
  91.   $nginx -t -c $NGINX_CONF_FILE 
  92. } 
  93.   
  94. rh_status() { 
  95.     status $prog 
  96. }  
  97.  
  98. rh_status_q() { 
  99.     rh_status >/dev/null 2>&1 
  100. } 
  101.   
  102. case "$1" in 
  103.     start) 
  104.         rh_status_q && exit 0 
  105.         $1 
  106.         ;; 
  107.     stop) 
  108.         rh_status_q || exit 0 
  109.         $1 
  110.         ;; 
  111.     restart|configtest) 
  112.         $1 
  113.         ;; 
  114.     reload) 
  115.         rh_status_q || exit 7 
  116.         $1 
  117.         ;; 
  118.     force-reload) 
  119.         force_reload 
  120.         ;; 
  121.     status) 
  122.         rh_status 
  123.         ;; 
  124.     condrestart|try-restart) 
  125.         rh_status_q || exit 0 
  126.             ;; 
  127.     *) 
  128.         echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" 
  129.         exit 2 
  130. esac 

而后为此脚本赋予执行权限,添加至服务管理列表,并让其开机自动启动,在启动服务测试:

  1. # chmod +x /etc/rc.d/init.d/nginx 
  2. # chkconfig --add nginx 
  3. # chkconfig nginx on 
  4. # service nginx start 

2.实现nginx URL重写,实例域名跳转

  1. # vim /etc/nginx/nginx.conf  #实例如下 
  2. server { 
  3. listen 80; 
  4. server_name www.linuxidc.net; 
  5. root html; 
  6. index index.html index.htm; 
  7. rewrite ^/ http://www.linuxidc.com/; 
  8. } 
  9. # service nginx restart 

说明:在windows下当你访问http://www.linuxidc.net/的时候,自动跳转到http://www.linuxidc.com/的服务上了。

3.实现反向代理

  1. # vim /etc/nginx/nginx.conf 
  2. server { 
  3. listen 80; 
  4. server_name www.linuxidc.net; 
  5. root html; 
  6. index index.html index.htm; 
  7. proxy_pass  http://www.linuxidc.com; 
  8. } 
  9. # service nginx restart 

说明:当你访问www.linuxidc.net的服务时,此时www.linuxidc.net并没有提供web服务,而是反向代理到www.linuxidc.com的web服务上了。

4.实现缓存服务器

  1. # vim /etc/nginx/nginx.conf 
  2. http { 
  3.     proxy_cache_path  /data/nginx/cache  levels=1:2    keys_zone=STATIC:10m    inactive=24h  max_size=1g; 
  4.     server { 
  5.        location / { 
  6.             proxy_pass             http:// www.linuxidc.com; 
  7.             proxy_set_header       Host $host; 
  8.             proxy_cache            STATIC; 
  9.             proxy_cache_valid  200 302 10m; 
  10.             proxy_cache_valid  301 1h; 
  11.             proxy_cache_valid  any 1m; 
  12.             proxy_cache_use_stale  error timeout invalid_header updating 
  13.             http_500 http_502 http_503 http_504; 
  14.                            } 
  15. } 
  16. } 
  17. # service nginx restart 

说明:当你访问www.linuxidc.net反向代理到www.linuxidc.com的时候,明显比直接访问www.linuxidc.com的速度快,这就是缓存服务的作用。

5.做负载均衡

  1. # vim /etc/nginx/nginx.conf 
  2. upstream loadbalance { 
  3.         server www.linuxidc.com weight=5; 
  4.          server www.lhlinuxidc.com;     
  5. } 
  6. server { 
  7.         listen 80; 
  8.          server_name www.linuxidc.net; 
  9.          location / { 
  10.                    include proxy.conf; 
  11.                   proxy_pass http:// loadbalance; 
  12.          } 
  13. } 
  14. # service nginx restart 

说明:在windows主机上当你访问www.linuxidc.net的时候,刷新几次会是不同的页面,这就说明负载均衡实现了(如果想明确看到实验结果,可以让web1和web2的页面不一样,但实际工作中,二者的web页面数据是完全一致的)

 6.实现健康状态监测

  1. # vim /etc/nginx/nginx.conf 
  2. http { 
  3.   upstream loadba { 
  4.     server 172.16.22.2:80; 
  5.     server 172.16.22.3:80; 
  6.     healthcheck_enabled; 
  7.     healthcheck_delay 1000; 
  8.     healthcheck_timeout 1000; 
  9.     healthcheck_failcount 1; 
  10.     healthcheck_send "GET /.health HTTP/1.0"; 
  11.                      } 
  12.   server { 
  13.     listen 80; 
  14.     location / { 
  15.       proxy_set_header Host $http_host; 
  16.       proxy_pass http://172.16.22.2; 
  17.       proxy_connect_timeout 3; 
  18.                  } 
  19.     location /stat { 
  20.       healthcheck_status; 
  21.                     } 
  22.                  } 
  23. } 
  24. # service nginx restart 

说明:当你在地址栏里输入:http://172.16.22.1/stat,即可看到web1和web2 web服务的健康状况的。

linux
相关资讯       Nginx 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款