nginx配置笔记

nginx [engine x]是Igor Sysoev编写的一个HTTP和反向代理服务器,另外它也可以作为邮件代理服务器。 它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括Yandex、Mail.Ru、VKontakte,以及Rambler。

据Netcraft统计,在2012年8月份,世界上最繁忙的网站中有11.48%使用Nginx作为其服务器或者代理服务器。目前互联网主流公司360、百度、新浪、腾讯、阿里等都在使用nginx作为自己的web服务器。

Nginx由内核和模块组成,其中,内核的设计非常微小和简洁,完成的工作也非常简单,仅仅通过查找配置文件将客户端请求映射到一个location block(location是Nginx配置中的一个指令,用于URL匹配),而在这个location中所配置的每个指令将会启动不同的模块去完成相应的工作。

Nginx相对于Apache优点:

高并发响应性能非常好,官方Nginx处理静态文件并发5w/s


反向代理性能非常好。(可用于负载均衡)


内存和cpu占用率低。(为Apache的1/5-1/10)


功能较Apache少(常用功能均有)


对php可使用cgi方式和fastcgi方式。


Nginx WEB安装


实验机器:192.168.77.133


首先需要安装pcre库,然后再安装Nginx:


安装pcre支持rewrite库,也可以安装源码,注*安装源码时,指定pcre路径为解压

源码的路径,而不是编译后的路径,否则会报错

(make[1]: *** [/usr/local/pcre/Makefile] Error 127 错误)

yum install pcre-devel pcre -y

#下载Nginx源码包

root@192_168_77_133 ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz

#进入解压目录,然后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 查看编译参数

nginx version: WS

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

然后就可一在客户端访问可以查看到测试页面如下:

wKiom1XDdeGhOktnAABl_3LYx48297.jpg


#nginx虚拟主机配置


在真实的服务器环境,为了充分利用服务器资源,一台nginx web服务器同时会配置N个虚拟域名主机,即多个域名对于同样一个80端口。然后服务器IP数量很多,也可以配置基于多个IP对应同一个端口。

vi修改nginx.conf server段配置内容如下:

server {

        listen       88;   #端口

        server_name  www.a.com;     #主机名 

        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;      #端口

        server_name  www.b.com;   #主机名 

        location / {

            root   html/b;                    #虚拟目录

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

创建两个不同的目录mkdir �Cp /usr/local/nginx/html/{a,b},然后分别在两个目录创建两个不 同的index.html网站页面即可。通过客户端配置hosts指向两个域名,然后在IE浏览器访问测试效果。

 虚拟主机也可以单独创建个虚拟文件,然后在nginx.conf中引用即可,引用命令include+虚拟文件


你可能感兴趣的:(nginx)