Linux课程笔记 Nginx介绍

1  Nginx介绍

与Apache相比,在性能上,nginx占用更少的系统资源,特定的场景应用(静态数据)能支持更多的并发连接,达到更高的访问效率;在功能上,Nginx是一个优秀的反向代理和负载均衡服务器,也可以作为缓存服务器。

 

2  Nginx有点总结:

  1. 高并发:能支持1-2万甚至更多的并发连接(静态小文件环境下)
  2. 内存消耗少:在3万并发连接下,开启的10个Nginx进程消耗不到200M内存
  3. 可以做HTTP反向代理,即负载均衡功能,相当于专业的haproxy软件或lvs的功能
  4. 内置对RS服务器健康检查功能:如果Nginx代理后端某台Web服务器宕机,不会影响前端的访问,这个功能还比较弱,需要后需改进。
  5. 通过cache插件可以实现类似squid等专业的缓存软件实现的功能

     Nginx最主要的优点是:支持kqueue(FreeBSD 4.1+),epoll(Linux 2.6+)等网络IO事件模型,由此来支持高并发的连接。Apache支持的是select模型。

 

 

3 Nginx的主要应用场景

Web服务器

1) 结合FastCGI运行PHP、JSP等程序

2) 运行HTML,JS,CSS,小图片等静态数据

 

代理服务器

1) 做反向代理或负载均衡、规则过滤

 

缓存服务器

2)   加cache插件实现对web服务器缓存功能

 

4 主流服务器产品对比

apache

(1) 2.2版本非常强大,官方说明,2.4版本性能超强

(2) Perfork模式取消了进程创建开销,性能很高

(3) 处理动态业务数据时,因关联到后端的引擎和数据库,瓶颈不在于Apache本身

(4) 高并发消耗系统资源相对多一些

 

Nginx

(1) 基于异步IO模型,性能强,能够支持上万并发

(2) 对小文件支持很好,性能很高(限静态小文件)

(3) 代码优美。扩展必须编进进主进程

(4) 消耗系统资源比较低

 

 

5 为什么Nginx总体性能比Apache高

 

 

6  Nginx基础安装

6.1  pcre安装

Pcre,中文兼容正则表达式,安装pcre库是为了使Nginx支持HTTP  Rewrite模块

wget http://exim.mirror.fr/pcre/pcre-8.30.tar.gz

tar  zxf  pcre-8.30.tar.gz

cd  pcre-8.30

./configure

make && make install

 

注意:在安装的过程中遇到了“libtool: compile: unrecognized option `-DHAVE_CONFIG_H'”。这是由于没有安装gcc-c++. yum install gcc-c++即可解决。

 

6.2  Nginx安装

wget  http://nginx.org/download/nginx-1.2.9.tar.gz

 

useradd  nginx  -s  /sbin/nologin  -M  #创建nginx这个用户,可随意起名

 

tar zxf  nginx-1.2.9.tar.gz

 

cd  nginx-1.2.9

 

./configure --user=nginx --group=nginx --prefix=/application/nginx-1.2.9 --with-http_stub_status_module --with-http_ssl_module

 

make && make install

 

ln  -s  /application/nginx-1.2.9  /application/nginx  #在版本升级的时候很重要

 

./configure 后参数说明: (更多可使用./configure  --help查询)

--user=USER                      set non-privileged user for

--group=GROUP                   set non-privileged group for

--with-http_stub_status_module     enable ngx_http_stub_status_module

--with-http_ssl_module             enable ngx_http_ssl_module

 

6.3  Nginx的启动

[root@test2 nginx-1.2.9]# /application/nginx/sbin/nginx

/application/nginx/sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory

#这是由于libpcre.so.1文件无法找到,如果是yum安装,不会出现这个错误

 

[root@test2 nginx-1.2.9]# find / -type f -name "libpcre.so.*"

/lib64/libpcre.so.0.0.1

/usr/local/lib/libpcre.so.1.0.0

/root/tools/pcre-8.30/.libs/libpcre.so.1.0.0

 

[root@test2 nginx-1.2.9]# vi /etc/ld.so.conf

include ld.so.conf.d/*.conf

/usr/local/lib/

[root@test2 nginx-1.2.9]# ldconfig  #使配置文件生效

[root@test2 nginx-1.2.9]# /application/nginx/sbin/nginx -t

nginx: the configuration file /application/nginx-1.2.9/conf/nginx.conf syntax is ok

nginx: configuration file /application/nginx-1.2.9/conf/nginx.conf test is successful

 

验证启动:

[root@test2 nginx-1.2.9]# lsof -i:80

COMMAND   PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME

nginx   16853  root    6u  IPv4  34743      0t0  TCP *:http (LISTEN)

nginx   16854  nginx    6u  IPv4  34743      0t0  TCP *:http (LISTEN)

 

疑难问题:

[root@test2 nginx-1.2.9]# /application/nginx/sbin/nginx

nginx: [emerg] getpwnam("nginx") failed

#这是由于没有创建用户导致的,重现这个问题只需要执userdel  nginx

 

 

6.4  Nginx配置文件

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost;

 

        location / {

            root   html;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

 

        location = /50x.html {

            root   html;

        }

    }

}

 

posted @ 2017-05-08 00:38  fengze  阅读(142)  评论(0编辑  收藏  举报