zoukankan      html  css  js  c++  java
  • Nginx源码编译安装与负载均衡配置实践

    1. 首先安装一些库

    sudo yum install pcre-devel zlib-devel openssl-devel

    2. 去官网下载源码,并进行编译安装

    tar -xzvf nginx-1.0.14.tar.gz
    cd nginx-1.0.14
    ./configure --prefix=/opt/nginx \
    make && make install

    参数详解:
    --prefix #nginx安装目录,默认在/usr/local/nginx
    --user=nginx #运行nginx的用户
    --group=nginx #运行nginx的用户组
    --pid-path #pid问件位置,默认在logs目录
    --lock-path #lock问件位置,默认在logs目录
    --with-http_ssl_module #开启HTTP SSL模块,以支持HTTPS请求。
    --with-http_dav_module #开启WebDAV扩展动作模块,可为文件和目录指定权限
    --with-http_flv_module #支持对FLV文件的拖动播放
    --with-http_realip_module #支持显示真实来源IP地址
    --with-http_gzip_static_module #预压缩文件传前检查,防止文件被重复压缩
    --with-http_stub_status_module #取得一些nginx的运行状态
    --with-mail #允许POP3/IMAP4/SMTP代理模块
    --with-mail_ssl_module #允许POP3/IMAP/SMTP可以使用SSL/TLS
    --with-pcre=../pcre-8.11 #指定未安装的pcre路径
    --with-zlib=../zlib-1.2.5 #注意是未安装的zlib路径
    --with-debug #允许调试日志
    --http-client-body-temp-path #客户端请求临时文件路径
    --http-proxy-temp-path #设置http proxy临时文件路径
    --http-fastcgi-temp-path #设置http fastcgi临时文件路径
    --http-uwsgi-temp-path #设置uwsgi 临时文件路径
    --http-scgi-temp-path #设置scgi 临时文件路径

    或者通过yum安装,首先安装EPEL on Centos 6 / RHEL 6:

    rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-6.noarch.rpm

    然后直接yum安装

    yum install nginx

    3.配置Nginx服务脚本

    #!/bin/sh
    #
    # nginx - this script starts and stops the nginx daemin
    #
    # chkconfig:   - 85 15
    # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
    #               proxy and IMAP/POP3 proxy server
    # processname: nginx
    # config:      /usr/local/nginx/conf/nginx.conf
    # pidfile:     /usr/local/nginx/logs/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="/opt/nginx/sbin/nginx"
    prog=$(basename $nginx)
    
    NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
    
    lockfile=/opt/nginx/lock/nginx.lock
    
    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        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
        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 /opt/nginx/init.d/nginx

    这样,就可以通过以下方式来管理Nginx服务:
    # /opt/nginx/init.d/nginx start
    # /opt/nginx/init.d/nginx stop
    # /opt/nginx/init.d/nginx restart
    # /opt/nginx/init.d/nginx reload

    4.Nignx负载均衡配置

    # cd /opt/nginx/conf/
    # mv nginx.conf nginx.conf.bak

    # vim /opt/nginx/conf/nginx.conf

    user  nginx nginx;
    
    worker_processes 10;
    
    error_log  logs/error.log;
    
    worker_rlimit_nofile 51200;
    
    events
    {
          use epoll;
          worker_connections 51200;
    }
    
    http
    {
          include       mime.types;
          default_type  application/octet-stream;
    
          keepalive_timeout 120;
    
          tcp_nodelay on;
    
          upstream  192.168.203.133  {
                  #ip_hash;
                  server   192.168.203.134:80;
                  server   192.168.203.135:80;
                  server   192.168.203.136:80;
                  server   192.168.203.137:80;
          }
    
          server
          {
                  listen  80;
                  server_name  192.168.203.133;
    
                  location / {
                           proxy_pass        http://192.168.203.133;
                           proxy_set_header   Host             $host;
                           proxy_set_header   X-Real-IP        $remote_addr;
                           proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                  }
    
                  log_format  192_168_203_133  '$remote_addr - $remote_user [$time_local] $request '
                                    '"$status" $body_bytes_sent "$http_referer" '
                                    '"$http_user_agent" "$http_x_forwarded_for"';
                  access_log  /opt/nginx/logs/cluster.log 192_168_203_133;
          }
    }

    启动Nginx服务
    # /opt/nginx/init.d/nginx start

    通过浏览器直接访问http://192.168.203.133,可以发现,在多次刷新之后,请求会随机分配到后端的Web服务器上。

  • 相关阅读:
    centos配置ADSL拨号 配置阿里云的yum源
    强烈推荐:一个网页转安卓App的工具,可以在线云打包apk
    【html5】input标签capture属性不为人知的秘密
    国庆微信头像在线生成器,头像加国旗小工具
    这是一个玩法简单又充满刺激的安卓App游戏
    微信群二维码突破100人限制和7天不过期的方法
    Xcode9学习笔记84
    Xcode9学习笔记83
    Xcode9学习笔记80
    python 压缩文件.zip
  • 原文地址:https://www.cnblogs.com/feichan/p/nginx.html
Copyright © 2011-2022 走看看