设为首页 收藏本站
查看: 1144|回复: 0

[经验分享] 关于nginx,你不可不知的几大特色功能

[复制链接]

尚未签到

发表于 2018-11-13 08:44:43 | 显示全部楼层 |阅读模式
  关于nginx,你不可不知的几大特色功能
  nginx,轻量级的http服务与反向代理服务器软件,由于其并发能力较强,并且体积很小,所以被称为轻量级http服务软件。
  nginx的特色功能有:
  (1).URL rewrite:URL重写
  (2).reverse proxy:反向代理
  (3). 做缓存服务器
  (4). 实现对web服务的负载均衡
  (5). 安装第三方插件,实现健康状态监测
  (6).其他功能(这里不一一列举了)
  本实验示意图如下:
DSC0000.png

  
  说明:本实验是在虚拟机环境下实验,所需要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.
  • [ &quot;$NETWORKING&quot; = &quot;no&quot; ] && exit 0

  • nginx=&quot;/usr/sbin/nginx&quot;
  • prog=$(basename $nginx)

  • NGINX_CONF_FILE=&quot;/etc/nginx/nginx.conf&quot;

  • [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx

  • lockfile=/var/lock/subsys/nginx

  • make_dirs() {
  •    # make required directories
  •    user=`nginx -V 2>&1 | grep &quot;configure arguments:&quot; | 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 &quot;=&quot; -f 2`
  •            if [ ! -d &quot;$value&quot; ]; then
  •                # echo &quot;creating&quot; $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 $&quot;Starting $prog: &quot;
  •     daemon $nginx -c $NGINX_CONF_FILE
  •     retval=$?
  •     echo
  •     [ $retval -eq 0 ] && touch $lockfile
  •     return $retval
  • }

  • stop() {
  •     echo -n $&quot;Stopping $prog: &quot;
  •     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 $&quot;Reloading $prog: &quot;
  •     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 &quot;$1&quot; 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 $&quot;Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}&quot;
  •         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 &quot;GET /.health HTTP/1.0&quot;;
  •                      }
  •   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服务的健康状况的。



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-634348-1-1.html 上篇帖子: Nginx 启动出错 error while loading shared libraries: libpcre.so.1-boyhack 下篇帖子: Nginx状态信息(status)配置及信息详解
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表