当前位置: > CentOS > CentOS服务器 > 环境配置 >

CentOS下搭建nginx反向代理配置测试

时间:2015-11-21 19:01来源:linux.it.net.cn 作者:IT
系统centos6.2

首先装好contos ,此处略。

 

一、安装gcc

   yum install gcc-c++

 

二、安装所需要的依赖库

   yum -y install zlib-devel openssl--devel pcre-devel

 

 

三、如有安装老版本,则卸载。

   查看:find -name nginx

   卸载:yum remove nginx


四、下载nginx源码并解压.(源码去官网下载, 以下是官方链接)

    wget -c http://nginx.org/download/nginx-1.6.2.tar.gz

    tar -zxvf nginx-1.6.2.tar.gz

    mv nginx-1.6.2 nginx

    cd nginx

    ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx

    --with-http_addition_module

    --with-http_flv_module --with-http_gzip_static_module

    --with-http_realip_module --with-http_ssl_module

    --with-http_stub_status_module --with-http_sub_module

    --with-http_dav_module

 

    make

    make install


五、建立nginx用户及用户组

      groupadd -r nginx
 
    useradd  -s /sbin/nologin -g nginx -r nginx
  
cd 到nginx目录,看能否启动
sbin/nginx           #如果没有报错,说明ok
 
 
六、配置nginx,编辑配置文件nginx.conf

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

 

   将第一行user前的#去掉,改为:

   user nginx nginx;

   设置nginx服务的worker子进程,比如设为2:

   worker_processes 2;

   将以下两行前的#去掉:

   error_log logs/error.log

   pid logs/nginx.pid

 

   把下面的#去掉,这是日志配置:

   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 logs/access.log main;

 

 

 upstream proxy_test {

   server 192.168.4.69:80;     #如果你要测试,把这里换成你自己要代理后端的ip

   server 192.168.4.68:80;

 }

 

这是server段的配置

 

server {

    listen       80;

    server_name  www.test.com;    #要访问的域名,我这里用的测试域名

 

    charset utf8;

 

    location / {

        proxy_pass       http://proxy_test;

        proxy_set_header Host      $host;

        proxy_set_header X-Real-IP $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

}


保存退出!

nginx平滑重启:nginx -s reload   #加载刚刚加入的配置。

 

七、测试:

     后端服务器开启,在192.168.4.69和68的网页文件位置添加测试文件test.html, 内容: this is test 69,     this is test 68,这样方便查看访问到哪台了。

      在本地配置好host,用www.test.com去访问后端服务器的文件,

如: www.test.com/test.html,

    浏览器打开显示有this...说明配置ok了。

 

(责任编辑:IT)
------分隔线----------------------------