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

[经验分享] Nginx 高级扩展实例

[复制链接]

尚未签到

发表于 2018-11-8 06:33:00 | 显示全部楼层 |阅读模式
  一、首先安装Nginx:
  

  


  • # yum -y install pcre-devel

  • # groupadd -r nginx
  • # useradd -r -g nginx -s /sbin/nologin -M nginx

  • # tar xf nginx-1.2.3.tar.gz
  • # cd nginx-1.2.3
  • # ./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/ \
  •   --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \
  •   --http-scgi-temp-path=/var/tmp/nginx/scgi \
  •   --with-pcre

  • # make && make install
  

  新建文件/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
  
二、URL重写:
  
格式如下
  
rewrite   
  例:rewrite /images/(.*\.jpg) http://192.168.80.146/images/$1 permanent;
  将访问本机的任何以.jpg结尾的都永久重定向到http://192.168.80.146/images/下的.jpg去
  
标识有以下四种:
  
last —— 持续标识,替换后的规则在继续匹配,拿更改后的地址重新去匹配,会产生死循环
  
例:location /image/ {
  
          rewrite /images/.*\.jpg http://192.168.80.143/images/a.jpg last;
  
  }
  
  这样就残生死循环了,所以在定义的时候要避免重复
  

  
break —— 结束标识,表示到此为止,不再继续匹配
  
redirect —— 临时重定向标识,http协议代码为302
  
permanent —— 永久重定向标识,http协议代码为301
  例:在192.168.80.146上定义重写如下
  
location / {
  
                rewrite ^/forum/(test.html)$ http://192.168.80.143/bbs/$1 permanent;
  
        }
  # server nginx restart
  并在192.168.80.143上建立/var/www/html/bbs/test.html文件
  在浏览器中输入http://192.168.80.146/forum/test.html 访问看看:
DSC0000.png

  
可以看到自动重写为http://192.168.80.143/bbs/test.html
  这里重定向也可以使用if指令:
  
语法: if (condition) { ... }
  
应用环境: server, location
  条件:
  
1、变量名; false values are: empty string ("", or any string starting with "0";)
  
2、对于变量进行的比较表达式,可使用=或!=进行测试;
  
3、正则表达式的模式匹配:
  
~  区分大小的模式匹配
  
~* 不区分字母大小写的模式匹配
  
!~ 和 !~* 分别对上面的两种测试取反
  
4、测试文件是否存在-f或!-f
  
5、测试目录是否存在-d或!-d
  
6、测试目录、文件或链接文件的存在性-e或!-e
  
7、检查一个文件的执行权限-x或!-x
  例:如果用户请求的页面不存在,实现自定义跳转:
  if (!-f $request_filename) {
  
      rewrite ^(/.*)$ /rewrite.html permanent;
  
}
  三、配置反向代理缓存:
  
nginx : 192.168.80.146
  
web1:192.168.80.143
  web1配置:
  


  • # yum -y install httpd
  • # echo "web1" > /var/www/html/index.html
  • # service httpd start
  

  nginx配置:
  


  • # egrep -v "^$|#" /etc/nginx/nginx.conf
  • worker_processes  1;
  • events {
  •     worker_connections  1024;
  • }
  • http {
  •     include       mime.types;
  •     default_type  application/octet-stream;
  •     proxy_cache_path /var/www/cache levels=1:2 keys_zone=mycache:20m  max_size=2048m inactive=60m;   //指定缓存目录/var/www/cache,levels定义缓存级别,缓存名字mycache,最大缓存2048M,非活动时间
  •     proxy_temp_path /var/www/cache/tmp;  //临时缓存目录
  •     sendfile        on;
  •     keepalive_timeout  65;
  •     server {
  •         listen       80;
  •         server_name  localhost;
  •         location / {
  •      proxy_pass http://192.168.80.143;  //指定web服务器
  •      proxy_cache mycache;   //指定前面定义的缓存名字
  •             proxy_cache_valid 200 302 60m;  //定义http协议200,302 的缓存60min
  •      proxy_cache_valid 404 1m;   //http协议404缓存1min
  •         }
  •         error_page   500 502 503 504  /50x.html;
  •         location = /50x.html {
  •             root   html;
  •         }
  •     }
  • }

  • # mkdir -p /var/www/cache
  • # service nginx restart
  

  配置好了以后先访问先80.146看到是默认nginx的页面:
DSC0001.png

  之后我们启动服务开始代理,在访问80.146看到的后端web1的页面:
DSC0002.png

  四、nginx负载均衡:
  
nginx:192.168.80.146
  
web1:192.168.80.143
  
web2:192.168.80.144(web2同web1即可)
  


  • # egrep -v "^$|#" /etc/nginx/nginx.conf
  • worker_processes  1;
  • events {
  •     worker_connections  1024;
  • }
  • http {
  •     include       mime.types;
  •     default_type  application/octet-stream;
  •     upstream peace {     //名字为peace
  •        server 192.168.80.143 weight=1;   //定义节点信息,weight指定权重
  •        server 192.168.80.144 weight=2;
  •     }
  •     sendfile        on;
  •     keepalive_timeout  65;
  •     server {
  •         listen       80;
  •         server_name  localhost;
  •         location / {
  •               proxy_pass http://peace;   //引用peace的负载均衡
  •         }
  •         error_page   500 502 503 504  /50x.html;
  •         location = /50x.html {
  •             root   html;
  •         }
  •     }
  • }

  • # service nginx restart
  

  下面访问下nginx可以看到有轮询
DSC0003.png DSC0004.png

  
但是这里有个问题就是没有状态检查,有错误时还照样轮询,所以这里使用第三方来做健康监测
  
五、nginx 负载均衡+ 第三方模块,健康状态检测
  开始打算用cep21-healthcheck_nginx_upstreams-16d6ae7.zip来做的,但是发现有版本错误,在网上找了篇文章,http://www.myhack58.com/Article/sort099/sort0102/2012/34260.htm 可以再1.2.1以上使用了,结合了healthcheck_nginx_upstreams的来做
  https://github.com/yaoweibin/nginx_upstream_check_module 下载软件
  
需要打补丁,并且重新编译
  


  • # unzip yaoweibin-nginx_upstream_check_module-v0.1.6-17-gdfee401.zip

  • # cd nginx-1.2.3
  • # patch -p1 < /root/yaoweibin-nginx_upstream_check_module-dfee401/check_1.2.1+.patch

  • (如果nginx版本不是1.2.1以上的,用patch -p1 < /usr/local/yaoweibin-nginx_upstream_check_module-dfee401/check.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/yaoweibin-nginx_upstream_check_module-dfee401/

  • # make && make install
  

  修改配置文件:
  


  • # egrep -v &quot;^$|#&quot; /etc/nginx/nginx.conf
  • worker_processes  1;
  • events {
  •     worker_connections  1024;
  • }
  • http {
  •     include       mime.types;
  •     default_type  application/octet-stream;
  •     upstream peace {
  •         server 192.168.80.143 weight=1;  //指定节点web1
  • server 192.168.80.144 weight=2;  //指定节点web2
  •         check interval=1000 rise=2 fall=2 timeout=1000;  //interval检测间隔时间,rsie请求2次正常的话为up,fail请求2次失败的话为down,timeout检查超时时间(毫秒)
  • check_http_send &quot;GET /.test.html HTTP/1.0&quot;;  //所发送的检测请求
  •     }
  •     sendfile        on;
  •     keepalive_timeout  65;
  •     server {
  •         listen       80;
  •         server_name  localhost;
  •         location / {
  •      proxy_pass http://peace;  //引用
  •         }
  • location /status {   //定义一个类似stub_status的方式输出检测信息
  •   check_status;
  • }
  •         error_page   500 502 503 504  /50x.html;
  •         location = /50x.html {
  •             root   html;
  •         }
  •     }
  • }

  • # service nginx restart
  

  下面访问下192.168.80.146/status可以看到两台都up,并且页面正常:
DSC0005.png

  
之后关闭一台web2,可以看到已经down,并且访问都是web1工作:
DSC0006.png

  至此Nginx的相关扩展就结束了,如有错误请指出,非常感谢!



运维网声明 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-632059-1-1.html 上篇帖子: HAProxy Nginx LVS Apache总结篇 下篇帖子: Centos7.3安装nginx-loong576的博客
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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