web架构基础

web服务

web介绍

c/s架构client客户端server服务端 qq 微信 腾讯会议 LOL
客户端比较大,服务端小

b/s架构browser server
客户端:浏览器,小
服务端:大

web1.0:纯静态页面,不能交互html文档 人工编写网页

image.png
web2.0: 2003年 支持用户上传信息,交互,购物网站后台:ruby java perl php python html 编程语言自动生成 阿里巴巴的创立
web3.0:移动互联网时间衣食住行出门只带个手机 网站后台:java php python go

image.png

http协议介绍

超文本传输t协议(Hyper Text Transfer Protocol,HTTP)
请求头:

image.png

http协议:

http请求头包含4部分:

  1. http请求起始行 GET /favicon.ico HTTP/1.1 请求方法 请求uri http版本
  2. http请求头部 和服务端协商 4-n行 行数不固定
  3. http请求空行 # 隔断作用
  4. http请求主体 可无内容 post方法有时有内容

http响应头包含4部分:

  1. http响应起始行 HTTP/1.1 302 Moved Temporarily http协议版本 http状态码 对状态码的解释
  2. http响应头部,和客户端协商,行数不固定
  3. http响应空行 # 隔断
  4. http响应主体 #具体文件(html png mp4 exe )

请求url == http:// + 主机(192.168.20.131)+ 端口(:80) + uri (/favicon.icoI)
url:统一资源定位符 http://192.168.20.131/favicon.ico 端口省略了

http0.9、1.0和1.1版的区别

http0.9: 支持请求方法只有GET 没有请求头 网页只能看
http 1.0:支持GET POST… 可以进行交互 但速度慢 例如50个请求 每次请求html css js png…资源时都要进行三次握手四次挥手
http1.1:支持GET POST… 可以进行交互 50个请求 只需要进行一次三次握手四次挥手 三次握手 html css js1 js2 png… 四次挥手
http2.0 https复用连接 提高速度

请求方法有哪些?

get 下载 请求资源
post 上传 提交信息\数据
head 头 监控 健康检查
put 上传文件

http状态码2xx,3xx,4x,5xx

2xx 请求都是ok
3xx 跳转,重定向 301永久跳转 302 临时跳转
4xx 客户端请求问题请求姿势不对 403权限拒绝 404 文件找不到
5xx 服务端出问题 500 服务器内部错误 502 错误的网关 504 网关超时

html介绍

HTML的英文全称是
Hyper Text Markup Language,即超文本标记语言。
css:排版布局
js:特效
超链接
flash很多网站已经不支持flash ,html5 (H5) 支持视频播放,小游戏

nginx安装和配置

apache是一个软件的名字:web服务器==web server
apache 后来发展为软件基金会:
apache的 httpd: web server
nginx最常用的web服务器软件 效率高

安装方法1:
使用epeI源安装
yum install epel-release -y  安装扩展仓库   yum repolist 查看扩展仓库
yum install nginx -y
systemctl start nginx.service 设置开机自启
systemctl enable nginx.service

hostnamectl set-hostname web01   修改主机名可不改

netstat -lntup 查看网络状态

image.png
nginx 占了(监听)80端口当安装apache的httpd ,开启httpd时报错
yun install httpd -y
natstat -lntup
systemctl start httpd 启动报错
systemctl ststus httpd 查看状态

nginx 的默认界面

image.png
nginx配置1:
nginx 默认备份了配置文件
image.png
image.png
简化配置文件 把配置文件中的注释和空格删除
image.png
过滤后的配置文件重定向到nginx主配置中

grep -Ev '#|^$' /etc/nginx/nginx.conf.default >/etc/nginx/nginx.conf
vim /etc/nginx/nginx.conf
#删除17-20行
worker_processes 1;  #启动nginx 工作进程的数量  master进程是管理 worker process工作进程
events{
	worker_connections 1024;  #每个进程可以处理1024连接
}
http{        #http模块
	include 	mime.types;	#支持哪些多媒体格式
	default_type application/octet-stream;   #默认以十进制数据流
	charset utf-8;	#设置字符集
	server{			#代表一个网站   一个nginx可以运行多个网站
		listen 80;	#监听端口
		server_name	localhost;		#网站的域名
		location / { 		#目录
				root		html;		#站点根目录/usr/share/nginx/html 当前为相对路径,也可以手动更改为绝对路径
				index index.html index.htm;		#指定默认首页 若没有首页显示403 氮url中输入 IP/1.html (index.html改为了1.html) 可以访问
  	}
	}
}

