`
ybbkd2
  • 浏览: 33151 次
社区版块
存档分类
最新评论

Redhat/CentOS 5.7 安装Nginx 1.8 及配置反向代理手册

 
阅读更多
准备安装介质:

nginx 1.8 Stable version:

wget http://nginx.org/download/nginx-1.8.0.tar.gz

ngx_cache_purge:

wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz


操作系统基本配置:

1. 创建nginx用户

# useradd nginx

设置用户密码:
# passwd nginx

修改操作系统进程数及openfiles 限制:

# vi /etc/security/limits.conf
增加如下4行:
# add by nginx
nginx           hard    nofile          65535
nginx           soft    nofile          65535
nginx           hard    nproc           4096
nginx           soft    nproc           4096


保存退出。

# vi /etc/pam.d/login

增加1行:
session    required     pam_limits.so


保存退出。

创建编译工作目录:
# mkdir nginx-src
# mv nginx-1.8.0.tar.gz   nginx-src/
# mv ngx_cache_purge-2.3.tar.gz   nginx-src/

解压缩:

# cd nginx-src
# tar xzvf nginx-1.8.0.tar.gz
# tar xzvf ngx_cache_purge-2.3.tar.gz

编译:

# cd nginx-1.8.0


#  ./configure  --prefix=/etc/nginx --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.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=../ngx_cache_purge-2.3


# make && make install


创建cache 路径:

# mkdir –p  /var/nginx_cache

修改配置文件:

# vi /etc/nginx/conf.d/default.conf

# 反向代理缓存配置
proxy_cache_path /var/nginx_cache levels=2:2 keys_zone=websitecache:1024m inactive=1d  max_size=10g;
#反向代理服务器URL配置
server {
       listen       80;
       server_name  localhost;

        location / {
                proxy_pass      http://10.12.2.57;
                proxy_set_header  X-Real-IP  $remote_addr;

                proxy_cache       websitecache;
                proxy_cache_key $host$uri$is_args$args;
                proxy_ignore_headers   Cache-Control Set-Cookie X-Accel-Expires Expires;
                proxy_hide_header      Cache-Control;
                proxy_hide_header      Set-Cookie;
                add_header X-Cache $upstream_cache_status;

                proxy_cache_valid 200 302 25m;
                proxy_cache_valid any 1m;
        }



        #实现只代理,不缓存
        location ^~ /HQ/ {
                proxy_pass      http://10.12.2.57;
                proxy_set_header  X-Real-IP  $remote_addr;
        }

     
 
        # 只代理不缓存
        location ^~ /flash/ {
                proxy_pass     http://10.12.2.57;
                proxy_set_header  X-Real-IP  $remote_addr;
        }


        #purge 模块,实现清除指定URL缓存
        location ~ /clear(/.*) {
          allow 127.0.0.1;
          deny all;
          proxy_cache_purge websitecache $host$1$is_args$args;
        }

}




修改主配置文件:

# vi /etc/nginx/nginx.conf



user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  2048;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    log_format format1  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for" '
                        '$request_time' ;

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    # 启用 GZIP 压缩
    gzip  on;
    gzip_static on;
    gzip_http_version 1.0;
    gzip_disable "MSIE [1-6].";
    gzip_min_length  1020;
    gzip_comp_level 5;
    gzip_proxied any;
    gzip_types  text/html text/plain text/css application/x-javascript application/javascript application/xml;


    include /etc/nginx/conf.d/*.conf;
}




修改完毕后保存退出。

创建Linux Service

# vi /etc/init.d/nginx


#!/bin/sh
#
# nginx        Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -L $0 ]; then
    initscript=`/bin/readlink -f $0`
else
    initscript=$0
fi

sysconfig=`/bin/basename $initscript`

if [ -f /etc/sysconfig/$sysconfig ]; then
    . /etc/sysconfig/$sysconfig
fi

nginx=${NGINX-/usr/sbin/nginx}
prog=`/bin/basename $nginx`
conffile=${CONFFILE-/etc/nginx/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=${SLEEPMSEC-200000}
UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
RETVAL=0

start() {
    echo -n $"Starting $prog: "

    daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && touch ${lockfile}
    return $RETVAL
}

stop() {
    echo -n $"Stopping $prog: "
    killproc -p ${pidfile} ${prog}
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    killproc -p ${pidfile} ${prog} -HUP
    RETVAL=$?
    echo
}

upgrade() {
    oldbinpidfile=${pidfile}.oldbin

    configtest -q || return
    echo -n $"Starting new master $prog: "
    killproc -p ${pidfile} ${prog} -USR2
    echo

    for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
        /bin/usleep $SLEEPMSEC
        if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
            echo -n $"Graceful shutdown of old $prog: "
            killproc -p ${oldbinpidfile} ${prog} -QUIT
            RETVAL=$?
            echo
            return
        fi
    done

    echo $"Upgrade failed!"
    RETVAL=1
}

configtest() {
    if [ "$#" -ne 0 ] ; then
        case "$1" in
            -q)
                FLAG=$1
                ;;
            *)
                ;;
        esac
        shift
    fi
    ${nginx} -t -c ${conffile} $FLAG
    RETVAL=$?
    return $RETVAL
}

rh_status() {
    status -p ${pidfile} ${nginx}
}

# See how we were called.
case "$1" in
    start)
        rh_status >/dev/null 2>&1 && exit 0
        start
        ;;
    stop)
        stop
        ;;
    status)
        rh_status
        RETVAL=$?
        ;;
    restart)
        configtest -q || exit $RETVAL
        stop
        start
        ;;
    upgrade)
        rh_status >/dev/null 2>&1 || exit 0
        upgrade
        ;;
    condrestart|try-restart)
        if rh_status >/dev/null 2>&1; then
            stop
            start
        fi
        ;;
    force-reload|reload)
        reload
        ;;
    configtest)
        configtest
        ;;
    *)
        echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
        RETVAL=2
esac

exit $RETVAL



保存退出。

注册 nginx 服务:

#chmod +x /etc/init.d/nginx

#chkconfig --add nginx

#chkconfig --level 2345 nginx on

#chkconfig --list nginx


检查配置:

# service nginx configtest

确认没问题后,启动服务:
# service nginx start

如果修改了ningx配置,需要生效:

# service nginx reload
分享到:
评论
1 楼 ybbkd2 2015-12-31  
nginx 负载均衡及session保持一致plugin

https://bitbucket.org/nginx-goodies/nginx-sticky-module-ng

相关推荐

Global site tag (gtag.js) - Google Analytics