web服务器种类
apache
nginx
tomcat
resin
Lighttpd
IIS
WebLogic
Jetty
Node.js
web服务组合
比较早的,比较经典的web服务组合
LAMP(linux apache mysql php)
近几年的一个web服务组合
LNMP(linux nginx mysql php)
LEMP(linux (engine x) mysql php)
nginx
http://nginx.org/en/docs/
nginx本身是一款静态(html,js,css,jpg等)www软件
静态小文件高并发,同时占用资源少 3w并发 10个线程 150M
nginx三大应用
1、 www web 服务
2、 负载均衡 (反向代理proxy)
3、 web cache(web缓存)
https://w3techs.com/technologies/overview/web_server/all
[图片上传失败…(image-ecf6c2-1561896934467)]
nginx功能
可针对静态资源高速高并发访问及缓存
可使用反向代理加速,并且可进行数据缓存
具有简单负载均衡、节点健康检查和容错功能
支持远程FastCGI服务的缓存加速
支持FastCGI、Uwsgi、SCGI、and Memcached Servers的加速和缓存
支持SSL、TLS、SNI
具有模块化的架构:过滤器包括gzip压缩、ranges支持、chunked响应、XSLT、SSI及图像缩放等功能。在SSI过滤器中,一个包含多个SSI的页面,如果经由FastCGI或反向代理处理,可被并行处理
支持http2.0协议
http://nginx.org/en/docs/http/ngx_http_v2_module.html
nginx优点
高并发(静态小文件), 静态 1-2W
功能种类比较多(web,cache,proxy)
利用nginx可以对ip限速,可以限制连接数
基于异步网络I/O模型(epoll、kqueue)使得nginx可以支持高并发
具备支持高性能,高并发的特性,并发连接可达数万 (占用资源少 2W并发 开10个线程服务,内存消耗几百M)
对小文件(小于1MB的静态文件)高并发支持好,性能很高
不支持类似apache的DSO模式,扩展库必须编译进主程序(缺点)
进程占用系统资源比较低
nginx应用场合
1、 静态服务器(图片,视频服务)
html,js,css,flv等
2、 动态服务,nginx+fastcgi 运行php,jsp 并发 500-1500
apache+php, Lighttpd+fcgi php
3、反向代理,负载均衡 日pv2000W一下,都可以直接用nginx做代理
haproxy, F5, a10
4、缓存服务 squid,varnish
nginx作为web服务器的主要应用场合
使用nginx运行html、js、css、小图片等静态数据
nginx结合fastcgi运行php等动态程序(fastcgi_pass)
nginx 结合tomcat/resin 等支持java动态程序(proxy_pass)
nginx和其他web服务的对比
静态
[图片上传失败…(image-b7c21-1561896934468)]
动态
[图片上传失败…(image-a09ed8-1561896934468)]
nginx与其他web软件对比
先来看看apache的特点
高并发时消耗系统资源相对多一些
基于传统的select模型,高并发能力有限
支持扩展库,可通过DSO、apxs方法编译安装额外的插件功能,不需要重新编译apache
功能多,更稳定,更安全,插件也多
如何选择web服务器
静态业务:高并发,采用nginx或lighttpd,根据自己的掌握程度或公司要求
动态业务:采用nginx和apache均可
既有静态业务又有动态:nginx或apache,不要多选,要单选
动态业务可以由前端代理(haproxy),根据页面元素的类型,向后转发相应的服务器进行处理
反向代理或负载均衡服务
在反向代理或负载均衡服务方面,nginx可以作为web服务、php等动态服务及
memcached 缓存的代理服务器,它具有类似专业反向代理软件(如haproxy)的
功能,同时也是一个优秀的邮件代理服务软件
前端业务数据缓存服务
在web缓存服务方面,nginx可通过自身的proxy_cache模块实现类squid等
专业缓存软件的功能
为什么nginx总体性能比apache高
nginx使用最新的epoll(linux2.6内核)和kqueue(freebsd)异步网络I/O模型,而apache则使用的是传统的select模型。目前linux下能够承受高并发访问的squid、memcached软件都采用的是epoll模型
处理大量连接的读写,apache所采用的select网络I/O模型比较低效
linux下软件安装
1、 rpm -ivh *.rpm
有依赖问题
2、 yum安装 解决rpm安装的依赖问题,安装更简单化
优点:简单 易用 高效
缺点:不能定制
3、源码包编译安装
./configure #配置
make #编译
make install #安装
4、定制rpm包,搭建yum仓库,把我们定制的rpm包放到yum仓库,进行yum安装
优点:2和3 的优点
缺点:复杂
nginx安装
下载nginx软件包
wget http://nginx.org/download/nginx-1.12.2.tar.gz
安装基础依赖包
yum install gcc gcc-c++ automake autoconf
安装pcre
https://regexr.com/
http://www.regexlab.com/zh/deelx/
pcre全称(Perl Compatible Regular Expressions),中文“perl兼容正则表达式”,官方站点为
http://www.pcre.org 安装pcre库是为了使nginx支持具备URI重写功能的rewrite模块,如果不安装pcre库,则nginx无法使用rewrite模块功能,nginx的rewrite模块功能几乎是企业应用必须
yum install pcre pcre-devel -y
安装openssl
yum install openssl openssl-devel -y
安装nginx
tar xf nginx-1.6.2.tar.gz
cd nginx-1.6.2
useradd www -s /sbin/nologin -M
http://nginx.org/en/docs/configure.html
./configure --user=www --group=www --prefix=/application/nginx-1.6.2 --with-http_stub_status_module --with-http_ssl_module
make
make install
ln -s /application/nginx-1.6.2 /application/nginx
yum安装nginx
nginx yum源配置
cat /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/
r
e
l
e
a
s
e
v
e
r
/
releasever/
releasever/basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
- hosts: 172.16.1.7
tasks:- name: Add nginx Yum Repository
yum_repository:
name: nginx-stable
description: nginx-stable Repository
baseurl: http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/
gpgcheck: yes
gpgkey: https://nginx.org/keys/nginx_signing.key
nginx (yum 安装)
yum install nginx
- name: Add nginx Yum Repository
- name: install the latest version of nginx
yum:
name: nginx
state: latest- name: copy nginx.conf
copy:
src: default.conf
dest: /etc/nginx/conf.d/
notify: restart nginx - name: Start service nginx, if not started
service:
name: nginx
state: started
handlers: - name: restart nginx
service:
name: nginx
state: restarted
- name: copy nginx.conf
- hosts: nginx
tasks:- name: Add Base Yum Repository
yum_repository:
name: nginx-stable
description: nginx-stable Repository
baseurl: http://nginx.org/packages/centos/ r e l e a s e v e r / releasever/ releasever/basearch/
gpgcheck: yes
gpgkey: https://nginx.org/keys/nginx_signing.key - name: install the latest version of nginx
yum:
name: nginx
enablerepo: nginx-stable
state: latest - name: copy nginx.conf
copy:
src: default.conf
dest: /etc/nginx/conf.d/
notify: restart nginx - name: Start service nginx, if not started
service:
name: nginx
state: started
handlers: - name: restart nginx
service:
name: nginx
state: restarted
启动nginx
检查语法
[root@web01 nginx]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.2/conf/nginx.conf test is successful
启动nginx
[root@web01 nginx]# /application/nginx/sbin/nginx
查看端口
[root@web01 application]# netstat -lntup |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 3770/nginx
curl
curl -v 10.0.0.7
- name: Add Base Yum Repository
curl -H Host:www.etiantian.org 10.0.0.7
停止nginx
/application/nginx/sbin/nginx -s stop
查看nginx配置编译选项
[root@web01 ~]# /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: --user=www --group=www --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module
测试页面
vim index.html
Welcome to wuXing linux
cheer,all,every one.
wuxing github.Thank you for wuxing_github.
nginx配置 nginx模块 1、nginx core modules(必须的)main
events
2、standard http modules (不是必须的,默认都会安装)
core 核心的http参数配置,对应nginx的http区块
access 访问控制模块,用来控制网站用户对nginx的访问
fastcgi fastcgi模块,和动态应用相关,如php
gzip 压缩模块,对nginx返回的数据压缩,性能优化模块
log 访问日志模块
proxy proxy代理模块
rewrite URL地址重写模块
upstream 负载均衡模块
limit_conn 限制用户并发连接数及请求数模块
limit_req 根据定义的key限制nginx请求过程的速率
auth_basic web认证模块,设置web用户账号密码访问
ssl ssl模块,用于加密的http连接,如https
stub_status 记录nginx基本访问状态信息
nginx核心功能模块(Core functionality)
nginx核心功能模块负责nginx的全局应用,主要对应主配置文件的main区块和events区块区域,这里有很多nginx必须的全局参数配置
http://nginx.org/en/docs/ngx_core_module.html
ngx_http_core_module 包括一些核心的http参数配置,对应nginx的配置为http区块部分
ngx_http_access_module 访问控制模块,用来控制网站用户对nginx的访问
ngx_http_gzip_module 压缩模块,对nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_module fastcgi模块,和动态应用相关的模块,例如PHP
ngx_http_proxy_module proxy代理模块
ngx_http_upstream_module 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
ngx_http_rewrite_module URL地址重写模块
ngx_http_limit_conn_module 限制用户并发连接数及请求数模块
ngx_http_limit_req_module 根据定义的key限制nginx请求过程的速率
ngx_http_log_module 访问日志模块,以指定的格式记录nginx客户访问日志等信息
ngx_http_auth_basic_module web认证模块,设置web用户通过账号密码访问nginx
ngx_http_ssl_module ssl模块,用于加密的http连接,如https
ngx_http_stub_status_module 记录nginx基本访问状态信息等的模块
nginx主配置文件 nginx.conf (默认配置)
[root@web01 conf]# egrep -v “#|^$” nginx.conf |cat -n
1 worker_processes 1; #worker进程的数量
2 events { #事件区块开始
3 worker_connections 1024; #每个work进程支持的最大连接数
4 } #事件区块结束
5 http { #http区块开始
6 include mime.types; #nginx支持的媒体类型库文件包含
7 default_type application/octet-stream; #默认的媒体类型
8 sendfile on; #开启高效传输模式
9 keepalive_timeout 65; #连接超时
10 server { #第一个server区块开始,表示一个独立的虚拟主机站点
11 listen 80; #提供服务的端口,默认80
12 server_name localhost; #提供服务的域名主机名
13 location / { #第一个location区块开始
14 root html; #站点的根目录,相对于nginx安装目录
15 index index.html index.htm;
16 }
17 error_page 500 502 503 504 /50x.html;
18 location = /50x.html {
19 root html;
20 }
21 }
22 }
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 www.etiantian.org;
location / {
root html/www;
index oldboy.html;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
mkdir …/html/www
echo oldboy >> …/html/www/oldboy.html
oldboy.html
01 | xxx |
02 | xxx |
03 | xxx |

cd /usr/local/nginx/conf
openssl genrsa -idea -out server.key 2048
openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
在nginx.conf中配置这个证书
server {
server_name YOUR_DOMAINNAME_HERE;
listen 443 ssl;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key;
}
nginx状态
status.conf
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
allow 10.0.0.0/24; #<==设置允许和禁止的IP段访问
deny all; #<==设置允许和禁止的IP段访问
}
}
[图片上传失败…(image-84101c-1561896934468)]
第一个server表示nginx启动到现在共处理了67个连接;
第二个accepts表示nginx启动到现在共成功创建67个连接;
请求丢失数=握手数-连接数 可以看出,本次状态显示没有丢失请求
第三个handled requests 表示总共处理了202次请求;
Reading 为nginx读取到客户端的Header信息数
Writing 为nginx返回给客户端的Header信息数
Waiting 为nginx已经处理完正在等候下一次请求指令的驻留连接。在开启keep-alive的情况下,这个值等于 active-(reading+writing)
- name: get current http stats
nginx_status_facts:
url: http://localhost/nginx_status
timeout: 20