image.png
nginx的站点根目录
image.png

nginx 进阶学习

nginx多站点配置

一个nginx运行多个网站 ,每个网站的访问入口不能相同
http:// + ip/域名 + 端口 + uri (ip/域名 或 端口变了入口就变了)

实现方式

1 多端口

image.png
image.png

[root@web01 nginx]#cat nginx.conf
worker_processes 	2;
events {
		worker_connections 1024;
}
http {
		include				mime.types;
		default_type  application/octet-stream;
		charset utf-8;
		server {
					listen			80;
					server_name localhost;
					location / {
								root  /htm1/one;  三个网站有三个站点目录
        				index  index.html index.htm;
        	}
    	}
    	server {
					listen			81;
					server_name localhost;
					location / {
								root  /htm1/two;
        				index  index.html index.htm;
        	}
    	}
      server {
					listen			82;
					server_name localhost;
					location / {
								root  /htm1/three;
        				index  index.html index.htm;
        	}
    	}
}

[root@web01 nginx]#mkdir -p /html/{one,two,three}
[root@web01 nginx]#echo 'one' >/html/one/index.html
[root@web01 nginx]#echo two' >/html/two/index.html
[root@web01 nginx]#echo three' >/htm1/three/index.html

[root@web01 nginx]#systemctl restart nginx.service

image.png

2 多IP (不推荐)

Windows 也可以设置多IP
nginx配置多IP

image.png
修改nginx配置 vim /etc/nginx/nginx.conf
image.png
image.png
0.0.0.0:80 代表所有IP的80都能访问

3 多域名 (常用,省IP)

修改nginx配置 vim /etc/nginx/nginx.conf

image.png
image.png
重启nginx: systemctl restart nginx.service
现在这几个域名不是我们的,没有权限用别人的域名,需要配置windows的主机hosts,本地的hosts比dns优先级高
image.png
image.png

用include加载外部配置文件实现 多站点管理

image.png
修改主配置文件,让include加载 /etc/nginx/conf.d/目录下的配置文件
image.png
增加编辑每个站点的配置文件
image.png
image.png
image.png
当那个站点不使用时,将配置文件修改(a.dms.com.conf.stop),找不到到配置文件就会挑战到默认网站
image.png
增加了 default _server 后这个网站就变成了默认网站

nginx日志

web服务器必须开启日志,方便溯源
ssh日志 cat /var/log/secure
错误日志 可以自定义路径,不能定义格式

error_log /opt/nginx_error.log info;

访问日志
nginx 默认日志 /var/log/nginx

image.png
image.png

定制日志格式,修改主配置文件,日志必须定义在server前面
log_format compression 'Sremote_addr - $remote_user [$ime_local] '
											 '"$request" $status $bytes_sent '
											 '"$http_referer" "Shttp_user_agent" "Sgzip_ratio"';
access_log /var/log/nginx/access.log compression;

192.168.18.10 -- [05/Aug/2021:14:46:07+0800] "GET /c.html HTTP/1.1 " 200 257
"http://c.com/" "Mozilla/5.0 (windows NT 10.0;win64;x64) Applewebkit/537.36 (KHTML,
like Gecko) Chrome/92.0.4515.131 Safari/537.36" "-"

# $remote_addr  客户端的ip地址
# $remote_user  客户端的用户名
# $time_local   当前时间
# $request      请求起始行
# $status       http状态码
# $bytes_sent   资源的大小
# $http_referer   记录资源的跳转地址
# $http._user_agent   用户的终端信息
# $gzip_ratio         gzip的压缩级别

