web服务介绍
Nginxe
是一款高性能轻量级的 Web 服务器/反向代理服务器及邮件服务器,工作在应用层和传输层负载均衡器,支持单机5万个并发,用C编写,占有内存少.并发能力强,配置简单;内部直接支持Rails 和 PHP
OpenRestye
是一个基于Nginx 与 Lua的高性能Web平台,其内部集成了大量精良的Lua库、第三方模块以及大多数的依赖项。目标是让 Web 服务直接跑在 Nginx服务内部,充分利用Nginx的非阻塞I/O模型,单机支持10到100万并发,甚至对远程后端都进行一致的高性能响应Tenginee
是由淘宝网发起的Web 服务器项目。它在 Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性
Apachee
是Apache软件基金会的一个开源的web服务器,由于其多平台和安全性被广泛使用,是世界使用排名第一的 Web 服务器软件。它快速、可靠并性能稳定且可通过简单的API扩展,将Perl/Python等解释器编译到服务器中
Tomcat
是Apache软件基金会一个免费开源的 Web应用服务,属于轻量级应用服务,是开发和调试JSP程序的首选llS
是一种Web服务组件,用于网页浏览,是由微软公司提供的基于运行MicrosoftWindows的互联网基本服务,安全性差
nginx 与apache对比
nginx比 apache占用内存及资源少.nginx社区活跃模块出品迅速,apache模块超多,超级稳定;Apache是同步多进程模型,一个连接对应一个进程,而Nginx是异步的.多个连接(万级别)可以对应一个进程。
apache有3种工作模式
perfork,worker,event模式(多进程处理)
perfork(多进程模型):
每个子进程只有一个线程,一个进程响应一个用户请求,并发使用多个进程实现,进程相对占用资源,消耗大量内存,不擅长处理高并发的场景
worker(多线程模型):
多进程和多线程的混合,一个进程生成多个线程,一个线程响应一个用户请求;并发使用多个线程实现:n进程,n*m个线程
在高并发的场景下会比prefork有更多可用的线程,表现会更优秀一些,在使用keep-alive长连接的时候,某个线程会一直被占用,即使中间没有请求,需要等待到超时才会被释放,资源浪费
event(线程事件)模型):
一个线程响应多个用户请求,基于事件驱动机制来维持多个用户请求
解决了keep-alive长连接的时候占用线程资源被浪费的问题,增强了在高并发场景下的请求处理。在event工作模式中,会有一些专门的线程用来管理这些keep-alive类型的线程,当有真实请求过来的时候,将请求传递给服务器的线程,执行完毕后,又允许它释放
NGINX中支持的并发模型(多进程)
记
select:
IO多路复用、标准并发模型。在编译nginx时指定--with-select_module,select模块将被自动编译
poll:e
lO多路复用、标准并发模型。与select类似,在编译nginx时指定--with-poll_module,poll模块将被自动编译
epoll:
IO多路复用、高效并发模型,可在Linux2.6+及以上内核可以使用kqueue:e
IO多路复用、高效并发模型,可在 FreeBSD 4.1+,OpenBSD 2.9+,NetBSD 2.0,MacOS X平台中使用
/dev/poll:
高效并发模型,可在Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+, andTru64 UNIX 5.1A+平台使用
Nginx 如何实现高并发:
异步非阻塞的事件处理,比如epoll,对于Nginx来讲,一个进程只有一个主线程,通过异步非阻塞的事件处理机制,实现了循环处理多个准备好的事件,从而实现轻量级和高并发。https://news.netcraft.com/archives/category/web-server-survey/
编译nginx
#安装nginx 依赖环境
yum -y install pcre-devel zlib-devel openssl-devel elinks geoip-devel
#创建nginx服务启动用户
[root@localhost ~]# useradd -M -s /sbin/nologin nginx #不创建家目录,指定shell环境
[root@localhost ~]# ls
a.sh init_all.sh uaddwhile.sh 模板 图片 下载 桌面
dhcp.sh nginx-1.23.1.tar.gz 公共 视频 文档 音乐
[root@localhost ~]# tar xf nginx-1.23.1.tar.gz
[root@localhost ~]# mkdir /usr/local/nginx #创建安装目录
[root@localhost ~]# chown -R nginx:nginx /usr/local/nginx #对目录进行递归授权
[root@localhost ~]# cd nginx-1.23.1
[root@localhost nginx-1.23.1]# ./configure --help
[root@localhost nginx-1.23.1]# ./configure \ 对环境进行检测
--prefix=/usr/local/nginx \ #指定nginx安装位置
--user=nginx \ #指定nginx运行的用户
--group=nginx \ #指定nginx用户组
--with-http_stub_status_module \ #启用http_stub_status_module模块
--with-http_ssl_module \ #启用ssl模块
--with-http_flv_module \ #启用FLV模块,提供寻求内存使用基于时间的偏移量文件
--with-http_gzip_static_module[root@localhost nginx-1.23.1]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \--with-http_flv_module \
--with-http_gzip_static_module
[root@localhost nginx-1.23.1]# make #把源码包编译成二进制文件
[root@localhost nginx-1.23.1]# make install #执行安装
[root@localhost nginx-1.23.1]# ls /usr/local/nginx/
[root@localhost nginx-1.23.1]# ls /usr/local/nginx/sbin/
[root@localhost nginx-1.23.1]# ln -sv /usr/local/nginx/sbin/nginx /usr/local/bin/
#为了nginx服务器运行更方便,可以为主程序nginx创建软连接,以遍管理员执行nginx命令可以直接调用nginx的主程序
[root@localhost nginx-1.23.1]# ls /usr/local/bin/nginx
[root@localhost nginx-1.23.1]# nginx
[root@localhost nginx-1.23.1]# nginx -t #检测nginx -t文件是否有错误
[root@localhost nginx-1.23.1]# nginx #启动nginx
[root@localhost nginx-1.23.1]# killall nginx #停止nginx
============================《参数介绍》===============================
主程序Nginx支持标准的进程信号,通过kill或者killall命令传送
HUP 重载配置 等同于-1
QuIT 退出进程 等同于-3
KILL 杀死进程 等同于-9[root@localhost nginx-1.23.1]# killall -HUP nginx #重新加载配置文件
[root@localhost nginx-1.23.1]# killall -s QUIT nginx #退出进程
[root@localhost nginx-1.23.1]# netstat -anptul | grep :80 #nginx端口号80
============================《配置nginx启动脚本》===============================
[root@localhost nginx-1.23.1]# vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost nginx-1.23.1]# chmod +x /etc/init.d/nginx #给脚本修改权限
[root@localhost nginx-1.23.1]# chkconfig --add nginx #把脚本加入到系统里
[root@localhost nginx-1.23.1]# chkconfig nginx on #让服务开机自启
[root@localhost nginx-1.23.1]# chkconfig --list nginx #查看开机自启是否成功
[root@localhost nginx-1.23.1]# service nginx stop
[root@localhost nginx-1.23.1]# service nginx start
nginx主配置文件介绍
主配置文件:ls /usr/local/nginx/conf/nginx.conf
全局配置
#指定运行worker进程用户和组
#worker进程应该以普通用户身份运行(nginx用户、nginx组)
#user nobody##worker进程的个数;通常其数值应该为CPU 的物理核心数减1或相等
worker_processes 1;#CPU绑定
worker_cpu_affinity 000000001;#指定一个worker进程所能够打开的最大文件句柄数(ulimit -n)
worker_rlimit_nofile 1024000;#错误日志文件位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#指定nginx的 pid文件及位置
#pid logs/nginx.pid;I/O事件配置
events{
#使用epoll模型,2.6以上的内核建议使用epoll模型提高性能
user epoll
##每个worker进程能够并发响应最大请求数CPU个数x1024
# work_connections 1024;
}
HTTP配置
http {
include mime.types;
default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';访问日志配置文件
#access_log logs/access.log main;
#是否能启用sedfile(文件下载)功能
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
#保持连续的超时时间;默认为65秒
keepalive_timeout 65;
#gzip on;
👆上面的定义对全局有效#web服务监听配置
server {
#监听地址及端口
listen 80;
#网站名称
server_name localhost;
#网页默认的字符集(FQDN)
#charset koi8-r;#access_log logs/host.access.log main;
#根目录配置
location / {
#网站根目录安装位置的hmtl中
root html;
#默认首页
index index.html index.htm;
}
#error_page 404 /404.html;# redirect server error pages to the static page /50x.html
#
#内部错误反馈页面
error_page 500 502 503 504 /50x.html;
#错误页面配置
location = /50x.html {
#错误页面放的位置
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;# location / {
# root html;
# index index.html index.htm;
# }
#}#https配置段
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;# location / {
# root html;
# index index.html index.htm;
# }
#}}
http超文本传输协议(默认)
https加密超文本传输协议
[root@localhost nginx-1.23.1]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.bak
#备份文件的作用,如果改配置文件出错了,可以还原
虚拟主机上的应用
虚拟主机分类:
基于域名:多个域名解析为一个P地址,不同域名访问到不同网站内容。
www.a.com
www.b.com基于端口:相同IP地址的不同端口访问到不同网站内容。
http//192.168.1.1:80
http//192.168.1.1:81[root@localhost conf]# mkdir /usr/local/nginx/html/linux
[root@localhost conf]# mkdir /usr/local/nginx/html/cloud
[root@localhost conf]# ls /usr/local/nginx/html
echo "<h1>www.linux.com</h1>" > /usr/local/nginx/html/linux/index.html
echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html
[root@localhost conf]# echo "<h1>www.linux.com</h1>" > /usr/local/nginx/html/linux/index.html
[root@localhost conf]# echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html[root@localhost conf]# vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;
sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#第一个网页
server {
listen 80;
server_name www.linux.com;charset koi8-r;
access_log logs/linux.access.log main;
location / {
root html/linux ;
index index.html index.htm;
}
}
#第二个网页
server {
listen 80;
#域名名字
server_name www.cloud.com;charset koi8-r;
#日志的名字
access_log logs/cloud.access.log main;location / {
root html/cloud; #位置
index index.html index.htm; #定义的第一个页面 默认index.html
}
[root@localhost conf]# nginx -t #测试
[root@localhost conf]# systemctl restart nginx #启动nginx
[root@localhost conf]# vim /etc/hosts
192.168.20.128 www.linux.com
192.158.20.128 www.cloud.com
[root@localhost conf]# curl http://www.linux.com
[root@localhost conf]# curl http://www.cloud.com测试
真机c盘
C:\Windows\System32\drivers\etc
用记事本编辑192.168.20.128 www.linux.com
192.168.20.128 www.cloud.com
加入hosts文件
虚拟机执行
流程
安装nginx
yum -y install pcre-devel zlib-devel openssl-devel elinks geoip-devel
useradd -M -s /sbin/nologin nginx
tar xf nginx-1.23.1.tar.gz
mkdir /usr/local/nginx
chown -R nginx.nginx /usr/local
cd nginx-1.23.1
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-http_flv_module \
--with-http_gzip_static_module
make && make installln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
nginx -t
nginx
-----------------------------------------------
基于域名的虚拟主机
mkdir /usr/local/nginx/html/linux
mkdir /usr/local/nginx/html/cloud
echo "<h1>www.linux.com</h1>" > /usr/local/nginx/html/linux/index.html
echo "<h1>www.cloud.com</h1>" > /usr/local/nginx/html/cloud/index.html
vim /usr/local/nginx/conf/nginx.conf#user nobody;
worker_processes 1;#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';access_log logs/access.log main;
sendfile on;
#tcp_nopush on;#keepalive_timeout 0;
keepalive_timeout 65;#gzip on;
server {
listen 80;
server_name www.linux.com;
charset utf-8;
access_log logs/linux.access.log main;location / {
root html/linux;
index index.html index.htm;
}
}server {
listen 80;
server_name www.cloud.com;
charset utf-8;
access_log logs/cloud.access.log main;location / {
root html/cloud;
index index.html index.htm;
}
}
}
killall nginx
nginx
vim /etc/hosts
192.168.80.40 www.linux.com
192.168.80.41 www.cloud.com测试
真机c盘
C:\Windows\System32\drivers\etc
用记事本编辑
测试访问
curl http://www.linux.com
curl http://www.cloud.com
nginx的https配置
https数字证书
数字证书的基本架构是利用一对密钥实施加密和解密。其中密钥包括私钥和公钥,私钥主要用于签名和解密,由用户自定义,只有用户自己知道;公钥用于签名验证和加密,可被多个用户共享。
CA为颁发证书机构,而https通信基于证书实现,所以要部署CA服务器.然后授予WEB服务器证书签名,最后实现https通信《记住 TLS/SSL(数据完整性的一种安全协议)
实现方式:对于web网站使用https协议:端口443,基于对称加密算法实现的》
《记住 HTTPS请求过程
1、先 TCP/IP协议三次握手
2、客户端向server 端发送建立连接请求,
3、server端和客户端互相协商使用的加密协议(SSLv2、SSLv3、TLSv1)和加密算法
4、一般客户端没有证书,server 将自己证书发给客户端
5、客户端验证证书完整性和信任性及获取server 加密协议公钥,然后客户端传递加密后的对称密码给server
6、然后server拿着对称密码加密数据给客户端传递》https和http的区别
1.申请https证书需要一定的费用·
2.https用的是443端口,http用的是80端口
3.http是无状态的,明文传输数据,https对数据进行加密传输https的缺点
1.连接比较耗时
2.并非百分之百安全可靠
3.必须绑定IP地址,同一个IP地址不能绑定多个域名
生成
#生成rsa私钥,des3 算法,2048位强度,server.key是秘钥文件名
[root@localhost nginx-1.23.1]# openssl genrsa -des3 -out server.key 2048
Generating RSA private key, 2048 bit long modulus
.....................+++
..............+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
#两次输入密码,我的密码是root#生成CSR(证书签名请求)需要依次输入国家,地区,城市,组织,组织单位,CommonName和 Email。其中Common Name,可以写自己的名字或者域名,如果要支持https,Common Name应该与域名保持一致,否则会引起浏览器警告。
[root@localhost ~]# openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key:#输入密码root
Country Name (2 letter code) [XX]: #CN地区
State or Province Name (full name) []:#地区北京bj
Locality Name (eg, city) [Default City]:#地区bj
Organization Name (eg, company) [Default Company Ltd]:it
Organizational Unit Name (eg, section) []:abc
Common Name (eg, your name or your server's hostname) []:*.abc.com
Email Address []:123@163.com
A challenge password []: #直接回车
An optional company name []:#直接回车#删除私钥中密码
[root@localhost ~]# openssl rsa -in server.key -out server.key
Enter pass phrase for server.key:#输入之前设置的密码
writing RSA key#生成自签名证书
[root@localhost ~]# openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
#365 生成的天数,就是有效期多少天。server.csr签名请求在什么地方,server.crt私钥在什么地方,server.crt生成的证书在什么地方,server.crt证书名
Signature ok
subject=/C=CN/ST=bj/L=bj/O=it/OU=abc/CN=*.abc.com/emailAddress=123@163.com
Getting Private key
X.509证书包含三个文件:key,csr,crt
key 是服务器上的私钥文件,用于对发送给客户端数据的加密,以及对从客户端接收到数据的解密
csr 是证书签名请求文件,用于提交给证书颁发机构(CA)对证书签名
crt 是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息
nginx 配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
root@node4~$>vim /usr/local/nginx/conf/nginx.conf
# HTTPS server
#
server {
listen 443 ssl;
server_name www.abc.com;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_certificate server.crt;
ssl_certificate_key server.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
location / {
root html;
index index.html index.htm;
}
}
mv /root/server.* /usr/local/nginx/conf/
echo "aaaaaaaaaaaaa" > /usr/local/nginx/html/index.html
vim /etc/hosts
192.168.80.40 www.linux.com
192.168.80.41 www.cloud.com测试访问
curl http://www.linux.com
curl http://www.cloud.com