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

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

相关推荐

    redhat/centos版本>=7.x系统一键安装mysql数据库5.7.x并配置基线安全策略shell脚本

    5.暂时不支持centos和redhat 版本为6.x的系统自动安装 6.可同时按住Ctrl和c停止脚本的安装运行 7.若运行脚本表明您愿意承担脚本运行带来的风险和责任,请提前检查备份 8.反馈可联系qq 1024588739 9.可安装目前最新版...

    RedHat/CentOS7 离线安装oracle 11g_r2_x64所有依赖包,亲测可用

    RedHat/CentOS7离线安装Oracle11g_R2_x64所需的依赖包。 rpm -ivh \ mpfr-3.1.1-4.el7.x86_64.rpm \ compat-libstdc++-33-3.2.3-72.el7.x86_64.rpm \ libmpc-1.0.1-3.el7.x86_64.rpm \ libXau-1.0.8-2.1.el7.x86_64....

    华为存储多路径软件(适用于RedHat/CentOS)

    总的来说,华为存储多路径软件是提升RedHat/CentOS系统与华为存储设备间连接稳定性和性能的重要工具。它通过多路径技术、负载均衡、故障恢复等功能,确保了数据的可靠传输,降低了系统风险,提升了IT基础设施的可用...

    RedHat/CentOS ext4无法格式化大分区 补充ext4格式化方式

    RedHat/CentOS ext4无法格式化大分区 补充ext4格式化方式

    redhat/centos6通用yum源

    通用版redhat6或者centos6的yum源

    ansible-nexus3-oss, Ansible角色为 redhat/centos提供 Nexus 仓库管理器 3.x.zip

    ansible-nexus3-oss, Ansible角色为 redhat/centos提供 Nexus 仓库管理器 3.x Ansible角色:Nexus 3-操作系统这里角色在 centos/rhel上安装和配置 Nexus 存储库管理器 操作系统 版本 3.x 。除了 blobstores -related...

    RedHat 7 编译安装Nginx 1.12并配置WEB站点.pdf

    在Linux系统中,尤其是RedHat 7环境下编译安装Nginx是一个相对复杂的过程,因为需要手动安装Nginx所依赖的库,例如pcre、zlib和openssl等,并且需要按照一定顺序进行安装。本文将详细介绍如何在RedHat 7上编译安装...

    Centos7.9安装zabbix6.0LTS版

    CentOS 7.9 安装 Zabbix 6.0 LTS 版 Zabbix 是一个基于 Web 的网络监控系统,能够实时监控服务器、网络设备和应用程序的状态。下面是 CentOS 7.9 安装 Zabbix 6.0 LTS 版的详细步骤。 安装 Nginx 首先,需要安装 ...

    zabbix snmp redhat7/centos7 离线安装包

    离线安装Zabbix SNMP涉及到对网络管理协议的理解,RHEL/CentOS系统的操作,以及Zabbix组件的配置。确保每个步骤都正确执行,以确保有效的网络监控。如果遇到问题,应查阅官方文档或社区资源来解决。

    redhat/centos/linux系统上zookeeper和kafka进程监控脚本

    如下所示,手动指定zookeeper和kafka的bin目录、配置文件以及需要添加定时任务的周期几个参数后,执行当前脚本,脚本会自动添加定时任务并开始监控zookeeper及kafka进程,如果进程不存在则重启并放入后台,存在则...

    OEL/Redhat/CentOS gcc、g++的全套rpm安装包

    在OEL、RHEL和CentOS上安装GCC和G++,首先需要确保系统中的RPM包管理系统正常运行。以下是安装GCC和G++的RPM包的步骤: 1. 更新系统包列表: 对于使用YUM的系统(如CentOS 6及更早版本): ``` sudo yum update ...

    redhat/centos 创建yum本地源所需安装rpm包以及说明文档

    在Linux系统管理中,尤其是对于企业级服务器,`yum`(Yellowdog Updater, Modified)是Red Hat和CentOS等基于RPM包管理系统的发行版中广泛使用的软件包管理器。`yum`允许用户方便地安装、更新和删除软件包,同时也...

    RedHat Linux Enterprise 5.7 下安装Oracle 10g 教程

    ### RedHat Linux Enterprise 5.7 下安装Oracle 10g 教程 #### 一、准备工作 在开始安装Oracle 10g之前,确保已经完成以下准备工作: 1. **操作系统环境**: 确认使用的是RedHat Linux Enterprise 5.7版本。 2. **...

    Centos 7.5 线下安装 nginx 所需的安装包

    Nginx则是一款高性能的HTTP和反向代理服务器,以其高效的性能、稳定性以及低内存占用而受到青睐。本文将详细介绍如何在离线环境下,在CentOS 7.5上安装Nginx所需的步骤,以及提供的压缩包`centos7_gcc`的作用。 ...

    nginx-1.14.2-tcp+tcp反向代理模块+check模块

    # 201908亲测 rhel/redhat/centos6 rhel/redhat/centos7 亲测可用 #解包内含简要编译步骤 #主要包含模块 开源tcp模块 https://github.com/yaoweibin/nginx_tcp_proxy_module 开源check模块,可以检查http、tcp健康 ...

    redhat/centos6.9 kmod-oracleasm/oracleasm-support/oracleasm rpm包

    kmod-oracleasm-2.0.8-15.el6_9.x86_64 ...安装顺序: rpm -ivh kmod-oracleasm-2.0.8-15.el6_9.x86_64.rpm rpm -ivh oracleasm-support-2.1.8-1.el6.x86_64.rpm rpm -ivh oracleasmlib-2.0.4-1.el6.x86_64.rpm

    openssh 9.9p1 el8(redhat/centos el8 x86-64)

    安装前,可以多开几个ssh窗口,也可以开启telnet服务,防止意外中断无法访问服务器 rpm -e openssh --nodeps rpm -e openssh-server --nodeps rpm -e openssh-clients --nodeps rpm -e openssh-askpass --nodeps ...

    Redhat 使用CentOS的yum源进行升级或软件安装

    Redhat 使用CentOS的yum源进行升级或软件安装 分类: linux 2012-07-19 15:54 1113人阅读 评论(0) 收藏 举报 Redhat默认的源不但速度不给力,而且软件版本陈旧,今天试着将Redhat默认源替换为CentOS的163源,发现...

    RHEL/CentOS 5 Yum方式安装 MySQL5.7

    安装时先卸载mariadb 和 已安装的mysql:rpm –qa | grep maria, rpm –qa | grep –...mysql5.7 rpm安装顺序,common→libs→client→server→devel 安装过程报依赖错误,使用 rpm –ivh xxx --nodeps --force 即可。

    64bit_centos5.7_oracle10g安装手册

    ### 64bit CentOS 5.7 Oracle 10g 安装手册 #### 一、准备工作 在开始安装Oracle 10g之前,需要确保已经下载了正确的Oracle Database 10g Release 2 (10.2.0.1)安装包。此版本适用于64位CentOS 5.7操作系统。下载...

Global site tag (gtag.js) - Google Analytics