文章目录
第四章:web服务器
www简介
- Web网络服务也叫WWW(World Wide Web 全球信息广播)万维网服务,一般是指能够让用户通过浏览器访问到互联网中文档等资源的服务
- Web 网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的请求后才会响应,最终用于提供服务程序的 Web 服务器会通过 HTTP(超文本传输协议)或HTTPS(安全超文本传输协议)把请求的内容传送给用户,如图:
常见Web服务程序介绍:
- Windows系统中默认Web服务程序是I I S(Internet Information Services),这是一款图形化的网站管理工具,IIS程序不光能提供Web网站服务,还能够提供FTP、NMTP、SMTP等服务功能,但只能在Windows系统中使用
- 2004 年 10 月 4 日,为俄罗斯知名门户站点而开发的 Web 服务程序 Nginx 横空出世。Nginx程序作为一款轻量级的网站服务软件,因其稳定性和丰富的功能而快速占领服务器市场,但Nginx 最被认可的还当是低系统资源占用、内存少且并发能力强,因此得到了国内诸如新浪、网易、腾讯等门户站的青睐
- Apache——取自美国印第安人土著语Apache,寓意着拥有高超的作战策略和无穷的耐性,由于其跨平台和安全性广泛被认可且拥有快速、可靠、简单的API扩展。目前拥有很高的Web服务软件市场占用率,全球使用最多的Web服务软件,开源、跨平台(可运行于Unix,linux,windows中)
- Tomcat——属于轻量级的Web服务软件,一般用于开发和调试JSP代码,通常认为Tomcat是Apache的扩展程序
服务器主机
- 网站是由域名、网页源程序和主机空间组成的,其中主机空间则是用于存放网页源代码并能够将网页内容展示给用户,虽然本小节与Apache服务没有直接关系,但如果您想要在互联网中搭建网站并被顺利访问,主机空间一定不能选错
-
虚拟主机:在一台服务器中分出一定的磁盘空间供用户放置网站、存放数据等,仅提供基础的网站访问、数据存放与传输流量功能,能够极大的降低用户费用,也几乎不需要管理员维护除网站数据以外的服务,适合小型网站
-
VPS(Virtual Private Server):在一台服务器中利用OpenVZ、Xen或KVM等虚拟化技术模拟出多个“主机”,每个主机都有独立的IP地址、操作系统,实现不同VPS之间磁盘空间、内存、CPU资源、进程与系统配置间的完全隔离,管理员可自由使用分配到的主机中的所有资源,所以需要有一定的维护系统的能力,适合小型网站
-
云服务器(ECS):是一种整合了计算、存储、网络,能够做到弹性伸缩的计算服务,其使用起来与VPS几乎一样,但差别是云服务器建立在一组集群服务器中,每个服务器都会保存一个主机的镜像(备份),大大的提升了安全稳定性,另外还具备了灵活性与扩展性,用户只需按使用量付费的即可,适合大中小型网站。
-
独立服务器:这台服务器仅提供给您使用,详细来讲又可以区分为租用方式与托管方式
- 租用方式:用户只需将硬件配置要求告知IDC服务商,服务器硬件设备由机房负责维护,运维管理员一般需要自行安装相应的软件并部署网站服务,租期可以为月、季、年,减轻了用户初期对硬件设备的投入,适合大中型网站。
- 托管方式:用户需要自行购置服务器后交给IDC服务供应商的机房进行管理(缴纳管理服务费用),用户对服务器硬件配置有完全的控制权,自主性强,但需要自行维护、修理服务器硬件设备,适合大中型网站。
- 另外有必要提醒,选择主机空间供应商时请一定要注意看口碑,综合分析再决定购买,某些供应商会有限制功能、强制添加广告、隐藏扣费或强制扣费等恶劣行为,一定一定不要上当!
主要数据
- 服务器所提供的最主要数据是超文本标记语言(Hyper Text Markup Language,HTML)、多媒体文件(图片、影像、声音、文字等,都属于多媒体或称为超媒体),HTML只是一些纯文本数据,通过所谓的标记来规范所要显示的数据格式
浏览器
- 客户端收到服务器的数据之后需要软件解析服务器所提供的数据,最后将效果呈现在用户的屏幕上。
- 那么著名的浏览器就有内建在Windows操作系统内的IE浏览器(淘汰)和Microsoft Edge,还有Firefox浏览器和Google的chrome浏览器
网址及HTTP简介
- web服务器提供的这些数据大部分都是文件,那么我们需要在服务器端先将数据文件写好,并且放置在某个特殊的目录下面,这个目录就是我们整个网站的首页,在redhat中,这个目录默认在 /var/www/html 。
- 浏览器是通过你在地址栏中输入你所需要的网址来取得这个目录的数据的
URL
-
Uniform Resource Locator,统一资源定位符,对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址
-
网址格式:
<协议>://<主机或主机名>[:port]/<目录资源,路径>/资源文件名
-
协议::http、https、ftp等
-
主机地址或者主机名:主机地址就是服务器在因特网所在的IP地址。如果是主机名的话,那么就需要域名解析了
-
端口号(port):http为80,https为443 (IANA:互联网数字分配机构)
- 0-1023:永久地分配给固定的应用程序使用
- 1024-41951:注册端口,但要求不是特别严格,分配给程序注册为某应用使用
- 41952-60000:客户端程序随机使用的端口,动态端口,或私有端口
HTTP请求方法:
工作机制:
在http通信中,每个http请求报文都包含一个方法,用以告诉web服务器端需要执行哪些具体的动作,这些动作包括:获取指定web页面、提交内容到服务器、删除服务器上资源文件等。
- 状态代码:由三位数字组成,第一个数字定义了响应的类别,且有五种可能取值
- 1xx:指示信息 —— 表示请求已接收,继续处理
- 2xx:成功 —— 表示请求已被成功接收、理解、接
- 3xx:重定向 —— 要完成请求必须进行更进一步的操作
- 4xx:客户端错误 —— 请求有语法错误或请求无法实现
- 5xx:服务器端错误 —— 服务器未能实现合法的请求
- 常见状态代码、状态描述的说明如下:
- 200 OK:客户端请求成功
- 400 Bad Request:客户端请求有语法错误,不能被服务器所理解
- 401 Unauthorized:请求未经授权,这个状态代码必须和 WWW-Authenticate 报头域一起使用
- 403 Forbidden:服务器收到请求,但是拒绝提供服务
- 404 Not Found:请求资源不存在,举个例子:输入了错误的URL
- 500 Internal Server Error:服务器发生不可预期的错误
- 503 Server Unavailable:服务器当前不能处理客户端的请求,一段时间后可能恢复正常
HTTP协议请求的工作流程:
特点
www服务器的类型
静态网站
仅提供用户浏览的单向静态网页,单纯是由服务器单向提供数据给客户端,Server不需要与client端有互动,可以浏览网站,但是无法数据上传。
动态网站
- 该站可以让服务器与用户互动,常见的例如留言板,博客。这种类型的网站需要通过“网页程序语言”来实现与用户互动的行为。常见的例如:PHP网页程序语言,配合数据库系统来进行数据的读、写。当你在向服务器请求数据时,其实是通过服务器端同一个网页程序在负责将数据读出或写入数据库,变动的是数据库的内容,网页程序并没有任何改变。
- 另外一种交互式的动态网页主要是在客户端实现。服务端将可执行的程序代码(JavaScript)传送给客户端,客户端的浏览器如果提供JavaScript的功能,那么该程序就可以在客户端的计算机上面工作了;另外一种可在客户端执行的就是flash动画格式,在这种动画格式内还可以进行程序设计
静态页面资源特点
1. 处理文件类型:如.html、jpg、.gif、.mp4、.swf、.avi、.wmv、.flv等-
2. 地址中不含有问号"?"或&等特殊符号。.htm .html
3. 保存在网站服务器文件系统上的,是实实在在保存在服务器上的文件实体
4. 页内容是固定不变的,因此,容易被搜索引擎收录
5. 网页页面交互性交差,因为不能与数据库配合
6. 网页程序在用户浏览器端解析,当客户端向服务器请求数据时,服务器会直接从磁盘文件系统上返回数据(不做任何解析)
优势:
1. 访问的效率比较高
2. 网页内容是固定不变的,因此,容易被搜索引擎收录
3. 网页程序在用户浏览器端解析,当客户端向服务器请求数据时,服务器会直接从磁盘文件系统上返回数据(不做任何解析)
劣势:
1. 网页页面交互性交差,因为不能与数据库配合
2. 保存在网站服务器文件系统上的,是实实在在保存在服务器上的文件实体
动态网页资源
所谓的动态网页是与静态网页相对而言的,也就是说,动态网页的URL后缀不是.html .htm、.xml、.shtml、.js css 等静态网页的常见扩展名形式,而是.asp、.aspx、.php、.js、.do、.cgi等形式
请求响应信息,发给事务端进行处理,由服务端处理完成,将信息返回给客户端,生成的页面称为动态页面
动态网页资源特点
1. 网觅扩展名后缀常见为:.asp、.aspx、.php、.js、.do、.cgi
2. 网页页面交互性强,可以与数据库配合
3. 地址中含有问号"?"或 & 等特殊符号
4. 不便于被搜索引擎收录
优势:
1.客户端与服务端交互能力强
劣势:
1.访问的效率比较低
2.不便于被搜索引擎收录
http 80 8080(默认端口)
Apache: http服务器是一个模块化的服务器,可以运行几乎所有广泛使用的计算机平台上。属于应用服务器。Apache支持模块多,性能稳定,apache本身是静态解析,但也可以通过扩展脚本、模块等支持动态页面。(apache可以支持phpcgiperl,但是使用java的话需要tomcat在apache后台支持,将java请求由apache转给tomcat处理)
tomcat:应用(java)服务器,他只是一个servlet容器,可以认为是apache的扩展,但是可以独立用于apache运行。
nginx:相对一Apache占用更少的内存及资源一个轻量级服务器,是一个高性能的http和反向代理服务器,同时也是一个IMPA/POP3/SMTP代理服务器。
web服务器基本配置
[root@localhost nginx]# tree /etc/nginx
/etc/nginx
├── conf.d #辅助配置(子配置文件)目录
├── default.d
├── fastcgi.conf #php界面解析通用网关接口配置
├── fastcgi.conf.default
├── fastcgi_params #用以翻译nginx的变量供php识别
├── fastcgi_params.default
├── koi-utf
├── koi-win
├── mime.types #用以配置文件的媒体文件类型
├── mime.types.default
├── nginx.conf #主配置文件
├── nginx.conf.default
├── scgi_params
├── scgi_params.default
├── uwsgi_params #用以配置nginx的变量供python识别
├── uwsgi_params.default
└── win-utf
#/usr/share/nginx/html #默认的nginx网站根目录
#/var/log/nginx #nginx的日志文件目录
#nginx服务配置文件nginx.conf的结构
#############全局配置(无{}标志)############
user nginx; #进程所属用户
worker_processes auto; #线程数量CPU核心数,(双核4线程,可以设置为4)
error_log /var/log/nginx/error.log; #错误日志文件路径
pid /run/nginx.pid; #nginx pid文件位置
include /usr/share/nginx/modules/*.conf; #导入功能模块配置文件
#######################################################
##################性能配置(有{}标志)############
events {
worker_connections 1024; #tcp连接数
}
########################################################
##################http模块配置(有{}标志)############
http { #http区块开始 132
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 /var/log/nginx/access.log main; #访问日志文件位置
sendfile on; #开启高效文件传输
tcp_nopush on; #性能优化参数
tcp_nodelay on; #性能优化参数
keepalive_timeout 65; #持久连接或超时时间
types_hash_max_size 4096; #性能优化参数
include /etc/nginx/mime.types; #可解析的静态资源类型
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf; #加载子配置文件
server { #server区块开始,就相当于一个虚拟主机
listen 80;
listen [::]:80;
server_name _; #服务名
root /usr/share/nginx/html; #主页存放主路径(/)
include /etc/nginx/default.d/*.conf; #子配置文件路径
location / { #定义URI /匹配符匹配的是root设置的URI路径
root html;
index index.html index.htm; #链接的网页文件
}
error_page 404 /404.html; #404错误的返回页面
location = /404.html { #location 定义用户请求的uri,并返回相应的资源文件
}
error_page 500 502 503 504 /50x.html; #5xx状态返回的页面
location = /50x.html {
}
}
}
.......
uri中的斜线,如:
-
location /test { # 尾部无/ ... } location /test/ { ... }
-
不带 / 当访问 http://www.nginx-test.com/test 时, Nginx 先找是否有 test 目录,如果有则找 test 目录下的 index.html ;如果没有 test 目录, nginx则会找是否有 test 文件。
-
带 / 当访问 http://www.nginx-test.com/test 时,只是查找 test 目录下的 index.html 。
-
server_name指令一般用于指定虚拟主机的域名,在匹配时有以下四中写法
- 精确匹配:server_name http://www.nginx.com ;
- 左侧匹配:server_name *.http://nginx.com ;
- 右侧匹配:server_name www.nginx.* ;
- 正则匹配:server_name ~^www.nginx.*$ ;
- 注:匹配优先级:精确匹配 > 左侧通配符匹配 > 右侧通配符匹配 > 正则表达式匹配
-
root指令与alias指令区别
-
root指令用于静态资源目录位置,它可以写在 http 、 server 、 location 等配置中,root指令定义的路径会与 URI 叠加产生最终访问路径,如:
-
location /image { root /opt/nginx/static; } # 当用户访问 www.test.com/image/1.png 时,实际在服务器找的路径是 /opt/nginx/static/image/1.png
-
alias也是指定静态资源目录位置,但只能访问指定的绝对路径,不会叠加目录,只能写在 location 中且末尾一定要添加 / , 如:
-
location /image { alias /opt/nginx/static/image/; } #当用户访问 www.test.com/image/1.png 时,实际在服务器找的路径是 /opt/nginx/static/image/1.png
-
-
location匹配路径
-
格式:
-
location [ = | ~ | ~* | ^~ ] uri { ... }
-
匹配规则:
- = 精确匹配;
- ~ 正则匹配,区分大小写;
- ~* 正则匹配,不区分大小写;
- ^~ 匹配以某个字符串开头,不是正则匹配;
- / 通用匹配,如果没有其它匹配,任何请求都会匹配到
- 注:匹配优先级:= > ^~ > ~ > ~* > 不带任何字符。
-
示例
-
server { listen 80; server_name www.nginx-test.com; # 只有当访问 www.nginx-test.com/match_all/ 时才会匹配到/usr/share/nginx/html/match_all/index.html location = /match_all/ { root /usr/share/nginx/html index index.html } # 当访问 www.nginx-test.com/1.jpg 等路径时会去 /usr/share/nginx/images/1.jpg 找对应的资源 location ~ \.(jpeg|jpg|png|svg)$ { root /usr/share/nginx/images; } # 当访问 www.nginx-test.com/bbs/ 时会匹配上 /usr/share/nginx/html/bbs/index.html location ^~ /bbs/ { root /usr/share/nginx/html; index index.html index.htm; } } # 注意: location / { root html; index index.html index.htm; } # 其/不是根目录而是统统都匹配到指定路径,而指定路径为html ,即nginx的默认网页目录/usr/share/nginx/html
-
nginx.conf 配置文件的语法规则
- 配置文件由指令与指令块构成
- 每条指令以 “;” 分号结尾,指令与参数间以空格符号分隔
- 指令块以 {} 大括号将多条指令组织在一起
- include 语句允许组合多个配置文件以提升可维护性
- 通过 # 符号添加注释,提高可读性
- 通过 $ 符号使用变量
- 部分指令的参数支持正则表达式,例如常用的 location 指令
测试案例:
实现在自定义路径/www下网页内容访问。
服务器:
#systemctl stop firewalld
#setenforce 0
#dnf install nginx
#vim /etc/nginx/nginx.conf
server {
.....
root /www;
....
}
#mkdir /www
#echo helloworld > /www/index.html
#systemctl restart nginx
客户端
浏览器: http://192.168.235.128
安装nginx软件以及通过nginx服务实现自定义网址内容:
[root@localhost ~]# mount /dev/sr1 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@localhost ~]# dnf install nginx -y # 下载nginx
[root@localhost ~]# rpm -ql nginx
[root@localhost ~]# cd /etc/nginx/
[root@localhost nginx]# ll
total 68
drwxr-xr-x. 2 root root 6 Oct 11 2023 conf.d
drwxr-xr-x. 2 root root 6 Oct 11 2023 default.d
-rw-r--r--. 1 root root 1077 Oct 11 2023 fastcgi.conf
-rw-r--r--. 1 root root 1077 Oct 11 2023 fastcgi.conf.default
....
-rw-r--r--. 1 root root 2334 Oct 11 2023 nginx.conf
-rw-r--r--. 1 root root 2656 Oct 11 2023 nginx.conf.default
...
[root@localhost ~]# vim /etc/nginx/nginx.conf
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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 /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
......
[root@localhost ~]# ll /usr/share/nginx/html/
total 12
-rw-r--r--. 1 root root 3971 Oct 11 2023 404.html
-rw-r--r--. 1 root root 4020 Oct 11 2023 50x.html
drwxr-xr-x. 2 root root 27 Oct 19 14:46 icons
lrwxrwxrwx. 1 root root 25 Oct 11 2023 index.html -> ../../testpage/index.html
-rw-r--r--. 1 root root 368 Oct 11 2023 nginx-logo.png
lrwxrwxrwx. 1 root root 14 Oct 11 2023 poweredby.png -> nginx-logo.png
lrwxrwxrwx. 1 root root 37 Oct 11 2023 system_noindex_logo.png -> ../../pixmaps/system-noindex-logo.png
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# # selinux关闭
测试:
[root@localhost ~]# vim /etc/nginx/nginx.conf
server {
........
# root /usr/share/nginx/html;
root /www;
.......
[root@localhost ~]# mkdir /www
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# vim /www/index.html
hello,world!
[root@localhost ~]# echo 123 > /www/123.html
[root@localhost ~]# ll /www
total 8
-rw-r--r--. 1 root root 4 Oct 19 15:39 123.html
-rw-r--r--. 1 root root 13 Oct 19 15:38 index.html
然后在浏览器输入网址即可看见效果。
[root@localhost ~]# journalctl -xeu nginx.service # 查看日志
添加网卡:
第一种方式:
[root@localhost ~]# nmcli connection modify ens160 ipv4.method manual ipv4.addresses 192.168.235.128/24 +ipv4.addresses 192.168.235.200/24 +ipv4.addresses 192.168.235.100/24 ipv4.gateway 192.168.235.2 ipv4.dns 192.168.235.2
[root@localhost ~]# nmcli connection up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@localhost ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:25:92:58 brd ff:ff:ff:ff:ff:ff
altname enp3s0
inet 192.168.235.128/24 brd 192.168.235.255 scope global noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.235.200/24 brd 192.168.235.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
inet 192.168.235.100/24 brd 192.168.235.255 scope global secondary noprefixroute ens160
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe25:9258/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]#
实验一:多IP访问多网段实验
https://blog.youkuaiyun.com/m0_75008371/article/details/143091704?fromshare=blogdetail&sharetype=blogdetail&sharerId=143091704&sharerefer=PC&sharesource=m0_75008371&sharefrom=from_link
实验二:多端口访问多网段
# 添加新的IP并激活ens160
[root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.235.10/24
[root@localhost ~]# nmcli connection up ens160
# 创建并编辑执行文件
[root@localhost ~]# vim /etc/nginx/conf.d/test_port.conf
server {
listen 192.168.235.10:8909;
root /test/8909;
location / {
index index.html;
}
}
server {
listen 192.168.235.10:10000;
root /test/10000;
location / {
index index.html;
}
}
# 创建多级目录并添加内容
[root@localhost ~]# mkdir /test/{8909,10000} -pv
[root@localhost ~]# echo this is 8909 > /test/8909/index.html
[root@localhost ~]# echo this is 10000 > /test/10000/index.html
# 重启nginx服务
[root@localhost ~]# systemctl restart nginx.service
# 查看各端口内容
[root@localhost ~]# curl 192.168.235.10:8909
this ih 8909
[root@localhost ~]# curl 192.168.235.10:10000
this ih 10000
实验三:多域名
# 添加IP(与实验二共用一个IP,但效果不同)并激活ens160服务
[root@localhost ~]# nmcli connection modify ens160 +ipv4.addresses 192.168.235.10/24
[root@localhost ~]# nmcli connection up ens160
# 添加多域名
[root@localhost ~]# vim /etc/hosts
192.168.235.10 www.haha.com www.xixi.com
# 创建并编辑执行文件
[root@localhost ~]# vim /etc/nginx/conf.d/test_name.conf
server {
listen 192.168.235.10:80;
server_name www.haha.com;
root /test/haha;
location / {
index index.html;
}
}
server {
listen 192.168.235.10:80;
server_name www.xixi.com;
root /test/xixi;
location / {
index index.html;
}
}
# 创建多级目录
[root@localhost ~]# mkdir /test/{haha,xixi} -pv
mkdir: created directory '/test/haha'
mkdir: created directory '/test/xixi'
# 设置内容
[root@localhost ~]# echo this is haha > /test/haha/index.html
[root@localhost ~]# echo this is xixi > /test/xixi/index.html
# 重启nginx服务
[root@localhost ~]# systemctl restart nginx.service
# 查看各域名内容
[root@localhost ~]# curl www.haha.com
this is haha
[root@localhost ~]# curl www.xixi.com
this is xixi
在windows主机上设置多域名:
通过win+R打开命令运行框,输入drivers,找到目录下的etc文件,通过编辑ect文件下的hosts(使用普通的文本方式是没有编辑权限的,可以使用Notepad++或其他无视打开权限的软件)文件对域名进行添加或删除。
客户端测试windows:C:\Windows\System32\drivers\etc 编辑hosts文件添加本地域名解析信息
通过浏览器http://www.node1.com
linux客户端测试:[root@node1 ~]# vim /etc/hosts
追加写入
192.168.10.129 www.node1.com www.node2.com
[root@node1 ~]# curl www.node1.com
node1
实验四:虚拟目录和用户控制
虚拟目录
[root@localhost ~]# vim /etc/nginx/conf.d/test_name.conf
server {
listen 192.168.235.10:80;
server_name www.xixi.com;
root /test/xixi;
location /real/ {
alias /virt/; # http://www.xixi.com/real/ ---> http://www.xixi.com/virt
index index.html;
}
}
alias:别名命令
格式:alias 别名 真实名称
[root@localhost ~]# mkdir /virt/
[root@localhost ~]# echo this is virt > /virt/index.html
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# curl www.xixi.com
this is xixi
[root@localhost ~]# curl www.xixi.com/real/
this is virt
用户认证
[root@localhost ~]# vim /etc/nginx/conf.d/test_name.conf
server {
listen 192.168.235.10:80;
server_name www.xixi.com;
root /test/xixi;
location /real/ { # http://www.xixi.com/real/
alias /virt/; # alias /real/虚拟路径 http://www.xixi.com/real/ ---> /virt/
index index.html;
auth_basic on;
auth_basic_user_file /etc/nginx/userfile;
}
}
[root@localhost ~]# mount /dev/sr1 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@localhost ~]# dnf install httpd-tools -y
[root@localhost ~]# dnf provides htpasswd
# 创建用户zhangsan
[root@localhost ~]# htpasswd -c /etc/nginx/userfile zhangsan
New password: (zhou020816)
Re-type new password:
Adding password for user zhangsan
[root@localhost ~]# systemctl restart nginx.service
nginx帮助:https://nginx.org/en/
使用cpolar实现内网穿透
- 使用nginx建立网站
- 部署文档:https://dashboard.cpolar.com/get-started
通过上面的网站获取cpolar内网穿透工具,然后注册自己的cpolar用户,为内网穿透做准备。
通过xftp和xshell连接安装cpolar工具:
[root@localhost ~]# ll # 查看cpolar安装压缩包
total 7256
-rw-r--r--. 1 root root 7423434 Oct 22 19:56 cpolar-stable-linux-amd64.zip
[root@localhost ~]# unzip cpolar-stable-linux-amd64.zip # 对cpolar压缩包解压缩
Archive: cpolar-stable-linux-amd64.zip
inflating: cpolar
[root@localhost ~]# ll
total 26132
-rwxr-xr-x. 1 root root 19328632 Mar 9 2023 cpolar # cpolar内网穿透工具
-rw-r--r--. 1 root root 7423434 Oct 22 19:56 cpolar-stable-linux-amd64.zip
[root@localhost ~]# ./cpolar authtoken N2FlMmUxZGEtMzc1YS00OWZhLTg3NGYtOTI4MWFiYmU4OGVl # 连接cpolar内网穿透工具的账户
Authtoken saved to configuration file: /root/.cpolar/cpolar.yml
[root@localhost ~]# ./cpolar http 80 # 对端口进行内网穿透
注意:在内网穿透过程中,只有上述命令被执行时,才能进行地址转换。
实验五:https/443
(1)https简介
超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息。HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂其中的信息,因此HTTP协议不适合传输一些敏感信息,比如信用卡号、密码等。为了解决HTTP协议的这一缺陷,需要使用另一种协议:安全套接字层超文本传输协议HTTPS。
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer 或 Hypertext Transfer Protocol Secure,超文本传输安全协议),是以安全为目标的HTTP通道。HTTPS并不是一个新协议,而是HTTP+SSL(TLS)。原本HTTP先和TCP(假定传输层是TCP协议)直接通信,而加了SSL后,就变成HTTP先和SSL通信,再由SSL和TCP通信,相当于SSL被嵌在了HTTP和TCP之间。
SSL 是“Secure Sockets Layer”的缩写,中文叫做“安全套接层”。它是在上世纪90年代中期,由网景公司设计的。到了1999年,SSL 应用广泛,已经成为互联网上的事实标准。IETF 就把SSL 标准化。标准化之后SSL被改为 TLS(Transport Layer Security传输层安全协议)。
SSL协议分为两层:
-
SSL握手协议(SSL Handshake Protocol):它建立在SSL记录协议之上,用于在实际的数据传输开始前,通讯双方进行身份认证、协商加密算法、交换加密密钥等。
-
SSL记录协议 (SSL Record Protocol):它建立在可靠的传输协议(如TCP)之上,为高层协议提供数据封装、压缩、加密等基本功能。
SSL协议提供的服务:
1)认证用户和服务器,确保数据发送到正确的客户机和服务器
2)加密数据以防止数据中途被窃取
3)维护数据的完整性,确保数据在传输过程中不被改变
(2)https协议加密所使用的算法
HASH是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值。Hash算法特别的地方在于它是一种单向算法,用户可以通过hash算法对目标信息生成一段特定长度的唯一hash值,却不能通过这个hash值重新获得目标信息。因此Hash算法常用在不可还原的密码存储、信息完整性校验等。
常见的HASH算法:MD2、MD4、MD5、HAVAL、SHA、SHA-1、HMAC、HMAC-MD5、HMACSHA1。
共享密钥加密(对称密钥加密):加密和解密使用相同密钥。
对称加密算法:DES、3DES、DESX、Blowfish、IDEA、RC4、RC5、RC6和AES。
公开密钥加密(非对称密钥加密):公开密钥加密使用一对非对称的密钥。一把叫做私有密钥,一把叫做公开密钥。私有密钥不能让其他任何人知道,而公开密钥则可以随意发布,任何人都可以获得。使用此加密方式,发送密文的一方使用公开密钥进行加密处理,对方收到被加密的信息后,再使用自己的私有密钥进行解密。利用这种方式,不需要发送用来解密的私有密钥,也不必担心密钥被攻击者窃听盗走。
常见的非对称加密算法:RSA、ECC(移动设备用)、Diffie-Hellman、El Gamal、DSA(数字签名用)。
但由于公开密钥比共享密钥要慢,所以我们就需要综合一下他们两者的优缺点,使他们共同使用,而这也是HTTPS采用的加密方式。在交换密钥阶段使用公开密钥加密方式,之后建立通信交换报文阶段则使用共享密钥加密方式。
如何证明公开密钥本身是货真价实的公开密钥?如,正准备和某台服务器建立公开密钥加密方式下的通信时,如何证明收到的公开密钥就是原本预想的那台服务器发行的公开密钥。或许在公开密钥传输过程中,真正的公开密钥已经被攻击者替换掉了。这个时候就需要第三方公证单位来帮忙啦。
对称加密
我的理解是: 首先我们需要协商使用什么算法(AES), 接着我们通过相同的密钥对数据进行加密即可实现:明文 + 密钥 ==> 密文 密文 + 密钥 ==> 明文。由此我们可以看出, 都使用相同的密钥,一旦密钥泄露, 那么数据相当于公开。 优点: 效率高。 改进:能不能提前把密钥加密,只能由服务端才能解密。
非对称加密
是网络通信的基石, 保证了非对称加密 密钥的安全。 首先, 非对称加密需要 私钥 和 公钥, 公钥加密的数据只能由私钥解开, 而私钥加密的数据只能由公钥解开。 公钥存储在客服端, 私钥存储在服务端, 永远不对外暴漏。
流程:首先客服端请求公钥, 服务端响应公钥, 之后客服端通过公钥加密数据传输, 服务端通过私钥解密获取信息。
存在的安全隐患: 我们的公钥是直接进行传输的, 那么黑客就可以得到我们的公钥从而获取信息。
非对称加密 + 对称加密
客服端请求公钥响应后, 客服端通过公钥可以加密一串随机数,作为以后信息传输对称加密的密钥, 而这串随机数也只能由服务端的私钥才能解析。
服务端通过私钥解析后, 通过对称加密对数据进行加密。
问题: 假如在客服端请求公钥的时候, 就被黑客拦截, 充当了服务器, 给了黑客的公钥给客服端, 此问题产生的主要原因是: 报文可能被篡改, 客服端完全相信服务端!
数字签名-----解决报文被篡改
(1). 能确定消息确实是由发送方签名并发出来的,因为别人假冒不了发送方的签名。
(2). 数字签名能确定消息的完整性 , 证明数据是否未被篡改过。
发送者将数据先于hash算法生成信息摘要, 然后用私钥加密生成数字签名, 一起发送给接收方。
接收方只有用发送者的公钥才能解密被加密的数据, 然后通过hash对原文参数一个摘要信息, 进行比对, 即可确认报文是否被篡改。
问题: 我们的公钥还是不安全的在网络中传输, 解决证书
证书(CA)—解决服务端身份问题
有专门的机构进行颁发, 而证书中包含了你的公钥, 同时包含数字签名
此后我们请求公钥改为请求证书, 如何保证不会得到一个假证书了! 我们CA机构在已把证书嵌套在了浏览器以及操作系统, 所以证书都不用经过网络自然不会出现假的。
1.请求连接,tcp三次握手 确认版本
2.秘钥套件的协商,开始身份认证(非对称算法)证书验证 协商对称算法[客户端验证服务器身份]
3.建立ssl 会话链接
4.加密会话交互
5.断开会话链接
TLS完整的通信流程
第一阶段:客户端端申请建立https连接
第二阶段:客户端和服务器确认加密版本,加密套件
1.浏览器向服务器发送一个clientHello的报文
客户端产生一个随机数Random(ClientRandom)
会话ID:第一次肯定为空
Cipher suite: 加密套件(秘钥交换,完整性校验算法,对称机密算法 ,算法列表)
2.服务端向客户端回复一个ServerHello(确认使用的TLS的版本,以及加密套件)
Random随机数:服务产生的(ServerRandom)
SessionID: 0
Cipher suite:确认使用加密套件
Compression method: null 不压缩
Extension: 扩展字段
第三阶段:证书发送验证 (客户端验证证书取出公钥,用公钥加密生成的对称秘钥发送个服务器通知改变加密信息传递)
3.服务器向客户端发送Certificate (服务器的身份证,证书由第三方权威机构颁发)
4.服务器向客户端发送serverkey Exchange(通过和客户端协商的密码算法套件发送Pubkey结合HD算法生成Premastersecret(最终加密的会话秘钥))
5.certificate Request 可选报文(认证可以是单向也可以是双向,一般都是单向客户端验证服务端身份,服务端验证客户端客户端也需要发送证书)
6.ServerHello Done 服务端发送握手信息完毕
7.客户端回复服务器报文Certificate 如果有第五阶段则提交客户端的证书进行认证
8.ClientKey Exchange 秘钥交换信息和步骤4类似
9.ChangeCiper Spec: 通知消息(消息改变通知,及后面消息要进行加密了)
10.Encrypted Handshake Messges 校验数据包的完整性MD5(hash)将数据包的值进行散列最后和服务端的散列值进行比较一致意味着沟通过程中没有第三者的介入
第四阶段:服务收到消息,用私钥解密取,确认对称秘钥通知客户端ssl通道建立完成
11.服务器向客户端回包:NewSession Ticket(建立会话票据,提供会话票据)
12.change cipher Spec: 改变密码通知加密发送信息
13.encrypted Handshake message (Finished)
第五阶段:客户端和服务端可以通过加密通道开始数据通信
Application Data(http)客户端和服务端可以通过加密通道开始数据通信
第六阶段:客户端断开连接
Encrypted Alert
秘钥交换的大概过程:
ClientHello客户端产生一个随机数C,Serverhello服务端产生一个随机数S--公开, 还需要一个随机数(加密)
serverhello过程中发送serverkeyexchange发送公钥pubkey给客户端
客户端通过clientkeychange发送通过pubkey加密的随机数Pre_master
服务端对客户端的发送的加密Pre_master进行解密
最终客户端和服务端各自掌握了三个随机数利用复杂的交换算法进行计算协商出彼此的对称秘钥
注:
TLS1.3 提供 1-RTT 的握手机制,还是以 ECDHE 密钥交换过程为例,握手过程如下。将客户端发送 ECDH 临时公钥的过程提前到 ClientHello ,同时删除了 ChangeCipherSpec 协议简化握手过程,使第一次握手时只需要1-RTT,来看具体的流程:
• 客户端发送 ClientHello 消息,该消息主要包括客户端支持的协议版本、DH密钥交换参数列表KeyShare;
• 服务端回复 ServerHello,包含选定的加密套件;发送证书给客户端;使用证书对应的私钥对握手消息签名,将结果发送给客户端;选用客户端提供的参数生成 ECDH 临时公钥,结合选定的 DH 参数计算出用于加密 HTTP 消息的共享密钥;服务端生成的临时公钥通过 KeyShare 消息发送给客户端;
• 客户端接收到 KeyShare 消息后,使用证书公钥进行签名验证,获取服务器端的 ECDH 临时公钥,生成会话所需要的共享密钥;
• 双方使用生成的共享密钥对消息加密传输,保证消息安全。
#key是私钥文件
#crt是由证书颁发机构(CA)签名后的证书,或者是开发者自签名的证书,包含证书持有人的信息,持有人的公钥,以及签署者的签名等信息
[root@www certs]# openssl req -utf8 -new -key jiami.key -x509 -days 100 -out jiami.crt
------------------------------------------------RHEL7--------------------------------
(第一种) [root@localhost certs]# make jiami.crt
-------------------------------------------------------------------------------------
!!!!!!!注意!!!!!!!!!!!!!!!!
(第二种) #openssl req -newkey rsa:4096 -nodes -sha256 -keyout haha.key -x509 -days 365 -out haha.crt
----------------------------------------------x509 key csr crt-----------------------
[root@www certs]# openssl genrsa -aes128 2048 > openlab.key
(第三种) #openssl req -utf8 -new -key openlab.key -x509 -days 365 -out openlab.crt
-------------------------------------------------------------------------------------
[root@node1 ~]# openssl genrsa -out /etc/pki/tls/private/openlab.key
[root@node1 ~]# openssl req -utf8 -new -key /etc/pki/tls/private/openlab.key -x509 -days 365 -out /etc/pki/tls/certs/openlab.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:86
State or Province Name (full name) []:xi'an
Locality Name (eg, city) [Default City]:shannxi
Organization Name (eg, company) [Default Company Ltd]:open
Organizational Unit Name (eg, section) []:ce
Common Name (eg, your name or your server's hostname) []:local
Email Address []:admin
[root@node1 ~]# cp /etc/nginx/conf.d/test_alias.conf /etc/nginx/conf.d/test_https.conf
[root@node1 ~]# vim /etc/nginx/conf.d/test_https.conf
[root@node1 ~]# cat /etc/nginx/conf.d/test_https.conf
server {
listen 192.168.10.129:443 ssl;
root /www/https;
ssl_certificate /etc/pki/tls/certs/openlab.crt;
ssl_certificate_key /etc/pki/tls/private/openlab.key;
location / {
}
}
[root@node1 ~]# mkdir /www/https
[root@node1 ~]# echo this is https > /www/https/index.html
[root@node1 ~]# openssl genrsa -out /etc/pki/tls/private/openlab.key
[root@node1 ~]# openssl req -utf8 -new -key /etc/pki/tls/private/openlab.key -x509 -days 365 -out /etc/pki/tls/certs/openlab.crt
[root@node1 ~]# systemctl restart nginx
[root@node1 ~]# curl https://192.168.10.129
curl: (60) SSL certificate problem: self-signed certificate
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
[root@node1 ~]# curl https://192.168.10.129 -k
this is https
x509的三种格式:
.key:私钥文件 .crs:未签名的证书文件 .crt:签名的证书文件
测试:
[root@localhost ~]# vim /etc/nginx/conf.d/test_https.conf
server {
listen 192.168.235.100:443 ssl http2;
root /test/https;
ssl_certificate "/etc/pki/tls/certs/openlab.crt"; ssl_certificate_key "/etc/pki/tls/private/openlab.key";
location / {
index index.html;
}
}
[root@localhost ~]# cd /etc/pki/tls/
[root@localhost tls]# cd /etc/pki/tls/private/
[root@localhost private]# openssl genrsa 2048 > openlab.key
[root@localhost private]# ll
total 4
-rw-r--r--. 1 root root 1704 Oct 23 20:40 openlab.key
[root@localhost private]# openssl req -utf8 -new -key openlab.key -x509 -days 365 -out ../certs/openlab.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:86
State or Province Name (full name) []:jiangsu
Locality Name (eg, city) [Default City]:nanjing
Organization Name (eg, company) [Default Company Ltd]:openlab
Organizational Unit Name (eg, section) []:rhce
Common Name (eg, your name or your server's hostname) []:zhou
Email Address []:admin@admin.com # 邮箱地址
[root@localhost private]# mkdir /test/https
[root@localhost private]# echo this is https > /test/https/index.html
[root@localhost private]# systemctl restart nginx.service
[root@localhost private]# curl https://192.168.235.100 # 不加-k会有证书问题
curl: (60) SSL certificate problem: self-signed certificate
More details here: https://curl.se/docs/sslcerts.html
curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the web page mentioned above.
[root@localhost private]# curl https://192.168.235.100 -k
this is https
实验六:动态网站LAMP lnmp
LAMP是指由Linux、Apache、MySQL(或MariaDB)和PHP(有时也指Perl或Python)组成的一组动态网站或服务器的开源软件,这些组件通常被用来建立Web应用平台。
一、LAMP各组件的作用
Linux:作为LAMP架构的基础,提供了稳定、安全的操作系统环境。Linux具有强大的网络功能、丰富的软件包和开源社区支持,非常适合作为Web服务器的操作系统。
Apache:是LAMP架构中的HTTP服务器,负责处理客户端的请求并返回相应的响应。Apache具有高度的可配置性和可扩展性,支持多种模块和插件,能够满足各种Web应用的需求。
MySQL/MariaDB:作为LAMP架构中的数据库管理系统,负责存储和管理Web应用的数据。MySQL/MariaDB具有高性能、可靠性和易用性等特点,支持各种数据操作和语言接口,方便开发者进行数据库设计和开发。
PHP/Perl/Python:是LAMP架构中的服务器端脚本语言,用于处理客户端请求并生成动态内容。这些脚本语言具有强大的功能和灵活性,支持各种数据操作、文件处理和网络通信等功能,能够方便地实现各种Web应用的业务逻辑。
二、动态网站的特点
内容实时更新:动态网站的内容可以根据数据库的变化而实时更新,无需手动刷新页面即可看到最新的内容。
高交互性:动态网站通常具有表单、搜索、登录等交互功能,用户可以通过这些功能与网站进行交互,实现数据的输入和输出。
个性化内容展示:动态网站可以根据用户的身份、喜好和行为等信息,为用户展示个性化的内容和服务。
三、LAMP架构在动态网站开发中的优势
成本低廉:LAMP架构中的各个组件都是开源的,无需支付高昂的许可费用,降低了开发成本。
易用性:LAMP架构中的各个组件都具有丰富的文档和社区支持,开发者可以轻松上手并进行开发。
安全性和稳定性:Linux操作系统具有强大的安全性和稳定性,Apache服务器也具有高度的可靠性和稳定性,能够保证Web应用的安全运行。
可定制性和可扩展性:LAMP架构中的各个组件都支持模块化和插件化,开发者可以根据需求进行定制和扩展,满足各种Web应用的需求。
四、LAMP动态网站的构建流程
安装Linux操作系统:选择合适的Linux发行版(如Ubuntu、CentOS等)并安装操作系统。
安装和配置Apache服务器:下载并安装Apache服务器,配置服务器参数(如端口号、虚拟主机等)以满足Web应用的需求。
安装和配置数据库:下载并安装MySQL/MariaDB数据库管理系统,创建数据库和表结构,并配置数据库参数以满足Web应用的需求。
安装和配置PHP/Perl/Python脚本语言:下载并安装PHP/Perl/Python脚本语言,配置脚本语言参数(如时区、扩展模块等)以满足Web应用的需求。
开发Web应用:使用HTML、CSS、JavaScript等前端技术开发Web页面的布局和样式;使用PHP/Perl/Python等后端技术开发Web应用的业务逻辑和数据库操作;将前端和后端代码整合在一起,形成一个完整的Web应用。
部署和测试Web应用:将Web应用部署到LAMP架构的服务器上,并进行测试以确保其正常运行和满足需求。
测试:
# 安装PHP
[root@localhost ~]# mount /dev/sr1 /mnt
mount: /mnt: WARNING: source write-protected, mounted read-only.
[root@localhost ~]# dnf install php -y
# 关闭防火墙
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# setenforce 0
# 配置相关php文件
[root@localhost ~]# vim /etc/nginx/conf.d/test_php.conf
[root@localhost ~]# cat /etc/nginx/conf.d/test_php.conf
server {
listen 192.168.235.128:9999;
root /test/php;
location / {
# index index.php;
fastcgi_pass unix:/run/php-fpm/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
# 创建对应的文件路径
[root@localhost ~]# mkdir /test/php -pv
[root@localhost ~]# echo "<?php phpinfo(); ?>" > /test/php/index.php
重启php服务:
[root@localhost ~]# systemctl restart nginx.service # 可以通过重启nginx服务,对php服务进行重启
[root@localhost ~]# systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
Loaded: loaded (/usr/lib/systemd/system/php-fpm.servic>
Active: active (running) since Sat 2024-10-26 09:36:12>
Main PID: 3521 (php-fpm)
Status: "Processes active: 0, idle: 5, Requests: 0, sl>
Tasks: 6 (limit: 10680)
Memory: 13.8M
CPU: 65ms
CGroup: /system.slice/php-fpm.service
├─3521 "php-fpm: master process (/etc/php-fpm.>
├─3524 "php-fpm: pool www"
├─3526 "php-fpm: pool www"
├─3527 "php-fpm: pool www"
├─3528 "php-fpm: pool www"
└─3529 "php-fpm: pool www"
Oct 26 09:36:12 localhost.localdomain systemd[1]: Starting >
Oct 26 09:36:12 localhost.localdomain systemd[1]: Started T>
lines 1-18/18 (END)
# php-fpm 默认端口9000
# 安装php的所有插件
[root@localhost ~]# dnf install php* -y
# 安装mariadb数据库
[root@localhost ~]# dnf install mariadb-server -y
Complete!
# 启动mariadb服务
[root@localhost ~]# systemctl restart mariadb.service
实验七:autoindex on
当不存在网页文件时实现文件共享
[root@localhost ~]# vim /etc/nginx/conf.d/test_https.conf
server {
listen 192.168.235.100:443 ssl http2;
root /test/https;
ssl_certificate "/etc/pki/tls/certs/openlab.crt";
ssl_certificate_key "/etc/pki/tls/private/openlab.key";
location /www {
autoindex on;
}
}
[root@localhost ~]# mkdir /test/https/www
[root@localhost ~]# touch /test/https/www/{1..10}
[root@localhost ~]# systemctl restart nginx.service
[root@localhost ~]# dnf install lrzsz -y # windows系统与linux系统交互软件