一、配置前准备

安装相应的编译工具

yum -y install gcc glibc automake autoconf libtool make

安装pcre实现rewrite功能

yum -y install pcre*

如有需求ssl 安全协议,可以安装下这个模块

yum -y install openssl*

gzip 类库安装

yum -y install zlib zlib-devel

官网下载nginx编译安装包

wget http://www.nginx.org/download/nginx-1.9.14.tar.gz

二、编译安装nginx

首先检测下yum安装的组件,如:

rpm -qa | grep gcc
#其他类似

解压从官方下载的压缩包

tar -xvf nginx-1.9.14.tar.gz

用make进行编译安装

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre
# 用 . /来执行安装命令,--with参数选择相应需求的模块
#with-http_stub_status_module    支持nginx状态查询
#with-http_ssl_module    支持https
#with-pcre    支持rewrite重写功能
make
make install

三、建立网站目录及用户

新建wwwroot用户用于web管理

useradd wwwroot

建立www.test.com站点目录用于测试

mkdir -p /wwwroot/html/www.test.com/

更改站点目录属主和属组为wwwroot

chown -R wwwroot.wwwroot /wwwroot

四、修改nginx配置参数

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

user  wwwroot wwwroot;

添加测试虚拟站点目录用于测试

server
    {
        listen 80 ;
        server_name www.test.com test.com;
        index index.html index.htm index.php;
        root  /wwwroot/html/www.test.com;

        location ~ \.php$ {

                 fastcgi_pass   127.0.0.1:9000;
                 fastcgi_index  index.php;
                 fastcgi_param  SCRIPT_FILENAME  /$document_root$fastcgi_script_name;    
                 include        fastcgi_params;
                                     }
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }

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

        location ~ /\.
        {
            deny all;
        }
        error_log /wwwroot/wwwlogs/www.test.com.error.log;
        access_log  /wwwroot/wwwlogs/www.test.com.access.log;
    }

五、核查nginx服务

启动nginx服务并加入开机自启

/usr/local/nginx/sbin/nginx
echo '/usr/local/nginx/sbin/nginx' >> /etc/rc.local
参数说明

重读nginx配置文件

/usr/local/nginx/sbin/nginx -s reload

关闭nginx服务

/usr/local/nginx/sbin/nginx -s stop

启动服务器后核查访问

curl -s http://localhost | grep nginx.com
#<a href="http://nginx.com/">nginx.com</a>.</p>

六、扩展

nginx反向代理

server {
        listen       80;
        server_name ~^(www.)?(.+)$;
        location / {
                proxy_pass        http://114.215.185.1 ;
                #location条件满足将请求转发给代理服务器
                proxy_set_header        X-Real-IP  $remote_addr;
                proxy_set_header        Host $host;
                proxy_set_header        X-Forward-For $remote_addr;
                    }

        }

反向代理参数详解

#在代理向后端服务器发送的http请求头中加入host字段信息,用于当后端服务器配置有多个虚拟主机时,可以识别代理的是哪个虚拟主机。这是节点服务器多虚拟主机时的关键配置。
        proxy_set_headerHost  $host;
#在代理向后端服务器发送的http请求头中加入X-Forwarded-For字段信息,用于后端服务器程序、日志等接收记录真实用户的IP,而不是代理服务器的IP。
        proxy_set_header X-Forwarded-For $remote_addr;
#设定反向代理与后端节点服务器连接的超时时间,即发起握手等候响应的超时时间。
        proxy_connect_timeout60;
#设定代理后端服务器的数据回传时间
        proxy_send_timeout 60;
#设定Nginx从代理的后端服务器获取信息的时间
        proxy_read_timeout 60;
#设定缓冲区的大小
        proxy_buffer_size 4k;
#设定缓冲区的数量和大小。nginx从代理的后端服务器获取的响应信息,会放置到缓冲区。
        proxy_buffers 4 32k;
#设定系统很忙时可以使用的proxy_buffers大小
       proxy_busy_buffers_size 64k;
#设定proxy缓存临时文件的大小
       proxy_temp_file_write_size 64k;
#对于以上参数的详细理解可见本文开头图解。

负载均衡

upstream serverpool 
        {
            server 10.0.0.1:80 max_fails=3 fail_timeout=30 weight=1;
            server 10.0.0.2:80 max_fails=3 fail_timeout=30 weight=2;

            #max_fails=3  设置链接后端服务器3次失败则认为服务器无效状态
            #fail_timeout 设置链接后端服务器超时时间
            #weight       设置后端服务器负载权重
        }

负载均衡调用

server {
            proxy_pass          http://serverpool;
        }