Web 网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的 请求后才会响应,最终用于提供服务程序的 Web 服务器会通过 HTTP(超文本传输协议)或 HTTPS(安全超文本传输协议)把请求的内容传送给用户。
目前能够提供 Web 网络服务的程序有 IIS、Nginx 和 Apache 等。其中,IIS(Internet Information Services,互联网信息服务)是 Windows 系统中默认的 Web 服务程序。
2004 年 10 月 4 日,为俄罗斯知名门户站点而开发的 Web 服务程序 Nginx 横空出世。 Nginx 程序作为一款轻量级的网站服务软件,因其稳定性和丰富的功能而快速占领服务器市 场,但 Nginx 最被认可的还当是系统资源消耗低且并发能力强,因此得到了国内诸如新浪、 网易、腾讯等门户站的青睐。
常用web框架为diango,flask,tornado以及sanic等
django 大而全, 功能特别多 ,例如:form表单 , ORM, 中间件 ,特点:笨重,臃肿,并发数: 600/s
flask 特点:轻量级的,小而精, 它使用的都是第三方模块进行拼接起来的 并发数:4988/s
tornado 特点:支持异步, 处理用户请求过来数据不用等待,类似于协程 并发数:2138/s
sanic 特点:python3.5+uvloop 并发数: 33342/s
nginx 开源的,支持高性能,高并发的
apache nginx他父亲
IIS(windows下面的WEB Server)
使用curl -I 命令查看taobao和JD的WEB服务器,淘宝和京东都是使用的都是自主定制的nginx的web服务器
1.4.1 安装nginx的依赖库
yum install -y gcc patch libffi-devel python-devel zlib-devel bzip2-devel openssl openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
1.4.2 下载安装nginx源码包
cd /opt wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
1.4.3 解压缩源码(还是在/opt目录下)
tar -zxvf nginx-1.12.0.tar.gz
1.4.4 配置,编译安装 开启nginx状态监测功能
cd /opt/nginx-1.12.0
./configure --prefix=/opt/nginx112
1.4.5 编译安装
cd /opt/nginx-1.12.0
make && make install
1.4.6 启动nginx,进入sbin目录,找到nginx启动命令
cd /opt/nginx112/sbin ./nginx #启动 ./nginx -s stop #关闭 ./nginx -s reload # 平滑重启 ,修改了nginx.conf之后,可以不重启服务,加载新的配置
1.4.7 查看nginx运行状态
查看端口是否运行: netstat -tunlp
查看进程是否运行: ps -ef | grep nginx
1.4.8 nginx目录下的文件
conf 存放nginx所有配置文件的目录,主要nginx.conf
html 存放nginx默认站点的目录,如index.html、error.html等
logs 存放nginx默认日志的目录,如error.log access.log
sbin 存放nginx主命令的目录,sbin/nginx
1.4.9 nginx.conf配置文件解析
#定义nginx工作进程数 worker_processes 5; #错误日志 #error_log logs/error.log; #http定义代码主区域 http { include mime.types; default_type application/octet-stream; #定义nginx的访问日志功能 #nginx会有一个accses.log功能,查看用户访问的记录 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; keepalive_timeout 65; #开启gzip压缩传输 gzip on; #虚拟主机1 定义一个 斗鱼网站 server { #定义nginx的访问入口端口,访问地址是 192.168.11.37:80 listen 80; #定义网站的域名www.woshidouyu.tv #如果没有域名,就填写服务器的ip地址 192.168.11.37 server_name www.woshidouyu.tv; #nginx的url域名匹配 #只要请求来自于www.woshidouyu.tv/111111111 #只要请求来自于www.woshidouyu.tv/qweqwewqe #最低级的匹配,只要来自于www.woshidouyu.tv这个域名,都会走到这个location location / { #这个root参数,也是关键字,定义网页的根目录 #以nginx安装的目录为相对路径 /opt/nginx112/html #可以自由修改这个root定义的网页根目录 root html; #index参数定义网站的首页文件名,默认的文件名 index index.html index.htm; } #错误页面的优化(只要是遇到前面4系列的错误,就会直接跳转到相对目录下的40x.html页面) error_page 400 401 402 403 404 /40x.html; } }
vim /opt/nginx112/conf/nginx.conf 修改server代码块 server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 400 402 403 404 /40x.html; #error_page 500 502 503 504 /50x.html; } 平滑重启nginx /opt/nginx112/sbin/nginx -s reload
打开百度或其他网站,随便输入一个不存在的链接,就可以访问到错误页面 右键点击页面空白处,查看网页源代码 拷贝源代码,粘贴到/opt/nginx112/html/40x.html下面去 再访问我们的网站,随便输入一个不存在的链接, 就可以访问到这个错误页面 http://192.168.1.40/asldfjasd
nginx访问日志功能
nginx限制网站来源IP
如果想要在本地访问一个域名, 可以更改本机hosts文件
windows下
c:\\\windows\system32\drivers\etc\hosts
linux下
/etc/hosts
配置nginx多虚拟主机
worker_processes 5; #error_log logs/error.log; 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; keepalive_timeout 65; #gzip on; server { listen 80; server_name zongze.qishi3.com; location / { #deny 192.168.1.0/24; root /opt/qishi3/zongze; index index.html index.htm; } error_page 400 402 403 404 /40x.html; #error_page 500 502 503 504 /50x.html; } server { listen 80; server_name xiaochun.qishi3.com; location / { root /opt/qishi3/xiaochun; index index.html index.htm; } } server { listen 80; server_name huafeng.qishi3.com; location / { root /opt/qishi3/huafeng; index index.html index.htm; } } }
cd /opt rm -rf qishi3 mkdir -p qishi3/huafeng mkdir -p qishi3/zongze mkdir -p qishi3/xiaochun touch qishi3/huafeng/index.html touch qishi3/zongze/index.html touch qishi3/xiaochun/index.html 分别打开这三个index.html文件 vim index.html 分别添加内容 我是华峰 我是宗泽 我是小春 然后保存退出
c:\\windows\system32\drivers\etc\hosts
添加以下三行解析记录
192.168.1.40 zongze.qishi3.com 192.168.1.40 xiaochun.qishi3.com 192.168.1.40 huafeng.qishi3.com
重启后即可
nginx的代理功能主要有正向代理与反向代理
正向代理,图例如下,server在明,用户在暗,服务器只知道这个区域访问,具体到指定的pc还是不清楚的,正向代理目前用的比较少,日常用的比较多的是各种FQ软件。
反向代理,图例如下,对于用户来说只知道访问这个网站,具体到后面这个网站哪台服务器则不是清楚,对于现在的web服务器则用的比较多的是反向代理
实验效果:
在windows中访问代理服务器,然后让代理服务器去拿web服务器的数据
请求数据: windows ——> 192.168.1.15 ——> 192.168.1.40
返回数据: windows <—— 192.168.1.15 <—— 192.168.1.40
机器准备,两台服务器
master 192.168.1.15 主负载
slave 192.168.1.40 web1
修改代理服务器192.168.1.15的配置文件
vim /opt/nginx112/conf/nginx.conf 在location代码块下添加一行数据 proxy_pass http://192.168.1.40;
负载均衡,在网站初期,单台服务器可以承担多数人的访问,在访问人数的增加,单个服务器已经无法承担。在增加多个服务器后,平均的让服务器接收这些访问量,就需要一个负载均衡。而nginx可以实现负载均衡
实验背景:
(1)有三台机器 一台为nginx代理服务器(负载均衡调度器), 另外两台为WEB服务器
192.168.1.15 # 负载均衡调度器 192.168.1.40 # WEB服务器1 192.168.1.169 # WEB服务器2
(2)用户访问192.168.1.15,由nginx代理服务器通过负载均衡调度器分别分配到两个WEB服务器,实现负载均衡
实验步骤:
(1)准备三台机器
192.168.1.15 # 负载均衡调度器 192.168.1.40 # WEB服务器1 192.168.1.169 # WEB服务器2
(2)两个WEB服务器可以正常访问
访问web服务器1返回“代噶好,我系帅帅刘 我真的是1.40这台机器”
访问web服务器2返回“MMP 我TM是文龙”
(3)在nginx代理服务器(负载均衡调度器)上面做如下配置:
(4)分别平滑重启三台机器的nginx服务
/opt/nginx112/sbin/nginx -s reload
(5)访问192.168.1.15就可以看到,WEB1和WEB2交替返回数据
注:在实际应用场景中,不一定会使用nginx来做负载均衡,有些在大的公司中,则会用一些硬件来做负载均衡,例如F5等。
a
原文:https://www.cnblogs.com/gbq-dog/p/10668618.html