nginx 负载均衡配置

1、安装相关依赖    yum -y install gcc gcc-c++ make libtool zlib zlib-devel openssl openssl-devel pcre pcre-devel

2、另外下载模块nginx_upstream_check_module-0.3.0.tar.gz —— 检查后端服务器的状态  https://github.com/yaoweibin/nginx_upstream_check_module/releases

3、加压pcre  tar -xzf pcre-8.34.tar.gz  mv pcre-8.34 /usr/local/pcre

       解压并重命名为

v古典风格

3、cd nginx-1.20.1   

  ./configure --prefix=/usr/local/nginx --with-pcre=/usr/local/pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module  --with-http_image_filter_module --add-module=/usr/local/nginx_upstream

make && make install

4、/usr/local/nginx/sbin/nginx -v

5、添加运行使用用户

6、/usr/sbin/groupadd nginx

7、/usr/sbin/useradd -g nginx nginx

8、检查配置文件nginx.conf的正确性命令:

/usr/local/nginx/sbin/nginx -t

9、Nginx 启动命令如下:

/usr/local/nginx/sbin/nginx

/usr/local/nginx/sbin/nginx -s reopen

测试一下是否有错:/usr/local/nginx/sbin/nginx -t

平滑重启:/usr/local/nginx/sbin/nginx -s reload

10、加入系统服务

vim /etc/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=" /usr/local/nginx/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/local/nginx/sbin/nginx"

prog=$(basename $nginx)

NGINX_CONF_FILE="/usr/local/nginx/conf/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:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`

  if [ -n "$user" ]; then

      if [ -z "`grep $user /etc/passwd`" ]; then

        useradd -M -s /bin/nologin $user

      fi

      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

    fi

}

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 $prog -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


11、 设置执行权限chmod +x /etc/init.d/nginx

12、设置中的模式开机启动/sbin/chkconfig nginx on

检查一下## 显示开机启动自动列表是否存在nginx chkconfig --list # 显示开机自动启动的服务sudo chkconfig --list nginx

nginx 0:off 1:off 2:on 3:on 4:on 5:on 6:off

完成!

之后,就可以使用以下命令了

service nginx start

service nginx stop

service nginx restart

service nginx reload

service nginx status

将nginx 加入系统变量

修改/etc/profile 文件

  vim /etc/profile

1

添加PATH

如果已经存在PATH 也只需要在下面添加更多的PATH 两者之间不冲突

#  在文件中添加nginx 的执行文件。

export PATH=$PATH:/usr/local/nginx/sbin

1

2

使立即生效

保存文件后PATH 不会立即生效需要执行 source /etc/profile

source /etc/profile

加入执行权限chmod +x nginx

先将nginx服务加入chkconfig管理列表:

chkconfig --add /etc/init.d/nginx

加完这个之后,就可以使用service对nginx进行启动,重启等操作了。

service nginx start

service nginx stop

13、设置nginx 反向代理

vim /usr/local/nginx/conf/nginx.comf

#user nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

        include      mime.types;  #文件扩展名与文件类型映射表

        default_type  application/octet-stream; #默认文件类型,默认为text/plain

        #access_log off; #取消服务日志   

        log_format myFormat ' $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式

        access_log logs/access.log myFormat;  #combined为日志格式的默认值

        sendfile on;  #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。

        sendfile_max_chunk 100k;  #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。

        keepalive_timeout 65;  #连接超时时间,默认为75s,可以在http,server,location块。

        proxy_connect_timeout 1;  #nginx服务器与被代理的服务器建立连接的超时时间,默认60秒

        proxy_read_timeout 1; #nginx服务器想被代理服务器组发出read请求后,等待响应的超时间,默认为60秒。

        proxy_send_timeout 1; #nginx服务器想被代理服务器组发出write请求后,等待响应的超时间,默认为60秒。

        proxy_http_version 1.0 ; #Nginx服务器提供代理服务的http协议版本1.0,1.1,默认设置为1.0版本。

        #proxy_method get;    #支持客户端的请求方法。post/get;

        proxy_ignore_client_abort on;  #客户端断网时,nginx服务器是否终端对被代理服务器的请求。默认为off。

        proxy_ignore_headers "Expires" "Set-Cookie";  #Nginx服务器不处理设置的http相应投中的头域,这里空格隔开可以设置多个。

        proxy_intercept_errors on;    #如果被代理服务器返回的状态码为400或者大于400,设置的error_page配置起作用。默认为off。

        proxy_headers_hash_max_size 1024; #存放http报文头的哈希表容量上限,默认为512个字符。

        proxy_headers_hash_bucket_size 128; #nginx服务器申请存放http报文头的哈希表容量大小。默认为64个字符。

        proxy_next_upstream timeout;  #反向代理upstream中设置的服务器组,出现故障时,被代理服务器返回的状态值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off

        #proxy_ssl_session_reuse on; 默认为on,如果我们在错误日志中发现“SSL3_GET_FINSHED:digest check failed”的情况时,可以将该指令设置为off。

upstream web {

    # 默认以轮询策

    server 192.168.110.251 ;

    server 192.168.110.252 backup;

}

server {

        listen      80;

    # 服务器名称,随便起名

        server_name  www.proxy.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

          # root  html;

          # index  index.html index.htm;

          #设置主机头和客户端真实地址,以便服务器获取客户端真实IP

          proxy_set_header Host $host;

          proxy_set_header X-Real-IP $remote_addr;

          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

          #禁用缓存

          proxy_buffering off;

          # 反向代理的地址

          proxy_pass http://web;

        }

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,826评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,968评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,234评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,562评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,611评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,482评论 1 302
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,271评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,166评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,608评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,814评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,926评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,644评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,249评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,866评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,991评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,063评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,871评论 2 354

推荐阅读更多精彩内容