龚哥哥 - 山里男儿 爱生活、做自己!
CentOS下使用Ngin反向代理配置服务器集群
发表于 2015-8-31 | 服务器

目录结构

/data/soft/src       软件库
/data/soft/nginx    nginx安装目录
/data/www       项目根目录

1.安装PCRE库

ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/ 下载最新的 PCRE 源码包,使用下面命令下载编译和安装 PCRE 包:

cd /data/soft/src
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.tar.gz
tar -zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install

2.安装nginx

Nginx 一般有两个版本,分别是稳定版和开发版,您可以根据您的目的来选择这两个版本的其中一个,下面是把 Nginx 安装到 /data/soft/nginx 目录下的详细步骤:

cd /data/soft/src
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar -zxvf nginx-1.4.2.tar.gz
cd nginx-1.4.2
./configure --sbin-path=/data/soft/nginx/nginx \--conf-path=/data/soft/nginx/nginx.conf \--pid-path=/data/soft/nginx/nginx.pid \--with-http_ssl_module \--with-pcre=/data/soft/src/pcre-8.35 --with-http_gzip_static_module --with-http_stub_status_module
make && make install

其中参数 --with-http_stub_status_module 是为了启用 nginx 的 NginxStatus 功能,用来监控 Nginx 的当前状态。

cp /data/soft/nginx/nginx /etc/init.d/
/etc/init.d/nginx               启动nginx
/etc/init.d/nginx -s reload     重启nginx
/etc/init.d/nginx -s stop       关闭nginx

添加nginx开机启动
echo "/etc/init.d/nginx" >> /etc/rc.local

3.配置nginx.conf    文件位置 /data/soft/nginx/nginx.conf

user  www;# 工作进程的属主
worker_processes  4;# 工作进程数,一般与 CPU 核数等同

#error_log  logs/error.log; 
#error_log  logs/error.log  notice; 
#error_log  logs/error.log  info; 

#pid        logs/nginx.pid; 

events { 
    use epoll;#Linux 下性能最好的 event 模式
    worker_connections  2048;# 每个工作进程允许最大的同时连接数
} 

http
{
    include       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"'; 

    #access_log  off; 
    access_log  logs/access.log;# 日志文件名

    sendfile        on; 
    #tcp_nopush     on; 
    tcp_nodelay     on; 

    keepalive_timeout  65; 

    #include     gzip.conf; #gzip压缩配置

    # 集群中的所有后台服务器的配置信息
    upstream tomcats { 
        server 192.168.14.126:81 weight=10;
        server 192.168.14.126:82 weight=10;
        server 192.168.44.126:83 weight=10;
    }

    server { 
        listen       80;#HTTP 的端口
        server_name  localhost; 

        charset utf-8; 

        #access_log  logs/host.access.log  main; 

        location ~ ^/NginxStatus/ { 
            stub_status on; #Nginx 状态监控配置
            access_log off; 
        } 

        location ~ ^/(WEB-INF)/ { 
            deny all; 
        } 

        location ~ \.(htm|html|asp|php|gif|jpg|jpeg|png|bmp|ico|rar|css|js|zip|java|jar|txt|flv|swf|mid|doc|ppt|xls|pdf|txt|mp3|wma)$ { 
            root /data/www;
            expires 24h; 
        } 

        location / { 
            proxy_pass http://tomcats; # 反向代理
            proxy_redirect off;
            proxy_set_header  X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        } 

        error_page 404 /html/404.html; 

        error_page 502 503 /html/502.html; 
        error_page 500 504 /50x.html; 
        location = /50x.html{ 
            root   html; 
        }
    }

    #引入虚拟机
    include vhost/*.conf;
}

4.在vhost目录下添加虚拟机      文件名称 /data/soft/nginx/vhost/one.conf

server {
    listen              *:81; #对于虚拟机配置端口
    server_name         localhost;
    access_log          /data/log/one_access.log;
    error_log           /data/log/one_error.log warn;
    index               index.html index.htm index.php;
    root                /data/www/one; #对应/data/www目录下创建三个目录
    autoindex           off;
    error_page          404 http://$server_name;

    location ~ .*.(js|css)?$
    {
        expires 1h;
    }

    location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
    {
        expires 15d;
    }
}

创建文件 /data/www/one/index.html   内容:Th is One Node

按照此内容添加3个文件,listen和root对应修改

5.配置完成

重启nginx
访问 192.168.14.126 ,就可以看见不同效果了

发表评论:

TOP