第1章 web网站服务介绍

1.1 常用来提供静态Web服务的软件

Apache:这是中小型Web服务的主流,Web服务器中的老大哥。

Nginx:大型网站Web服务的主流,曾经Web服务器中的初生牛犊,现已长大。

           Nginx的分支Tenginehttp://tengine.taobao.org/)目前也在飞速发展。

Lighttpd:这是一个不温不火的优秀Web软件,社区不活跃,静态解析效率很高。在Nginx流行前,

              它是大并发静态业务的首选,国内百度贴吧、豆瓣等众多网站都有Lighttpd奋斗的身影。

1.2 常用来提供动态服务的软件

PHPFastCGI:大中小型网站都会使用,动态网页语言PHP程序的解析容器。它可配合Apache解析动态程序,不过,这里的PHP不是FastCGI守护进程模式,而是mod_php5.somodule)。也可配合Nginx解析动态程序,此时的PHP常用FastCGI守护进程模式提供服务。(.php)

Tomcat:中小企业动态Web服务主流,互联网Java容器主流(如jspdo)。

Resin:大型动态Web服务主流,互联网Java容器主流(如jspdo)。

IISInternet information services:微软windows下的Web服务软件(如aspaspx

第2章 nginx网站服务软件

2.1 Nginx是什么

如果你听说或使用过Apache软件,那么很快就会熟悉Nginx软件,与Apache软件类似, Nginxengine x)是一个开源的,支持高性能、高并发的WWW服务器和代理服务软件

Nginx因具有高并发(特别是静态资源)、占用系统资源少等特性,且功能丰富逐渐流行起来。

在功能应用方面,Nginx不但是一个优秀的web服务软件,还具有反向代理负载均衡功能和缓存服务功能。在反向代理负载均衡功能方面,它类似与大名鼎鼎的LVS负载均衡及Haproxy等专业的缓存服务软件。

Nginx可以运行在UNIXLinuxBSDMac OS XSolarls,以及Microsoft Windows等操作系统中。随着Nginx在国内很多大型网站中的稳定高效运行,近两年它也逐渐被越来越多的中小型网站所使用。当前流行的Nginx Web组合被称为LNMPLEMP(即Linux Nginx MySql PHP),其中LEMP里的E取自Nginx(“engine x”)

Nginx的官网:http://nginx.org/

2.2 Nginx的特色及优点

2.2.1 Nginx的重要特性

可针对静态资源高速高并发访问及缓存

可使用反向代理加速,并且可进行数据缓存

具有简单负载均衡、节点健康检查和容错功能

支持远程FastCGI服务的缓存加速

支持FastCGIUwsgiSCGIMemcached Servers的加速和缓存

支持SSLTLSSNI

具有模块化的架构:过滤器包括gzip压缩、ranges支持、chunked响应、XSLTSSI及图像缩放等功能。在SSI过滤器中,一个包含多个SSI的页面,如果经由FastCGI或反向代理处理,可被并行处理。

2.2.2 Nginx HTTP服务器的特色及优点(重要)

支持高并发:能支持几万并发连接(特别是静态小文件业务环境)

资源消耗少:在3万并发连接下,开启10Nginx线程消耗的内存不到200MBab webbenh

可以做HTTP反向代理及加速缓存、即负载均衡功能,内置对RS节点服务器健康检查功能,这相当于专业的Haproxy软件或LVS的功能。

具备Squid等专业缓存软件等的缓存功能。

支持异步网络I/O事件模型epollLinux 2.6+)(apache select

2.3 Nginx软件的主要企业功能应用

2.3.1 作为Web服务软件

Nginx是一个支持高性能、高并发的Web服务软件,它具有很多优秀的特性,作为Web服务器,与Apache相比,Nginx能够支持更多的并发连接访问,但占用的资源却更少,效率更高,在功能上也强大了很多,几乎不逊于Apache

2.3.2 反向代理或负载均衡服务

在反向代理或负载均衡服务方面,Nginx可以作为Web服务,PHP等动态服务及Memcached缓存的代理服务器,它具有类似专业代理软件(如Haproxy)的功能,同时也是一个优秀的邮件代理服务软件(最早开发这个产品的目的之一就是作为邮件代理服务)。Nginx的代理功能在逐渐增强。

2.3.3 前端业务数据缓存服务

web缓存服务方面,Nginx可通过自身的proxy_cache模块实现类Squid等专业缓存软件的功能。

Nginx的这三大功能(web服务、反向代理或负载均衡服务、前端业务数据缓存服务)是国内使用Nginx的主要场景,特别是前两个。

2.4 为什么Nginx总体性能比Apache

Nginx使用最新的epollLinux 2.6内核)和Kqueuefreebsd)异步网络I/O模型,

Apache使用的是传统的select模型,处理大量连接的读写时,Apache所采用的select网络I/O模型比较低效。

目前Linux先能够承受高并发访问的SquidMemcached软件采用的都是epoll模型。

Apache select 和Nginx epoll的技术对比

指标

select

epoll

性能

随着连接数的增加性能急剧下降。处理成千上万并发连接数,性能很差

随着连接数的增加,性能基本上没有下降。处理成千上万并发连接时,性能很好。

连接数

连接数有限制,处理的最大连接数不超过1024,如果要处理的连接数超过1024个,则需要修改FD_SETSIZE宏,并重新编译

连接数无限制

内在处理机制

线性轮询

回调callback

开发复杂性

第3章 部署nginx软件(编译安装)

3.1 第一个里程碑:进行nginx软件下载

cd /server/tools

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

[root@web02 ~]# cd /server/tools/

[root@web02 tools]# wget http://nginx.org/download/nginx-1.12.2.tar.gz

--2018-02-02 18:26:50--  http://nginx.org/download/nginx-1.12.2.tar.gz

Resolving nginx.org... 206.251.255.63, 95.211.80.227, 2001:1af8:4060:a004:21::e3, ...

Connecting to nginx.org|206.251.255.63|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 981687 (959K) [application/octet-stream]

Saving to: nginx-1.12.2.tar.gz

 

100%[===================================================>] 981,687     23.8K/s   in 43s     

 

2018-02-02 18:27:40 (22.1 KB/s) - nginx-1.12.2.tar.gzsaved [981687/981687]

3.2 第二个里程碑:安装依赖软件

yum install -y pcre-devel openssl-devel

[root@web02 tools]# yum install -y pcre-devel openssl-devel

说明,如果安装系统没有安装兼容程序库、开发工具的,请安装一下基础依赖包

yum install gcc gcc-c++ automake autoconf -y

3.3 第三个里程碑:解压软件,创建虚拟用户,进行编译安装(三步曲)

3.3.1 解压软件,创建虚拟用户

tar xf nginx-1.12.2.tar.gz

useradd -M -s /sbin/nologin www

[root@web02 tools]# tar xf nginx-1.12.2.tar.gz 

[root@web02 tools]# ls

nginx-1.12.2  nginx-1.12.2.tar.gz

[root@web02 tools]# useradd -M -s /sbin/nologin www

[root@web02 tools]# id www

uid=502(www) gid=502(www) groups=502(www)

3.3.2 编译第一步:进行配置

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

配置参数说明

--prefix=PATH

 set installation prefix

指定软件程序安装的路径信息

--user=USER

set non-privileged user for

worker processes

创建一个虚拟用户,用于管理nginx服务的worker进程

--group=GROUP

set non-privileged group for

worker processes

 创建一个虚拟用户组,用于管理nginx服务的worker进程

--with-http_ssl_module

enable ngx_http_ssl_module

nginx服务可以支持https访问

--with-http_stub_status_module

enable ngx_http_stub_status_module

便于监控软件监视nginx服务运行状态

 

[root@web02 tools]# cd nginx-1.12.2

[root@web02 nginx-1.12.2]# pwd

/server/tools/nginx-1.12.2

[root@web02 nginx-1.12.2]# ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module


3.3.3 编译第二步:编译(翻译)

说明:编译过程实质是将各种程序语言转换为系统可以识别的二进制信息

make

[root@web02 nginx-1.12.2]# make

make -f objs/Makefile

make[1]: Entering directory `/server/tools/nginx-1.12.2'

……

 

3.3.4 编译第三步:编译安装

make install

[root@web02 nginx-1.12.2]# make install

make -f objs/Makefile install

make[1]: Entering directory `/server/tools/nginx-1.12.2'

……

3.4 第四个里程碑:创建程序目录软链接文件

ln -s /application/nginx-1.12.2/ /application/nginx

[root@web02 nginx-1.12.2]# ln -s /application/nginx-1.12.2/ /application/nginx

[root@web02 nginx-1.12.2]# cd /application/

[root@web02 application]# ll

total 4

lrwxrwxrwx 1 root root   26 Feb  2 19:30 nginx -> /application/nginx-1.12.2/

drwxr-xr-x 6 root root 4096 Feb  2 19:28 nginx-1.12.2

3.5 第五个里程碑:网站服务启动成功

/application/nginx/sbin/nginx

[root@web02 application]# /application/nginx/sbin/nginx          

3.6 第六个里程碑:检查测试

[root@web02 application]# ps -ef |grep nginx

root       4781      1  0 19:32 ?        00:00:00 nginx: master process /application/nginx/sbin/nginx

www        4782   4781  0 19:32 ?        00:00:00 nginx: worker process        

root       4784   2022  1 19:32 pts/0    00:00:00 grep nginx

[root@web02 application]# netstat -lntup |grep nginx

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                 LISTEN      4781/nginx          

 

说明:master进程表示nginx主进程,负责nginx服务的启动 停止等操作

      worker进程表示真正处理用户请求的进程

第4章 部署nginx软件可能会遇到的问题  

1. 依赖包软件没有安装

2. 启动Nginx时如下报错“nginx:[emerg]getpwnam(nginx)failed

 解答:这是因为没有对应的Nginx服务用户,执行useradd nginx -s /sbin/nologin -M创建Nginx用户即可。

3. 如何查看Nginx编译时的参数?

 解答:可采用如下命令查看:/application/nginx/sbin/nginx -V

 

[root@web02 application]# /application/nginx/sbin/nginx -V

nginx version: nginx/1.12.2

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC)

built with OpenSSL 1.0.1e-fips 11 Feb 2013

TLS SNI support enabled

configure arguments: --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module          

第5章 nginx软件程序目录结构

5.1 nginx软件程序目录结构

[root@web02 nginx]# ll

total 36

drwx------ 2 www  root 4096 Feb  2 19:32 client_body_temp

drwxr-xr-x 2 root root 4096 Feb  2 19:28 conf                                  ---配置文件目录

drwx------ 2 www  root 4096 Feb  2 19:32 fastcgi_temp

drwxr-xr-x 2 root root 4096 Feb  2 19:28 html                                 ---站点目录

drwxr-xr-x 2 root root 4096 Feb  2 19:32 logs                                 ---日志目录

drwx------ 2 www  root 4096 Feb  2 19:32 proxy_temp

drwxr-xr-x 2 root root 4096 Feb  2 19:28 sbin                                 ---保存服务命令目录

drwx------ 2 www  root 4096 Feb  2 19:32 scgi_temp

drwx------ 2 www  root 4096 Feb  2 19:32 uwsgi_temp       

 

5.2 配置文件目录说明

image.png 

mime.types        --- 媒体资源类型文件

nginx.conf        --- nginx服务主配置文件


 image.png

5.3 精简nginx配置文件信息

