2024.7.29(web、nginx)

一、web基本概念和常识

■ Web:为用户提供的一种在互联网上浏览信息的服务,Web服务是动态的、可交互的、跨平台的和图形化的。

■ Web 服务为用户提供各种互联网服务,这些服务包括信息浏览服务,以及各种交互式服务,包括聊天、购物、学习等等内容。

■ Web 应用开发也经过了几代技术的不断发展,目前Web开发依然是最重要的开发内容之一。Web基础的技术包括超文本标记语言(HTML)和HTTP协议,HTML是一种呈现数据的方式(给人看的),而HTTP则是一组通信的标准(语法、语义、时许),可以简单的理解为HTTP携带HTML。

1、web应用:

1.网站(广义上的PC,手机app)

2. 浏览器(Browser):也称用户代理,web客户端,主要有IE、Edge、Chrome、Firefox、腾讯浏览器,360浏览器等。

3. web服务器(web server):也称HTTP服务器(HTTP server),主要有Nginx、Apache、Tomcat 等。

二、网站基础

1、域名

■ 域名是一个IP地址的“面具”,目的是便于记忆和访问一个或一组服务器的地址(网站,电子邮件,FTP等)。

2、域名解析

■ 本地HOSTS解析

■ DNS服务器解析

3、网站的基本概念

■ 网站、网页、主页;

■ HTTP、URL、HTML、超链接

4、web网站

■ web1.0(以编辑为特征)

■ web2.0(侧重用户交互)

5、动态页面与静态页面的差别

1. URL不同

■ 静态页面链接里没有“?”

■ 动态页面链接里包含“?”

6、域名格式

■ http://(协议头)www.bing.com(域名)/(URI资源:路径/文件名)

■ 示例:https://www.bilibili.com/v/game/?spm_id_from=333.851.b_Z

072696d6172794368616e6e656c4d656e75.21

[root@slv ~]# yum -y install httpd

[root@slv ~]# systemctl start httpd
[root@slv ~]# echo "我是一个网页" > /var/www/html/index.html

三、HTTP 协议

■ HTTP协议是超文本传输协议的缩写,英文是Hyper Text TransferProtocol。它是从WEB服务器传输超文本标记语言(HTML)到本地浏览器的传送协议。

1、HTTP 原理

■ HTTP是一个基于TCP/IP通信协议来传递数据的协议,传输的数据类型为HTML文件,图片文件,查询结果等。

■ HTTP协议一般用于B/S架构。浏览器作为HTTP客户端通过URL向HTTP服务端即web服务器发送所有请求,web服务器收到客户端请求后进行响应。

2、HTTP特点

1. http协议支持客户端/服务端模式,也是一种请求/响应模式的协议。

2. 简单快速:客户向服务器请求服务时,只需传送请求方法和路径。请求方法常用的有GET、HEAD、POST。

3. 灵活:HTTP允许传输任意类型的数据对象。传输的类型由Content-Type加以标记。除开可以响应字符串以外,还可以上传和下载二进制文件。

4. 无连接:限制每次连接只处理一个请求。服务器处理完请求,并收到客户的应答后,即断开连接,但是却不利于客户端与服务器保持会话连接,为了弥补这种不足,产生了两项记录http状态的技术,一个叫做Cookie,一个叫做Session。

5. 无状态:无状态是指协议对于事务处理没有记忆,后续处理需要前面的信息,则必须重传。

[root@slv html]# dd if=/dev/zero of=/var/www/html/a.txt bs=30M count=1

3、URI和URL的区别

1. HTTP使用统一资源标识符(Uniform Resource Identifiers,URI)来传输数据和建立连接。

■ URI:Uniform Resource Identifier 统一资源标识符

■ URL:Uniform Resource Location 统一资源定位符

2. URI 是用来标识一个具体的资源的,我们可以通过URI知道一个资源是什么,使用它就能够唯一地标记互联网上资源。

3. URL则是用来定位具体的资源的,标识了一个具体的资源位置。互联网上的每个文件都有一个唯一的。

4. URL,也就是我们俗称的网址,它实际上是URI的一个子集。

5. URI不仅包括URL,还包括URN(统一资源名称),它们之间的关系如下:

4、HTTP报文组成 

1. 客户端发出请求:

GET/index.php HTTP/1.5/ Host:zgod.cn

2. 客户端浏览器发送出来的请求格式:

■ GET:这个部分只声明了请求方式,除了get方式可能还有post 等方式。GET表示请求,POST表示邮寄。

■ /index.php:这里是一个URL,表示了我们要访问的资源是哪个。

■ HTTP/1.5/:这里表示的是客户端浏览器使用的协议版本是1.5。

■ Host:zgod.cn:这是请求是交给主机zgod.cn的。

 四、HTTPS 协议

HTTP⼀般是明⽂传输,很容易被攻击者窃取重要信息,鉴于 此,HTTPS应运⽽⽣。 HTTPS 的全称为 (HyperTextTransferProtocoloverSecureSocketLayer), HTTPS 和 HTTP 有很⼤的不同在于 HTTPS 是以安全为⽬标的 HTTP通道,在HTTP 的基础上通过传输加密和身份认证保证了 传输过程的安全性。

HTTPS 在 HTTP 的基础上增加了 SSL 层,也就是说 HTTPS=HTTP+SSL。

HTTP使⽤明⽂传播,有三⼤⻛险

(1)窃听⻛险(eavesdropping):第三⽅可以获知通信内容。

(2)篡改⻛险(tampering):第三⽅可以修改通信内容。

(3)冒充⻛险(pretending):第三⽅可以冒充他⼈身份参与通 信。

SSL/TLS协议是为了解决这三⼤⻛险⽽设计的,希望达到:

(1)所有信息都是加密传播,第三⽅⽆法窃听。

(2)具有校验机制,⼀旦被篡改,通信双⽅会⽴刻发现。

(3)配备身份证书,防⽌⽌身份被冒充。

查看主机的所有打开的端口

[root@slv html]# firewall-cmd --list-pa

永久开启80端口

[root@slv html]# firewall-cmd --zone=public --add-port=80/tcp --permanent

查看防火墙打开端口

[root@slv html]# firewall-cmd --list-all

五、搭建apache服务器

1、安装http服务

[root@slv ~]# yum -y install httpd

2、查看安装情况,以及安装的资源文件

[root@slv ~]# rpm -qa | grep httpd
[root@slv ~]# rpm -ql  httpd

3、配置文件

[root@slv ~]# ls /etc/httpd/


[root@slv ~]# vim /etc/httpd/conf/httpd.conf 

4、启动http服务

[root@slv ~]# systemctl start httpd.service 

5、查看端口

[root@slv ~]# netstat -lnput | grep http

6、检查防火墙

[root@slv ~]# systemctl status firewalld.service 

不能够直接停用防火墙,单独打开端口

[root@slv ~]# firewall-cmd --zone=public --add-service=http

打开服务器不会马上生效,需要重启或者重载服务

[root@slv ~]# firewall-cmd --reload

打开指定端口

[root@slv html]# firewall-cmd --zone=public --add-port=80/tcp --permanent

7.写网页

[root@slv ~]# vim /var/www/html/index.html

[root@slv ~]# mkdir /var/www/html/img/
[root@slv ~]# mv 1.jpg /var/www/html/img/
[root@slv ~]# cd /var/www/html/
[root@slv html]# ls


[root@slv img]# cd /var/www/html/img/
[root@slv img]# ls

六、nginx服务

1、下载

[root@git ~]# wget https://nginx.org/download/nginx-1.26.1.tar.gz

2、解压

[root@git ~]# tar -zxvf nginx-1.26.1.tar.gz 

3、移除之前安装的软件包

[root@git ~]# yum -y remove nginx

4、下载依赖包

[root@git ~]# yum -y install gcc gcc-c++

[root@git ~]# yum -y install make openssl-devel pcre-devel 

5、编译安装nginx

[root@git nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream

“configure”,编译安装前的预配置。
-- prefix=/usr/Local/nginx:指定软件的安装路径,注意不是安装包目录,而是软件要安装在哪。
-- yser=nginx:程序用户为nginx。
-- group=nginx:组为nginx。
-- with-http_ssl_module:提供HTTPS支持,ssl证书模块。
-- with-http_stub_status_module:获取nginx工作状态模块。
-- with-http_realip_module:获取真实客户端IP。
-- with-stream:启用TCP/UDP代理模块。

[root@git nginx-1.26.1]# make && make install

6、创建用户

[root@git nginx-1.26.1]# useradd -s /bin/nologin -M nginx

7、检查目录

[root@git nginx-1.26.1]# tree /usr/local/nginx/

[root@git nginx-1.26.1]# cd /usr/local/nginx/
[root@git nginx]# ls

#conf:存放nginx配置文件的目录
#html:存放索引文件也就是网站首页(站点根目录)的目录
#Logs:存放nginx日志的目录
#sbin:存放nginx可执行程序的目录

[root@git nginx]# ls conf/

■ fastcgi.conf.default: fastcgi.conf的原始备份文件。

■ fastcgi_params: fastcgi的参数文件。

■ nginx.conf: nginx默认主配置文件。

■fastcgi.conf: fastcgi 相关参数配置文件。

[root@git nginx]# ls html/     //网站根目录

8、启动

[root@git nginx]# ./sbin/nginx

[root@git nginx]# netstat -lnput | grep nginx

9、开放端口或服务

[root@git nginx]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@git nginx]# firewall-cmd --reload

10、浏览器访问

七、nginx配置文件

1、主配置文件nginx.conf

[root@git nginx]# vim conf/nginx.conf
[root@git nginx]# nginx
-bash: nginx: 未找到命令
[root@git nginx]# ls -l /usr/bin/nginx
[root@git nginx]# rm -rf /usr/bin/nginx
[root@git nginx]# ln -s /usr/local/nginx/sbin/nginx  /usr/bin/[root@git nginx]# ls -l /usr/bin/nginx
lrwxrwxrwx 1 root root 27 7月  29 16:18 /usr/bin/nginx -> /usr/local/nginx/sbin/nginx
[root@git nginx]# nginx
[root@git nginx]# nginx -s stop
[root@git nginx]# nginx
[root@git nginx]# netstat -lnput | grep nginx
之所以指令能在命令行使用,是因为在$PATH目录中能找到这个可执行文件或者是这个可执行文件的链接文件

2、启动和关闭nginx服务

./nginx  ./nginx -s reload

修改了配置文件后,重载nginx服务网

脚本 启动nginx服务

[root@git nginx]# vim ~/nginx.sh


[root@git nginx]# source ~/nginx.sh
nginx正在执行,或者80端口被占用

[root@git nginx]# nginx -s stop
[root@git nginx]# source ~/nginx.sh

3、systemctl控制nginx

[root@git nginx]# ls /usr/lib/systemd/system
[root@git nginx]# vim /usr/lib/systemd/system/nginx.service

[root@git nginx]# systemctl daemon-reload

[root@git nginx]# systemctl stop nginx
[root@git nginx]# systemctl restart nginx

4、添加监控模块

[root@git nginx]# vim /usr/local/nginx/conf/nginx.conf

[root@git nginx]# systemctl restart nginx

Active connections: 2    活动的连接数
server accepts handled requests   累计接受的连接数 累计处理的连接数 累计请求数
 4 4 3 
Reading: 0 Writing: 1 Waiting: 1  正在读取,写入和等待的连接数

八、nginx虚拟主机配置

■ 一个“location”相当于一个虚拟主机,也就是用户访问网站时,点击跳转的另一个页面。

■ location 内可以添加nginx各种功能模块。

1、配置多个虚拟主机

[root@tomcat nginx-1.26.1]# scp root@192.168.8.134:~/nginx-1.26.1.tar.gz ./
[root@tomcat nginx-1.26.1]# tar -zxvf nginx-1.26.1.tar.gz 

[root@tomcat ~]# ls
[root@tomcat ~]# cd nginx-1.26.1/
[root@tomcat nginx-1.26.1]# ls

[root@tomcat nginx-1.26.1]# yum -y install gcc gcc-c++ make openssl-devel pcre-devel

[root@tomcat nginx-1.26.1]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-stream 

[root@tomcat nginx-1.26.1]# make && make install
[root@tomcat nginx-1.26.1]# useradd -s /bin/nologin -M nginx
[root@tomcat nginx-1.26.1]# echo "我是后端服务" > /usr/local/nginx/html/index.html
[root@tomcat nginx-1.26.1]# firewall-cmd --zone=public --add-port=80/tcp --permanent
[root@tomcat nginx-1.26.1]# firewall-cmd --reload

[root@tomcat nginx-1.26.1]# /usr/local/nginx/sbin/nginx
[root@tomcat nginx-1.26.1]# vim /usr/local/nginx/conf/nginx.conf


[root@tomcat nginx-1.26.1]# /usr/local/nginx/sbin/nginx -s reload 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值