nginx官方文档 https://nginx.org/en/docs/

image.png
每个网站单独记录自己的日志
image.png
image.png可以自己定义路径
若没有自定义,就写在/var/log/nginx
image.png
给予opt权限,不然写不了文件
image.png

nginx的basic认证配置

basic认证 == 加一把锁
第一步生成:basic认证的账号密码

image.png
第二步:修改网站配置文件

auth_basic "qstack.com.cn(备注,随便写)";
auth_basic_user_file /etc/nginx/htpasswd;

image.png
image.png
image.png
编辑/etc/nginx/htpasswd/ 里面保存 htpasswd生成的账号密码

nginx的ssl证书配置

http显示不安全的 ,https安全 就是多了一个ssl证书认证
http 80端口
https 443 端口 加密
证书和域名必须匹配
第一步:申请证书
第二步:修改nginx配置文件 修改hosts文件
第三步:准备站点代码文件
第四步:访问nttps:xiaoniao,oldgiang,com
image.png
腾讯云的官方文档

image.png
image.png

rewrite跳转 nginx的http跳转到https

#使用returnl跳转
server {
			access_log off;
			listen	80;
			server_name			blog.o1dqiang.com;
  	  location / {
				return	302 https://blog.o1dqiang.comSrequest_uri;
    	}
}
#使用rewrlite跳转
server {
			access_log off;
			listen		80;
			server_name blog.oldqiang.com;
			location / {
					rewrite ^/(.*) https://blog.oldqiang.com/$1 redirect;#302临时跳转
					rewrite ^/(.*) https://blog.oldqiang.com/$1 permanent;#301永久跳转
    	}
}

image.png
增加一个server

url:协议http:/ + 主机(ip或者域名) + 端口(80/443) + uri(/2000.png)

nginx gzip压缩

gzip on;								#开启gzip压缩
gzip_min_length 1k;			#最小压缩文件
gzip_buffers 4 32k;			#内存缓冲
gzip_http_version 1.1;	#http版本
gzip_comp_level 9;			#压缩等级
gzip_types text/css text/xml application/javascript;#压缩w类型
gzip_vary on;						#http响应头添加gzip标识  验证vary
gzip_disable "MSIE [1-7]\.";	#遇到工E浏览器1-7取消gz1p压缩

image.png
image.png
压缩会消耗一些额外cpu的性能
压缩,压缩值得压缩的文件:text html css is
不值得压缩的文件:图片,视频,音频压缩包

nginx目录浏览

配置文件里面添加

autoindex on;   #没有首页自动生成首页
autoindex_exact_size off;    格式化文件大小   65535 -》65k

image.png
image.png

apache 也有目录浏览功能

nginx访问控制allow和deny

a11ow		允许
deny 		拒绝
			deny 192.168.18.10;  
			a11ow 0.0.0.0/0;  允许所有

访问控制:禁用 拉黑

  1. 4层 传输层 所有的网站都访问不了,拉黑 单个IP
systemctl start firewalld.service   #开启主机防火墙
firewall-cmd --add-rich-rule='rule family=ipv4 source address="192.168.20.1" drop' #封单个IP


关闭主机防火墙 systemctl stop firewalld.service   
  1. 7层 应用层 基于nginx封禁
  • 黑名单:先拒绝,再允许所有

vim /etc/nginx/conf.d/ image.png

  • 白名单:先允许单个,再拒绝所有

image.png
设置完重启

nginx的403状态码:

1:没有首页
2:被deny拉黑了
3:文件没有读权限

nginx location优先级

location 地方 位置 可以在站点或站点内的目录设置

image.png
image.png
匹配

  	#没有符号,代表模糊匹配,不支持正则 location /te可以匹配/te开头的目录和文件
~		#表示执行一个正则匹配,区分大小写
~*	#表示执行一个正则匹配,不区分大小写
=		#针对的是文件,精准匹配,不支持正则

image.png
匹配优先级

符号优先级 =  大于 ~ 大于 ~* 大于 无符号

