web基础HTTP协议+Nginx安装即基础配置

web基础HTTP协议

HTTP全称: Hyper Text Transfer Protocol 中文名:超文本传输协议

http://class.driverzeng.com/15648000575258.html

什么是URL


URL即统一资源定位符(Uniform Resource Locator),用来唯一地标识万维网中的某一个文档。

URL由协议、主机和端口(默认为80)以及文件名三部分构建:

http://    class.driverzeng.com     /15648000575258.html
# 协议        # 主机:端口             # 文件名
协议 http://   https://
主机端口  class.driverzeng.com:80    域名:端口
文件名    15648000575258.html

什么是超文本传输协议

是一种按照URL指示,将超文本文档从一台主机(web服务器)传输到另一台主机(浏览器)的应用层协议,以实现超链接的功能。

# 在浏览器之中输入网址访问页面
http://class.driverzeng.com/15648000575258.html
​
1、浏览器分析URL
http://class.driverzeng.com/15648000575258.html
​
2、浏览器向DNS请求分析ip地址
- 本地DNS
    如果本地DNS里有class.driverzeng.com把IP返回给浏览器
    如果没有就往DNS服务器做解析
- DNS服务器 13跟域
    .com
    .cn
    .org
    .xyz
    .net
    .gov
    .edu
    
    - 递归查询
    class.driverzeng.com/15648000575258.html
    class.driverzeng.com ---- A记录
    
    - 迭代查询
    DNS服务器里去找
    
3.浏览器收到了IP地址之后
10.0.0.7:80
    IP:80建立连接
    TCP 三次握手
​
4.浏览器向web服务器发送GET请求
10.0.0.7 GET 下载15648000575258.html
​
5.web服务器接收到了GET之后 返回给浏览器
/var/www/html/15648000575258.html
​
6.浏览器确保接收到了15648000575258.html
断开连接
TCP 四次挥手
​
7.浏览器展示web页面在网页中
​
web服务器
浏览器
DNS

http状态码

200 页面正常访问
301 永久重定向
302 临时重定向
304 浏览器的缓存
307 内部重定向
​
400 客户端错误
401 认证失败
403 权限不足
404 页面找不到
​
500 服务器的内部问题(代码问题 服务器的问题)
502 bad gateway 后端服务问题
503 服务器过载 访问的评率过快
504 后端服务超时

Nginx基础概述

nginx简述

Nginx是一个开源且高性能、可靠的Http Web服务、代理服务。

开源: 直接获取源代码

高性能: 支持海量并发

可靠: 服务稳定


我们为什么要选择Nginx服务

Nginx非常轻量

功能模块少(源代码仅保留http与核心模块代码,其余不够核心代码会作为插件来安装)

代码模块化(易读,便于二次开发,对于开发人员非常友好)

互联网公司都选择nginx

  1. Nginx技术成熟,具备的功能是企业最常使用而且最需要的

  2. 适合当前主流架构趋势,微服务、云架构、中间层

  3. 统一技术栈 降低维护成本,降低技术更新成本。

Nginx采用Epool网络模型,Apache采用Select模型

Select: 当用户发起一次请求,select模型就会进行一次遍历扫描,从而导致性能低下。

Epool: 当用户发起请求,epool模型会直接进行处理,效率高效,并无连接限制。

Nginx快速安装


Nginx的安装方式

  1. 源码编译=>Nginx (1.版本随意 2.安装复杂 3.升级繁琐 4.规范 5.便于管理)

  2. epel仓库=>Nginx (1.版本较低 2.安装简单 3.配置不易读)

  3. 官方仓库=>Nginx (1.版本较新 2.安装简单 3.配置易读)

Nginx官方仓库下载


# 添加nginx源
[root@web03 wordpress]# vim /etc/yum.repos.d/nginx.repo 
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
​
# 安装依赖
yum install -y pcre-devel openssl-devel  gcc gcc-c++
​
# 安装nginx
yum install -y nginx
nginx-1.26.0

Nginx启停

# 启动nginx
systemctl start nginx
nginx
​
# 停止nginx
systemctl stop nginx
nginx -s stop
​
# 重启nginx
systemctl restart nginx
nginx -s restart
​
# 重载
systemctl reload nginx
nginx -s reload

Nginx相关文件介绍

