一、HTTP协议
(1)HTTP是⼀个基于TCP/IP通信协议来传递数据的协议,传输的数 据类型为HTML ⽂件,图⽚⽂件,查询结果等。 HTTP协议⼀般⽤于B/S架构。浏览器作为HTTP客户端通过URL 向HTTP服务端即web服务器发送所有请求,web服务器收到客 户端请求后进⾏响应。
(2)HTTP 状态码
2xx:成功,200成功、201已经创建 3xx:重定向,304未修改 4xx:请求错误,404未找到⽂件、408请求超时 5xx:服务器错,500服务器内部错误、502⽹关错误
二、Apache服务
(1)概述
最早的 web 服务程序,基于 http 协议提供⽹⻚浏览服务。
(2)搭建 apache 服务器
(2.1)安装并设置防⽕墙
yum -y install httpd # 安装httpd服务应⽤,httpd服务和nginx服务都是使⽤80端⼝ systemctl start httpd firewall-cmd --permanent --add-service=http #设置防⽕墙放⾏apache firewall-cmd --reload #重载防⽕墙规则 firewall-cmd --list-all #查看当前区域下防⽕墙所有规则
(2.2)apache 配置⽂件
vim /etc/httpd/conf/httpd.conf
37 # Listen: Allows you to bind Apache to specific
IP addresses and/or
38 # ports, instead of the default. See also the
<VirtualHost>
39 # directive.
40 #
41 # Change this to Listen on specific IP addresses
as shown below to
42 # prevent Apache from glomming onto all bound IP
addresses.
43 #
44 #Listen 12.34.56.78:80
45 Listen 80 #默认的httpd监听端⼝,可在下⾯添加其他⾃设端⼝
46
47 #
48 # Dynamic Shared Object (DSO) Support
......# 省略部分内容
92 # ServerName gives the name and port that the
server uses to identify itself.
93 # This can often be determined automatically,
but we recom mend you specify
94 # it explicitly to prevent problems during
startup.
95 #
96 # If your host doesn't have a registered DNS
name, enter i ts IP address here.
97 #
98 ServerName www.example.com:80 #指定httpd服务域名和该域名的端⼝,可以⼿动修改其他域名
99
100 #
101 # Deny access to the entirety of your server's filesystem. You must
(2.3)apache 索引⽂件
httpd 使⽤ yum 安装时,默认的站html⽂件在/var/www/html/index.html,如果 index.html ⽂件不存在,需要⼿动创建
vim /var/www/html/index.html
<html>
<head>
<meta charset="utf-8">
<title>正方形</title>
<style>
div{
background-color:red;
width:120px;
height:120px;
}
</style>
</head>
<body>
<div>正方形</div>
</body>
</html>
(2.4)服务器访问测试
浏览器内输⼊搭建好的 httpd 服务器 IP 地址或域名(当前服务 器地址为 192.168.33.110)
Nginx服务
一、Nginx 简介
Nginx:开源、轻量级、⾼性能的 HTTP 和反向代理服务器,可以代理 HTTP、IMAP/POP3/SMTP 和 TCP/UDP协议。
基本特点:占⽤内存少、并发能⼒强,采⽤ C 语⾔编写,在性能⽅⾯有⼀定保证。
Nginx 可以⽤来做负载均衡及反向代理使⽤,⽬前使⽤最多的是负载均衡。
二、Nginx安装
wget https://nginx.org/download/nginx-1.26.1.tar.gz
//获取Nginx tar包
tar -zxvf nginx-1.26.1.tar.gz
//解压
yum -y install gcc gcc-c++
//采⽤ C 语⾔编写
yum -y install make lrzsz openssl-devel pcre-devel
//⾸先安装 pcre。此软件是为了⽀持 rewrite(重写、复写)功能 ⽽存在的。rewrite 是实现 url 重定向的重要命令,它会根据正则 表达式来匹配内容,从⽽跳转到⽬标上⾯去。当没有使⽤ ssl 证书对服务器数据进⾏加密认证时,⽤户的数据将会以明⽂的形式进⾏传输,⽽此时,⽤户的数据可以被⼀些抓包⼯具获取,就容易造成⽤户的信息泄露。
cd 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
//--prefix=/usr/local/nginx:指定软件的安装路径,注意不 是安装包⽬录,⽽是软件要安装在哪。 --user=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代理模块。
useradd -s /sbin/nologin nginx
//安装完成有创建⽤户,否则⽆法启动
make && make install
//进⾏编译安装 nginx
cp -r conf/ conf.bak
//备份配置文件
ss -lntup| grep nginx firewall-cmd --zone=public --add-port=80/tcp --permanent
//配置防火墙
firewall-cmd --reload firewall-cmd --list-all
public target: default icmp-block-inversion: no interfaces: sources: services: ssh dhcpv6-client ports: 80/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
三、Nginx配置文件
1、主配置文件
nginx.conf 配置⽂件由指令控制的模块组成。 指令分为简单指令和块指令,⼀个简单指令由名称和参数组成, 空格分隔,分号结尾,如:listen 80;。 块指令与简单指令相同的结构,但不是以分号结尾,⽽是以⼤括 号包围的组附加指令结束,如:server { }。
2、 nginx 块指令
全局块:配置nginx全局的指令events块:配置nginx与⽤户连接 相关指令 events 块:配置 nginx 与⽤户连接的相关指令,如: events {}。 http 块:提供HTTP服务,如:http { }。 server 块:配置虚拟主机,⼀个http可以有多个server,如:server { }。 location 块:匹配URL后做什么动作或者跳转到⽹⻚的哪⾥,如:location { }。
3、配置文件详解
2 #user nobody; # 配置运⾏nginx的⽤户和组,(全局块) 3 worker_processes 1; # 设置worker的进程数量 4 5 #error_log logs/error.log; # 错误⽇志的路径信息 6 #error_log logs/error.log notice; # notice等级的错误⽇志路径信息(等级可修改或添加) 7 #error_log logs/error.log info; # info等级的错误⽇志路径信息(等级可添加或修改) 8 9 #pid logs/nginx.pid; # pid的⽂件路径 10 11 12 events { # events块 13 worker_connections 1024; # 每个进程最多能处理多少个连接 14 } 15 16 17 http { # http块,设定http服务器,利⽤它的反向代理功能提供负载均衡⽀持 18 include mime.types; #指定⽂件拓展名和⽂件类型映射表
19 default_type application/octet-stream; #指定⽂件类型
20 21 #log_format main '$remote_addr - $remote_user [ $time_local] "$request" ' 22 # '$status $body_bytes_sent "$ht tp_referer" ' 23 # '"$http_user_agent" "$http_x_f orwarded_for"'; 24
#上⾯注释的三⾏为nginx程序内部变量
25 #access_log logs/access.log main;
#设定访问⽇志的路径及格式
26 27 sendfile on; # 指定nginx是否调⽤sendfile函数来输出⽂件,对于普通应⽤必须设定为no 28 #tcp_nopush on; # 放置⽹络阻塞 29 30 #keepalive_timeout 0; # 连接超过的时间设置 31 keepalive_timeout 65; 32 33 #gzip on; # 开启gzip压缩 34 35 server { # server块,配置各种虚拟主机 36 listen 80; # 设置监听端⼝ 37 server_name localhost; # 配置的域名信息,⼀个server可以对应⼀个域名信息 38 39 #charset koi8-r; # 字符集,utf-8为中⽂字符集 40 41 #access_log logs/host.access.log main; # 设定本虚拟主机的访问⽇志和⽇志类型 42 43 location / { # location块,代表url的跳转,“/”后⾯写⼊的内容代表我们在浏览器地址栏输⼊的域名或IP地址的后续访问路径 44 root html; #服务器的默认⽹站根⽬录位置 45 index index.html index.htm; #⾸⻚索引⽂件名称 46 } 48 #error_page 404 /404.html; # 错误提示⻚⾯ 49 50 # redirect server error pages to the static p age /50x.html 51 # 52 error_page 500 502 503 504 /50x.html; # 错误提示⻚⾯ 53 location = /50x.html { 54 root html; 55 } # 每个花括号都会与前⾯的对应 ......省略部分注释内容....... 79 } # 这个花括号对应的是 server 的 “{” ......省略部分注释内容....... 117 } # 这个花括号对应的是 http 的 “{”
4、优化nginx服务
(1)直接使⽤ nginx 命令
ln -s /usr/local/nginx/sbin/nginx /usr/sbin
//#将软件⽬录下的nginx可执⾏程序软链接到/usr/sbin
nginx -t
//对nginx配置⽂件的语法和格式进⾏检查
nginx //启动nginx nginx -s stop //停止nginx nginx -s reload //热重启,仅重载nginx配置文件
(2)以 systemctl 控制 nginx
在 /usr/lib/systemd/system ⽬录下新建⼀个 nginx.service ⾮⼿动执⾏脚本,并使⽤ vim 命令添加以下内容。
[root@YH2 ~]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking PIDFile=/usr/local/nginx/logs/nginx.pid ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop PrivateTmp=Flase [Install] WantedBy=multi-user.target
[root@YH2 ~]# systemctl daemon-reload #重载systemctl的配置⽂件,以便nginx.service⽣效 [root@YH2 ~]# systemctl start nginx.service #启动nginx [root@YH2 ~]# systemctl status nginx.service #查看nginx状态