---
title: "手把手教你认识并搭建Nginx"
author: "wosyingjun"
date: "2015-10-29T13:40:05Z"
tags: ["nginx"]
categories: ["企业架构"]
slug: "nginx"
draft: false
lastmod: "2016-04-29T11:00:42Z"
id: 2252941
url: "https://wosyingjun.iteye.com/blog/2252941"
---

## 手把手教你认识并搭建Nginx

> Nginx (“engine x”) 是一个高性能的 HTTP 和 反向代理 服务器，也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的，它已经在该站点运行超过两年半了。 Igor 将源代码以类 BSD 许可证的形式发布。尽管还是测试版，但是，Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

Nginx官方地址：[http://nginx.org/en/](http://nginx.org/en/)  
Nginx中文文档：[http://www.nginx.cn/doc/](http://www.nginx.cn/doc/)

#### 一. Nginx 的优点：

**作为 Web 服务器：**相比 Apache，Nginx 使用更少的资源，支持更多的并发连接，体现更高的效率，这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应。

**作为负载均衡服务器：**Nginx 既可以在内部直接支持 Rails 和 PHP，也可以支持作为 HTTP代理服务器 对外进行服务。Nginx 用 C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。

**作为邮件代理服务器:** Nginx 同时也是一个非常优秀的邮件代理服务器（最早开发这个产品的目的之一也是作为邮件代理服务器）。

Nginx 安装非常的简单，配置文件 非常简洁，Bugs非常少的服务器: Nginx 启动特别容易，并且几乎可以做到7\*24不间断运行，即使运行数个月也不需要重新启动。你还能够在 不间断服务的情况下进行软件版本的升级。

#### 二. 安装Nginx过程（在CentOS上搭建）：

1. 安装nginx依赖的插件，建议通过yum进行在线安装。  
   `yum -y install pcre-devel openssl openssl-devel gcc+c--`  
   Pcre-devel是支持读写（支持正则表达式的库）等插件  
   openssl是支持ssl证书插件
2. 安装libevent（由于nginx采用的是epoll机制需要事件库的支持）  
   `[root@localhost~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src/`  
   `[root@localhost~]# cd /usr/local/src/libevent-2.0.16-stable/`  
   `[root@localhost libevent-2.0.16-stable]# ./configure --prefix=/usr/local/libevent`  
   `[root@localhost libevent-2.0.16-stable]# make && make install`  
   `[root@localhost libevent-2.0.16-stable]# cd /usr/local/libevent/`  
   为了系统能其他的程序能够调用libevent的头文件和库文件我们需要进行下面的操作：  
   `[root@localhost libevent]# ln -s /usr/local/libevent/include /usr/include/libevent`  
   `[root@localhost libevent]# vim /etc/ld.so.conf.d/libevent.conf`  
   加入文件内容如下：  
   ![](http://i.imgur.com/zsvRs1W.png)  
   root@localhost libevent]# ldconfig -pv |grep  libevent  
   ![](http://i.imgur.com/q55OsdN.png)
3. 安装nginx过程  
   解压并配置nginx包  
   `#tar –zxvf nginx-1.6.2.tar.gz`  
   生成新的目录nginx-1.6.2，进入该目录下，配置安装nginx需要的相关配置信息  
   `#./configure --user=root --group=root --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-cc-opt='-O2' --with-cpu-opt=opteron`  
   编译  
   `#make`  
   安装nginx  
   `#make install`  
   检查nginx配置是否正确  
   `#/usr/local/nginx/sbin/nginx -t`  
   ![](http://i.imgur.com/O1s7HD9.png)  
   启动nginx  
   `#/usr/local/nginx/sbin/nginx`  
   查看nginx启动状态  
   `#ps –ef|grep nginx`
4. 到这里nginx的安装已经完成了，接下来添加nginx的代理服务
   
   ```#可执行用户
    user  root;
    #工作进程，一般根据CPU核数来定
    worker_processes  2;
   
     server {
        listen      9457;
        server_name  localhost;
   
        location / {
            root   html;
            index  index.html index.htm;
        }
        #这里配上项目地址的分发
        location /YourProject1/ {
            proxy_pass    http://127.0.0.1:8080/YourProject1/;
            proxy_set_header    Host        $host;
            proxy_set_header    X-Forwarded-For        $remote_addr;        
       }
        location /YourProject2/ {
            proxy_pass    http://127.0.0.1:8090/YourProject2/;
            proxy_set_header    Host        $host;
            proxy_set_header    X-Forwarded-For        $remote_addr;        
       }
    }
   ```
5. 重启nginx  
   `Killall ngnix`  
   `#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf`

       参数-c指定了配置文件的目录 如果不加-c参数 则加载默认的配置文件。

到这里Nginx的简单的安装和配置就完成，当然Nginx还有很多功能，后期可以在这之上不断完善。  
附件里是安装Nginx过程中用到的包。