您的位置:首页 > 运维架构 > Nginx

手把手教你认识并搭建Nginx

2016-07-13 15:33 459 查看

[架构设计]手把手教你认识并搭建Nginx

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

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

一. Nginx 的优点:

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

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

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

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

二. 安装Nginx过程(在CentOS上搭建): 安装nginx依赖的插件,建议通过yum进行在线安装。

yum -y install pcre-devel openssl openssl-devel gcc+c-- Pcre-devel是支持读写(支持正则表达式的库)等插件 openssl是支持ssl证书插件 安装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
加入文件内容如下: 


 root@localhost libevent]# ldconfig 


安装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 


 启动nginx #/usr/local/nginx/sbin/nginx 查看nginx启动状态 #ps –ef|grep nginx

到这里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;
}
}


重启nginx Killall ngnix #/usr/local/nginx/sbin/nginx



转载自:http://www.3fwork.com/b300/000751MYM030286/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: