Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。
Nginx相对于Apache优点:
高并发响应性能非常好,官方Nginx处理静态文件并发5w/s
反向代理性能非常好。(可用于负载均衡)
内存和cpu占用率低。(为Apache的1/5-1/10)
功能较Apache少(常用功能均有)
对php可使用cgi方式和fastcgi方式。
实验机器:192.168.77.133
首先需要安装pcre库,然后再安装Nginx:
安装pcre支持rewrite库,也可以安装源码,注*安装源码时,指定pcre路径为解压
源码的路径,而不是编译后的路径,否则会报错
(make[1]: *** [/usr/local/pcre/Makefile] Error 127 错误)
yum install pcre-devel pcre -y
#下载Nginx源码包
#进入解压目录,然后sed修改Nginx版本信息为WS(防止版本信息被恶意者查到)
cd nginx-1.6.3 ; sed -i -e 's/1.6.3//g' -e 's/nginx\//WS/g' -e
's/"NGINX"/"WS"/g' src/core/nginx.h
#预编译Nginx
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#.configure预编译成功后,执行make命令进行编译
make
#make执行成功后,执行make install 正式安装
make install
#自此Nginx安装完毕
/usr/local/nginx/sbin/nginx -t 检查nginx配置文件是否正确,返回OK即正确。
[root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
#启动nginx
root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx
[root@192_168_77_133 ~]# ps -ef | grep nginx
root 21715 1 0 14:01 ? 00:00:00 nginx: master process /usr/local/nginx/sbin/nginx
nobody 21716 21715 0 14:01 ? 00:00:00 nginx: worker process
root 21718 20717 0 14:01 pts/2 00:00:00 grep nginx
/usr/local/nginx/sbin/nginx -s stop 停止服务
[root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx -v 查看版本
nginx version: WS
[root@192_168_77_133 ~]# /usr/local/nginx/sbin/nginx -V 查看编译参数
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
然后就可一在客户端访问可以查看到测试页面如下:
<a href="http://s3.51cto.com/wyfs02/M01/71/06/wKiom1XDdeGhOktnAABl_3LYx48297.jpg" target="_blank"></a>
#nginx虚拟主机配置
在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器同时会配置N个虚拟域名主机,即多个域名对于同样一个80端口。然后服务器IP数量很多,也可以配置基于多个IP对应同一个端口。
vi修改nginx.conf server段配置内容如下:
server {
listen 88; #端口
location / {
root html/a; #虚拟目录
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
server {
listen 88; #端口
root html/b; #虚拟目录
创建两个不同的目录mkdir –p /usr/local/nginx/html/{a,b},然后分别在两个目录创建两个不 同的index.html网站页面即可。通过客户端配置hosts指向两个域名,然后在IE浏览器访问测试效果。
虚拟主机也可以单独创建个虚拟文件,然后在nginx.conf中引用即可,引用命令include+虚拟文件
本文转自 Anonymous123 51CTO博客,原文链接:http://blog.51cto.com/woshitieren/1684276