grep -Ev "#|^$" nginx.conf.default >nginx.conf

[root@web02 conf]# ll

total 60

-rw-r--r-- 1 root root 1077 Feb  2 19:28 fastcgi.conf

-rw-r--r-- 1 root root 1077 Feb  2 19:28 fastcgi.conf.default

-rw-r--r-- 1 root root 1007 Feb  2 19:28 fastcgi_params

-rw-r--r-- 1 root root 1007 Feb  2 19:28 fastcgi_params.default

-rw-r--r-- 1 root root 2837 Feb  2 19:28 koi-utf

-rw-r--r-- 1 root root 2223 Feb  2 19:28 koi-win

-rw-r--r-- 1 root root 3957 Feb  2 19:28 mime.types

-rw-r--r-- 1 root root 3957 Feb  2 19:28 mime.types.default

-rw-r--r-- 1 root root  484 Feb  2 20:13 nginx.conf

-rw-r--r-- 1 root root 2656 Feb  2 19:28 nginx.conf.default

-rw-r--r-- 1 root root  636 Feb  2 19:28 scgi_params

-rw-r--r-- 1 root root  636 Feb  2 19:28 scgi_params.default

-rw-r--r-- 1 root root  664 Feb  2 19:28 uwsgi_params

-rw-r--r-- 1 root root  664 Feb  2 19:28 uwsgi_params.default

-rw-r--r-- 1 root root 3610 Feb  2 19:28 win-utf

[root@web02 conf]# grep -Ev "#|^$" nginx.conf.default >nginx.conf

[root@web02 conf]# cat nginx.conf

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;

        }

    }

}

nginx配置文件区域分类

 main区块

 event区块

 http区块

 server区块(可以有多个)

 location区块(可以有多个)

 

扩展命令说明     

vimdiff nginx.conf nginx.conf.default    --- 比较两个文件之间配置区别

5.4 nginx服务命令参数说明

/application/nginx/sbin/nginx -s reload    --- -s参数表示一个信号参数,后面可以指定一些信号信息

                                                                         信号信息:reload stop

   /application/nginx/sbin/nginx -t          --- 检查配置文件语法信息

 

[root@web02 nginx-1.12.2]# /application/nginx/sbin/nginx -h

nginx version: nginx/1.12.2

Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

 

Options:

  -?,-h         : this help

  -v            : show version and exit

  -V            : show version and configure options then exit

  -t            : test configuration and exit

  -T            : test configuration, dump it and exit

  -q            : suppress non-error messages during configuration testing

  -s signal     : send signal to a master process: stop, quit, reopen, reload

  -p prefix     : set prefix path (default: /application/nginx-1.12.2/)

  -c filename   : set configuration file (default: conf/nginx.conf)

  -g directives : set global directives out of configuration file

 

 

5.5 实践配置编写配置文件

[root@web02 conf]# vim nginx.conf

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  www.wuhuang.com;

        location / {

            root   html/www;

            index  wuhuang.html;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

    }

}

编写静态访问页面文件信息

[root@web02 www]# pwd

/application/nginx/html/www      站点目录

[root@web02 www]# cat wuhuang.html

<html>

<meta charset="utf-8">

<head>

 <title>吾皇</title>

</head>

<body>

   吾皇

<table border=1>

<tr> <td>01</td> <td>吾皇</td> </tr>

<tr> <td>02</td> <td>巴扎黑</td> </tr>

<tr> <td>03</td> <td>糙汉</td> </tr>

</table>

<a href="http://www.baidu.com

 

">

<img src="wuhuang.jpg" />

</a>

</body>

</html>

[root@web02 www]# ls

wuhuang.html  wuhuang.jpg

修改本地的hosts文件

 C:\Windows\System32\drivers\etc\hosts

10.0.0.8    www.wuhuang.com

验证(通过IP地址访问)

image.png 

通过域名访问

image.png