为了让大家更清晰的了解 Nginx 软件的全貌,可使用 rpm -ql nginx 查看整体的目录结构及对应的功 能,如下表格整理了 Nginx 比较重要的配置文件

1.Nginx主配置文件

路径类型作用
/etc/nginx/nginx.conf配置文件nginx主配置文件
/etc/nginx/conf.d/default.conf配置文件默认网站配置文件

2.Nginx代理相关参数文件

路径类型作用
/etc/nginx/fastcgi_params配置文件Fastcgi代理配置文件
/etc/nginx/scgi_params配置文件scgi代理配置文件
/etc/nginx/uwsgi_params配置文件uwsgi代理配置文件

3.Nginx编码相关配置文件

路径类型作用
/etc/nginx/win-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-utf配置文件Nginx编码转换映射文件
/etc/nginx/koi-win配置文件Nginx编码转换映射文件
/etc/nginx/mime.types配置文件Content-Type与扩展名

4.Nginx管理相关命令

路径类型作用
/usr/sbin/nginx命令Nginx命令行管理终端工具
/usr/sbin/nginx-debug命令Nginx命令行与终端调试工具

5.Nginx日志相关目录与文件

路径类型作用
/var/log/nginx目录Nginx默认存放日志目录
/etc/logrotate.d/nginx配置文件Nginx默认的日志切割

nginx主配置文件讲解

# 核心模块
user nginx; # 启动nignx的用户
worker_processes auto; # nginxworker进程数 auto是根据服务器的cpu自行判断
error_log /var/log/nginx/error.log notice; # 错误日志的路径和文件名称
pid /var/run/nginx.pid; # nginx进程的pid信息
​
# 事件驱动模块
events {
    worker_connections 1024;#每个worker进程支持的最大连接数
    use epoll; #事件驱动模型, epoll默认
}
​
# HttpCoreModule(http内核模块)
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;
# 搭配sendfile一起使用
tcp_nopush on;
# 搭配sendfile一起使用
tcp_nodelay on;
# 长连接格式超时时间是65s
keepalive_timeout 65;
types_hash_max_size 4096;
# nginx相关的配置的类型配置文件
include /etc/nginx/mime.types;
# 默认类型
default_type application/octet-stream;
​
​
# nignx网站相关配置文件的编写路径 和文件和格式
include /etc/nginx/conf.d/*.conf;
​
​
server {
        listen 80; # nginx默认的端口 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 {
        }
    }
}
​

简单配置一个nginx网页

# 编辑nginx的配置文件
[root@web03 nginx]# vi /etc/nginx/conf.d/1.conf 
server{
        listen 80;
        server_name 10.0.0.9;
        root /code/wordpress;
        index readme.html;
}
​
# 检查Nginx语法
[root@web03 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
​
​
# 创建站点目录
mkdir /code
​
# 准备索引文件
[root@web02 nginx-1.26.0]# echo 123 > /code/index.html
[root@web02 nginx-1.26.0]# cat /code/index.html
123
​
# 启动服务
systemctl  restart nginx
​
# 检查进程和端口
[root@web02 nginx-1.26.0]# ps -ef | grep nginx
root 13755 1 0 11:38 ? 00:00:00 nginx: master process
/usr/sbin/nginx
nginx 13757 13755 0 11:38 ? 00:00:00 nginx: worker process
root 13762 6942 0 11:39 pts/0 00:00:00 grep --color=auto nginx
[root@web02 nginx-1.26.0]# netstat -lntup | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
13755/nginx: master
tcp6 0 0 :::80 :::* LISTEN
13755/nginx: master
​
# 本地DNS
windows键 + r

  • drivers


编辑hosts文件

nginx搭建小游戏网站

# 编辑nginx配置文件
vim /etc/nginx/conf.d/1.conf
    server{
    listen 80;
    server_name 10.0.0.8;
    root /code/h5_games;
    index index.html;
}

# 准备站点目录
mkdir /code

# 准备游戏代码
cd /code 
wget http://test.driverzeng.com/Nginx_Code/h5_games.zip

# 解压代码
cd /code
unzip h5_games.zip

# 检测nginx
nginx -t
nginx: the configuration file /opt/nginx-1.26.0/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx-1.26.0/conf/nginx.conf test is successful

# 重启nginx
systemctl restart nginx

# 访问10.0.0.8
10.0.0.8
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值