• nginx调度器


    目录

    源码安装nginx

    rpm安装nginx

     调度器配置

    节点服务器配置


    源码包下载:http://nginx.org/en/download.html   (mainline:开发版  stable:稳定版)

    rpm包的yum源:http://nginx.org/packages/centos/7/x86_64/

            本次通过安装另两个方式安装nginx实现调度功能,配置功能相同。源码安装方式只讲解如何安装,rpm安装将修改配置文件,只需要找到配置文件位置添加配置文件即可。

    源码安装nginx配置主要目录

    /usr/local/nginx/ 安装目录

    /usr/local/nginx/conf/nginx.conf 主配置文件

    /usr/share/nginx/html 网页根目录

    /var/log/nginx/     存放日志的位置,access.log访问日志,error.log错误日志

    rpm安装nginx配置文件路径

    /etc/nginx/nginx.conf 主配置文件,通过include /etc/nginx/conf.d/*.conf;指定扩展配置文件的路径

    /etc/nginx/conf.d/ 扩展配置文件的路径,配置文件名称自定义

    /usr/share/nginx/html 网页根目录

    /var/log/nginx/     存放日志的位置,access.log访问日志,error.log错误日志

    源码安装nginx

    1. 前提环境

    [root@localhost ~]# yum -y install gcc gcc-c++ make libtool zlib zlib-devel pcre pcre-devel openssl openssl-devel

    2. 解压Nginx包

    1. [root@localhost ~]# useradd -s /sbin/nologin nginx
    2. [root@localhost ~]# tar zxf /media/nginx-goodies-nginx-sticky-module-ng-08a395c66e42.tar.gz -C /usr/src
    3. [root@localhost ~]# tar zxf /media/ngx_cache_purge-2.3.tar.gz -C /usr/src
    4. [root@localhost ~]# tar zxf /media/nginx-1.12.0.tar.gz -C /usr/src

    3. 编译安装nginx

    1. [root@localhost ~]# cd /usr/src/nginx-1.12.0/
    2. [root@localhost nginx-1.12.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx \
    3. --with-http_stub_status_module --with-http_realip_module --with-http_ssl_module \
    4. --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client \
    5. --http-proxy-temp-path=/var/tmp/nginx/proxy --http-fastcgi-temp-path=/var/tmp/nginx/fcgi \
    6. --with-pcre --add-module=../ngx_cache_purge-2.3 --with-http_flv_module \
    7. --add-module=../nginx-goodies-nginx-sticky-module-ng-08a395c66e42
    8. [root@localhost nginx-1.12.0]# make && make install

    4. 添加系统服务并启动

    1. [root@localhost ~]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
    2. [root@localhost ~]# mkdir -p /var/tmp/nginx/client
    3. [root@localhost ~]# chown -R nginx:nginx /var/tmp/nginx
    4. [root@localhost ~]# vim /etc/init.d/nginx
    5. #!/bin/bash
    6. # chkconfig: 2345 99 20
    7. # description: Nginx Service Control Script
    8. PROG="/usr/local/nginx/sbin/nginx"
    9. PIDF="/usr/local/nginx/logs/nginx.pid"
    10. case "$1" in
    11.  start)
    12.  netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
    13.  if [ $? -eq 0 ]
    14.  then
    15.  echo "Nginx service already running."
    16.  else
    17.  $PROG -t &> /dev/null
    18.  if [ $? -eq 0 ] ; then
    19.  $PROG
    20.  echo "Nginx service start success."
    21.  else
    22.  $PROG -t
    23.  fi
    24.  fi
    25.  ;;
    26.  stop)
    27.  netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
    28.  if [ $? -eq 0 ]
    29.  then
    30.  kill -s QUIT $(cat $PIDF)
    31.  echo "Nginx service stop success."
    32.  else
    33.  echo "Nginx service already stop"
    34.  fi
    35.  ;;
    36.  restart)
    37.  $0 stop
    38.  $0 start
    39.  ;;
    40.  status)
    41.  netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
    42.  if [ $? -eq 0 ]
    43.  then
    44.  echo "Nginx service is running."
    45.  else
    46.  echo "Nginx is stop."
    47.  fi
    48.  ;;
    49.  reload)
    50.  netstat -anplt |grep ":80" &> /dev/null && pgrep "nginx" &> /dev/null
    51.  if [ $? -eq 0 ]
    52.  then
    53.  $PROG -t &> /dev/null
    54.  if [ $? -eq 0 ] ; then
    55.  kill -s HUP $(cat $PIDF)
    56.  echo "reload Nginx config success."
    57.  else
    58.  $PROG -t
    59.  fi
    60.  else
    61.  echo "Nginx service is not run."
    62.  fi
    63.  ;;
    64.  *)
    65.  echo "Usage: $0 {start|stop|restart|reload}"
    66.  exit 1
    67. esac
    68. [root@localhost ~]# chmod +x /etc/init.d/nginx 
    69. [root@localhost ~]# chkconfig --add nginx 
    70. [root@localhost ~]# chkconfig nginx on
    71. [root@localhost ~]# service  nginx  start 
    72. Nginx service start success.

    5. 验证

    查看版本信息:nginx -v

    查看模块信息:nginx -V

            通过访问Nginx服务器,可以看到Nginx默认网页(如下图)。

    38adf2f9c3204198aa2d7ac58b931762.png

    rpm安装nginx

     调度器配置

    1. [root@localhost ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force  //rpm安装nginx
    2. [root@localhost ~]# cd /etc/nginx/conf.d/
    3. [root@localhost conf.d]# ls
    4. default.conf
    5. [root@localhost conf.d]# rm -rf *  //删除默认扩展配置文件
    6. [root@localhost conf.d]# vim lb.conf //添加扩展配置文件,指定两个节点IP
    7. upstream web {
    8.         server 192.168.1.5:80;
    9.         server 192.168.1.6:80;
    10. }
    11. server {
    12.         listen 80;
    13.         server_name localhost;
    14.         location / {
    15.         root html;
    16.         index index.html index.htm;
    17.         proxy_pass http://web;
    18.         }
    19. }
    20. [root@localhost conf.d]# nginx -t //检查语法
    21. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    22. nginx: configuration file /etc/nginx/nginx.conf test is successful
    23. [root@localhost conf.d]# systemctl start nginx

    节点服务器配置

            两台服务器配置相同,但网页内容需要做出区分,以方便测试。

    1. [root@node1 ~]# rpm -ivh /media/nginx-rpm/* --nodeps --force
    2. [root@node1 ~]# echo "192.168.1.5" > /usr/share/nginx/html/index.html
    3. [root@node1 ~]# systemctl start nginx

            分别访问node1和node2可以看到网页内容分别不同。

     58eae92c82074dd388ca4d164084b83b.png 

     80845a3c0a2a4486a54740fa8c0d2365.png

             此时可以访问调度器IP,刷新两次网址,可以看到两个节点的网站内容即调度完成。

    b96262680e094ee588688478a57d8eb0.png

  • 相关阅读:
    C# 之 选择并调用文件[winform]
    mysql之事务二阶段提交
    【计算机网络】 Nagle算法
    GitHub官方出手,一针见血。Spring Boot趣味实战手册来袭(彩版)
    springcloud五大组件:Eureka:注册中心、Zuul:服务网关、Ribbon:负载均衡、Feign:服务调用、Hystix:熔断器
    使用HttpClient+Jsoup实现网络爬虫抓取商品数据信息
    Linux网络编程系列之服务器编程——阻塞IO模型
    Linux命令(127)之hash
    Unity2021发布微信小游戏步骤(附带工具和源码)
    Ansible初识以及安装
  • 原文地址:https://blog.csdn.net/qq_61116007/article/details/127831397