touch jpg
#配置文件例子:
	location  /jpg {
	return 500;
	}
	location ~ /jpg {
	return 504;
	}
	location ~* /Jpg{
	return 503;
	}
作业

image.png

nginx常用变量

变量说明
$args请求中的参数,如ww.123.com/1.php?a=1&b=2的
$args就是a=1&b=2
$content_lengthHTTP响应信息里的"Content-Length”
$conten_typeHTTP响应信息里的"Content-Type”
$document_rootnginx虚拟主机配置文件中的root参数对应的值
配置文件中的站点根目录
$document_uri当前请求中不包含指令的URl, 如w.123.com/1.php?a=1&b=2的
$document uri就是1.php,不包含后面的参数
$host主机头,也就是域名
$http_user_agent客户端的详细信息,也就是浏览器的标识,用cul-A可以指定
$http_cookie客户端的cookie信息
$limit rate如果nginx服务器使用llimit rate配置了显示网络速率,则会显示,
如果没有设置则显示0
$remote addr客户端的公网ip
$remote_port客户端的port
$remote user如果nginxi有配置认证,该变量代表客户端认证的用户名
$request_body_file做反向代理时发给后端服务器的本地资源的名称
$request_methodhttp请求方法,GET/POST/PUT/DELETE等
$request_filename当前请求的资源文件的路径名称,相当于是 d o c u m e n t r o o t / document_root/ documentroot/document_uri的组合
$request_uri请求的链接,包括 d o c u m e n t u r i 和 document_uri和 documenturiargs
$scheme请求的协议,如ftp,http,https
$server_protocol客户端请求资源使用的协议的版本,如HTTP/1.0,
HTTP/1.1,HTTP/2.0等
$uri和$document uri相同
$http_referer客户端请求时的referer,通俗讲就是该请求是通过哪个
链接跳过来的,用curl-e可以指定
location / {
		if($host!='192.168.18.25'){
				return 302 http://192.168.18.25/gun.htm1;
    }
}

重点变量

host 				#http请求头的host域名
referer			#从哪一个ur1跳转过来的
user_agent	#用户的浏览器客户端信息
Connection	#是否为长链接
remote_addr	#客户端的ip
status			#http的状态码

nginx防盗链

在html 里面资源设置为连接如

则请求的时被人服务器的流量,referer会显示来自别人的网站,称为“盗链”

image.png
image.png
=太严格
image.png

nginx中英文自动匹配

server {
		listen 80;
		server_name ryuyan.com;
		index index.htm index.htm1;
		location / {
				if ($http_accept_language ~* ^en ){
						root /htm1/1ang/en;
				}
				root /html/lang/cn;
			}
}

nginx + php 动态网站

前面的页面全是静态的,手写的HTML
只有nginx,只支持静态网站
nginx+php:支持动态网站

#安装php fpm
yum install php-fpm php-mbstring php-mysqInd php-gd -y
vim /etc/php-fpm.d/www.conf
#修改39 41 行的用户和用户组
user = nginx
group = nginx

systemctl start php-fpm.service
systemctl enable php-fpm.service

image.png

#nginx.连接php-fpm
server {
	listen 80;
	server_name kod.com;
	location / {
		root /html/kod;
		index index.php index.html index.htm;
	}
	location ~ \.php$ {
        	root 						/html/kod;	
					fastcgi_pass		127.0.0.1:9000;
					fastcgi_index		index.php;
					fastcgi_param		SCRIPT_FILENAME		/html/kod$fastcgi_script_name;
					include					fastcgi_params;
  }
}

cookie和session

http协议:传输工具 通讯
http请求头
http响应头

http传输数据,不能记住用户的状态 不能识别用户
为了记住用户的状态,浏览器记住状态 服务端记住状态
cookie存储在客户机浏览器 session_id 以及其他信息
session存储在服务器

lnmp网站架构(平台)

linux+nginx+mysql+php Inmp架构
linux+apache+mysql+php lamp架构
linux+nginx+mysql+tomcat Inmt java架构
linux+nginx+mysql+uwsgi python架构

