nginx 学习

Nginx简介:

Nginx(Engine x),俄罗斯人开发的,开源的www服务软件
软件一共780k。
Nginx本身是一款静态(html,js,css,jpg等)www软件。
特点:静态小文件高并发,同时占用的很少。3w并发10个线程只消耗150MB内存。
Nginx使用平台:unix,linux,Windows都可以;

Nginx功能:

 www  web服务
 负载均衡(反向代理proxy)
 Web  cache (web缓存)

Nginx的优点:

1.配置简单,更灵活
2.高并发(静态小文件),静态1-2w
3.占用资源少,2w并发,开10个线程服务,内存消耗几百M
4.功能种类比较多(web,cache,proxy),每一个功能都不是特别强。
5.支持epoll模型。使用nginx可以支持高并发。
6.Nginx配合动态服务和apache区别
7.利用nginx可以对ip限速,可以限制连接数。

当客户端去访问网站的时候,先去请求nginx服务器,nginx服务器会分辨静态网站与动态网站,如果是静态网站,nginx会自行处理,如果是动态网站,nginx会把这个请求任务交给php服务器处理;

Nginx的应用场合:

1.静态服务器(图片,视频服务),html,js,css,.flv等。并发:1-3w
2.动态服务,nginx是利用fastcgi的方式运行php,jsp     并发:500-1500
3.反向代理,负载均衡。日PV2000W以下,都可以直接用nginx做代理。(haproxy,F5,A10都可以做反向代理)
4.缓存服务,squid,varnish 

Nginx和其他web服务器的对比

1)apache
  2.2版本非常稳定强大,据官方说,其2.4版本性能超强
  Profork模式取消了进程创建开销,性能很高。
  处理动态业务数据时,因关联到后端的引擎和数据库,瓶颈不在于apache本身
  高并发时消耗系统资源相对比较多一些
  基于传统的select模型
  扩展库,DSO方法,apxs
2)nginx
  基于异步IO模型,性能强,能够支持上万并发
  对小文件支持很好,性能很高(限静态小文件1M)
  代码优美,扩展库必须编译进主程序
  消耗系统资源比较低
3)Lighttpd(百度贴吧,豆瓣)
  基于异步io模型,性能和nginx相近
  扩展库是SO模式,比nginx要灵活
  全球使用率比较低,安全性没有上面两个好
  通过插件(mod_secdownload)可实现文件URL地址加密
  

如何去选择web服务器:

1)静态业务:高并发,采用nginx或Lighttpd,根据自己的掌握程度或公司的要求
2)动态业务:采用nginx和apache均可
3)既有静态业务又有动态业务:nginx与apache,不要多选要单选
动态业务可以由前端代理(haproxy),根据页面元素的类型, 转发相应的服务器进行处理
   如果并发不是很大,又对apache很熟悉,采用apache也是可以的,apache2.4版本也很强大,并发连接数也有所增加,见后面的压力测试。
提示:nginx做web(apache,Lighttpd),反向代理(haproxy,lvs nat)及缓存服务器(squid)也是非常不错的

Nginx虚拟主机 server { }

1.基于域名 >>应用:外部网站
2.基于端口 >>应用:公司内部网站,外部网站的后台
基于ip(不完善) >>几乎不用。不支持ifconfig别名,配置文件可以

http://nginx.org/en/download.html 官方下载

1.安装准备

Pcre全程(Perl compatible regular expressions),中文perl兼容正则表达式,官方站点为www.pcre.org,安装pcre库是为了使nginx支持http rewrite模块。
[root@localhost ~]# yum install -y pcre pcre-devel

Wget  http://ftp.exim.llorien.org/pcre/pcre-8.30.tar 
[root@localhost ~]# tar zxf pcre-8.30.tar.gz 
[root@localhost ~]# cd pcre-8.30
[root@localhost pcre-8.30]# ./config
[root@localhost pcre-8.30]# ./configure
[root@localhost pcre-8.30]# make && make install

安装openssl
[root@localhost ~]# yum install -y openssl openssl-devel

2.安装nginx

[root@localhost]# useradd nginx -s /sbin/nologin -M
[root@localhost ~]# tar zxf nginx-1.6.0.tar.gz 
[root@localhost ~]# cd nginx-1.6.0
[root@localhost nginx-1.6.0]# ./configure --user=nginx --group=nginx --prefix=/application/nginx1.6.2/ --with-http_stub_status_module --with-http_ssl_module

检查是否编译成功
[root@localhost nginx-1.6.0]# echo $?
0      //返回0正常其他全错

[root@localhost nginx-1.6.0]# make && make install

[root@localhost ~]# ln -s /application/nginx1.6.2/ /application/nginx   //软连接去除目录

3.启动nginx服务

[root@localhost ~]# /application/nginx/sbin/nginx –t //检查语法
[root@localhost ~]# /application/nginx/sbin/nginx //启动
[root@localhost ~]# netstat -anpt | grep nginx

[root@localhost ~]# lsof -i :80 端口反查

访问:http://ip

Ok
关闭防火墙的命令,如果有外网ip生产环境请允许80端口的访问
[root@localhost ~]# iptables -I INPUT -p tcp --dport 80 -j ACCEPT

Nginx基本配置;

 Nginx使用不同的模块来实现不同的功能,主要2组重要的模块:
 1)nginx core  modules (必需的)
 包括: Main   Events
 2)standard HTTP modules (虽然不是必需,但是缺省都会安装,不建议改动)
 经典包括: core  Access  FastCGI  Gzip(压缩模块,性能优化)   Log(日志模块)
 Proxy  rewrite(URL重写模块)   upstream(负责均衡模块)
 通过官方http://nginx.org/en/docs/ 查看模块的使用帮助
 

5.Nginx的目录结构说明

        ├── client_body_temp              //客户端内容的临时文件
    ├── conf                          //这是nginx的所有的配置文件的目录
    │   ├── fastcgi.conf               //动态配置文件
    │   ├── fastcgi.conf.default
    │   ├── fastcgi_params             //fastcgi参数文件
    │   ├── fastcgi_params.default
    │   ├── koi-utf
    │   ├── koi-win
    │   ├── mime.types
    │   ├── mime.types.default
    │   ├── nginx.conf                  //主配置文件
    │   ├── nginx.conf.default            //默认主配置文件
    │   ├── scgi_params
    │   ├── scgi_params.default
    │   ├── uwsgi_params
    │   ├── uwsgi_params.default
    │   └── win-utf
    ├── fastcgi_temp
    ├── html
    │   ├── 50x.html
    │   └── index.html               //默认的站点目录
    ├── logs                       //日志目录
    │   ├── access.log              //访问日志
    │   ├── error.log                //错误日志
    │   └── nginx.pid                //pid进程文件
    ├── proxy_temp
    ├── sbin
    │   └── nginx                  //启动文件
    ├── scgi_temp
    └── uwsgi_temp
    
    Nginx主配置文件
    [root@localhost conf]# egrep -v "#|^$" nginx.conf   //把配置文件注释过滤掉
    
    user nobody;                     //程序的默认用户
    worker_processes  1;             //写cpu的核数
    events {
        worker_connections  1024;     //worker的连接数
    }
    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;
            }
        }
    }
    
    配置虚拟主机:
    为了更清晰一些把注释空行去掉
    [root@localhost conf]# egrep -v "#|^$" nginx.conf >a.log
    http {
      7     include       mime.types;
      8     default_type  application/octet-stream;
      9     sendfile        on;
     10     keepalive_timeout  65;
     11     server {
     12         listen       80;
     13         server_name  www.etiantian.org;
     14             root   html/www;
     15             index  index.html index.htm;
     16     }
     17     server {
     18         listen       80;
     19         server_name  bbs.etiantian.org;
     20             root   html/bbs;
     21             index  index.html index.htm;
     22     }
     23     server {
     24         listen       80;
     25         server_name  blog.etiantian.org;
     26             root   html/blog;
     27             index  index.html index.htm;
    
    创建测试站点
    [root@localhost conf]# cd ../html/
    [root@localhost html]# mkdir www
    [root@localhost html]# mkdir blog
    [root@localhost html]# mkdir bbs
    [root@localhost html]# clear
    [root@localhost html]# echo "www.etiantian.org" > www/index.html
    [root@localhost html]# echo "bbs.etiantian.org" > bbs/index.html
    [root@localhost html]# echo "blog.etiantian.org" > blog/index.html
    
    检查语法
    [root@localhost html]# nginx -t
    nginx: the configuration file /application/nginx1.6.2//conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx1.6.2//conf/nginx.conf test is successful
    [root@localhost ~]# killall nginx
    [root@localhost ~]# nginx

猜你喜欢

转载自www.cnblogs.com/panax/p/8961669.html