操作系统+软件+开发语言
linux +nginx+php+mysql php lnmp
linux +apache+php+mysql php lamp
windows +apache+php+mysql php wamp
linux +nginx+tomcat+mysql java lnmt

mysql

安装数据库
yum install mariadb-server mariadb -y   #mariadb几乎和MySQL相同 是cs架构 有服务端有客户端
systemctl start mariadb   #开启服务
systemctl enable mariadb 	#设置开机自启

数据库操作

#登录数据库
mysql
#查看数据库
show databases;
#创建数据库
create database wordpress;
#删除数据库
drop database wordpress;
#切换数据库
use mysql
#查看表
show tables;
#删除表
drop table xxxxx;
#查看所有数据
select user,host,Password from user;
#授权
grant all on wordpress.* to wordpress@'10.0.0.%' identified by '123456';
             数据库名字       用户名字   自己的网段                 密码        
#使用普通用户登录
mysql -uwordpress -p123456 -h 10.0.0.100 指定哪台服务器的IP地址
#安全初始化mysql
mysq1_secure_installation
回车,n,一路y

mariadb c/s架构
mariadb客户端 mariadb-server服务端
远程登陆:mysql -uroot -p123456 -h 192.168.19.100

安装wordpress

增加nginx的站点配置文件

cd /etc/nginx/conf.d/
cd kod.com.conf wp.com.conf
vim wp.com.conf

image.png

nginx -t   #检查语法
systemctl restart nginx.service

mkdir /html/wp   #创建网站根目录

上传wordpress源代码

上传文件
tar zcxf WordPress-4.6.tar.gz   
mv WordPress-4.6/* .   #将下一层文件移到本层

chown -R nginx:nginx .   #授权

更改hosts文件

image.png
浏览器访问安装

安装discuz

吾爱破解 远景论坛 恩山论坛(路由器论坛)电子发烧友论坛

#操作数据库
create database discuz;
grant all on discuz.* to discuz@'192.168.20.%' identified by '123456';

配置nginx

image.png
步骤与wordpress一样

伪静态

网站类型:
静态网站 不能交互安全,速度非常快,搜索引擎优化SE0 方便 容易被搜到
动态网站 能交互,不安全,速度慢,不方便$E0
伪静态网 能交互,不安全,速度最慢,方便SE0

image.png

纯静态网站  	最快			搜索引擎收录,很好
动态网站			稍微慢点		搜索引擎收录,很差
伪静态				最慢			搜索引擎收录,很好

开启伪静态:
1.网站后台设置,打开伪静态开关
2.修改nginx配置

nginx 的正向代理

代理:(中间人)
正向代理
反向代理
配置在客户端的代理叫正向代理,在服务端的代理叫反向代理
image.png

image.png
image.png

作业

image.png

nginx的反向代理

[root@lb01~]#cat /etc/nginx/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 / {
						proxy_pass http://10.0.0.7;   #反向代理
						proxy_http_version 1.1;
						proxy_set_header Host $host;   #设置代理请求头
						proxy_set_header X-Real-IP $remote_addr; #定义用户的真实IP
						proxy_set_header X-Forwarded-For $remote_addr;
					}
			}
}

image.png
image.png

后端服务器日志格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" $http_host '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

腾讯云部署业务

1购买云主机(云服务器),部署项目
2.注册域名
3.备案
4.添加域名解析

云主机云服务器 需要联接互联网才能使用的,就是云
云提供商:拉新 亚马逊云阿里云腾讯云百度云京东云美团云天翼云沃通云中国移动云···

网站简单架构

大型网站,一般都是有由很多台服务器组成的一个集群

image.png

http状态码

200 ok 请求正常
206 数据分次传输

301 永久跳转 会被浏览器记住(不建议使用)
302 临时跳转 不会被浏览器记住 (建议使用)
304 读本地缓存
307 == 302
308 == 301

400 错误请求 http协议格式不对
401 身份验证失败
403 deny;文件没有读权限;没有首页
404 文件找不到

500 服务器错误,代码执行错误;
502 代理后面的服务器连不上;
504 超时

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值