Nginx01-HTTP简介与Nginx简介(安装、命令介绍、目录介绍、配置文件介绍)

7 篇文章 0 订阅

HTTP简介

HTTP原理

  • WebServer为Client提供网页浏览等服务,又称HTTPServer或WWWServer。
  • HTTP采用B/S模型,Client和Server通过HTTP或HTTPS进行数据的传输。
  • HTTP采用TCP链接,HTTP默认监听80端口,HTTPS默认监听443端口。
  • HTTP的工作原理:
    1. 链接:Browser和WebServer建立链接,通过打开socket表明链接建立成功。
    2. 请求:Browser通过socket向WebServer提交请求,一般为HTTP的GET或POST。
    3. 应答:Server通过特定端口接收监听到的Web页面请求,若请求存在再通过HTTP发送Browser需要的信息;若请求不存在则返回错误提示。
    4. 关闭连接:每完成一次应答,就会断开一次链接。

查看访问网站的详细流程

curl -v

[root@front ~]# curl -v www.baidu.com
* Rebuilt URL to: www.baidu.com/
*   Trying 153.3.238.110...
* TCP_NODELAY set
* Connected to www.baidu.com (153.3.238.110) port 80 (#0)
> GET / HTTP/1.1
> Host: www.baidu.com
> User-Agent: curl/7.61.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: keep-alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Sun, 02 Jun 2024 12:25:56 GMT
< Etag: "588604eb-94d"
< Last-Modified: Mon, 23 Jan 2017 13:28:11 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><input id=kw name=wd class=s_ipt value maxlength=255 autocomplete=off autofocus></span><span class="bg s_btn_wr"><input type=submit id=su value=百度一下 class="bg s_btn"></span> </form> </div> </div> <div id=u1> <a href=http://news.baidu.com name=tj_trnews class=mnav>新闻</a> <a href=http://www.hao123.com name=tj_trhao123 class=mnav>hao123</a> <a href=http://map.baidu.com name=tj_trmap class=mnav>地图</a> <a href=http://v.baidu.com name=tj_trvideo class=mnav>视频</a> <a href=http://tieba.baidu.com name=tj_trtieba class=mnav>贴吧</a> <noscript> <a href=http://www.baidu.com/bdorz/login.gif?login&amp;tpl=mn&amp;u=http%3A%2F%2Fwww.baidu.com%2f%3fbdorz_come%3d1 name=tj_login class=lb>登录</a> </noscript> <script>document.write('<a href="http://www.baidu.com/bdorz/login.gif?login&tpl=mn&u='+ encodeURIComponent(window.location.href+ (window.location.search === "" ? "?" : "&")+ "bdorz_come=1")+ '" name="tj_login" class="lb">登录</a>');</script> <a href=//www.baidu.com/more/ name=tj_briicon class=bri style="display: block;">更多产品</a> </div> </div> </div> <div id=ftCon> <div id=ftConw> <p id=lh> <a href=http://home.baidu.com>关于百度</a> <a href=http://ir.baidu.com>About Baidu</a> </p> <p id=cp>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact

wget --debug

[root@front ~]# wget --debug www.baidu.com
DEBUG output created by Wget 1.19.5 on linux-gnu.

Reading HSTS entries from /root/.wget-hsts
URI encoding = ‘UTF-8’
Converted file name 'index.html' (UTF-8) -> 'index.html' (UTF-8)
--2024-06-02 20:28:06--  http://www.baidu.com/
Resolving www.baidu.com (www.baidu.com)... 153.3.238.110, 153.3.238.102, 2408:873d:22:18ac:0:ff:b021:1393, ...
Caching www.baidu.com => 153.3.238.110 153.3.238.102 2408:873d:22:18ac:0:ff:b021:1393 2408:873d:22:1a01:0:ff:b087:eecc
Connecting to www.baidu.com (www.baidu.com)|153.3.238.110|:80... connected.
Created socket 3.
Releasing 0x0000557a357bf620 (new refcount 1).

---request begin---
GET / HTTP/1.1
User-Agent: Wget/1.19.5 (linux-gnu)
Accept: */*
Accept-Encoding: identity
Host: www.baidu.com
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 200 OK
Content-Length: 2381
Content-Type: text/html
Server: bfe
Date: Sun, 02 Jun 2024 12:28:06 GMT

---response end---
200 OK
Registered socket 3 for persistent reuse.
Length: 2381 (2.3K) [text/html]
Saving to: ‘index.html’

index.html                 100%[=======================================>]   2.33K  --.-KB/s    in 0s

2024-06-02 20:28:06 (123 MB/s) - ‘index.html’ saved [2381/2381]

查看网站访问量

alexa.chinaz.com

指标说明
IP访问网站的独立IP数量,公网IP。
PV页面访问量Page view。
UV独立访客数量,接近于用户数量 Unique Visitor。
DAU每天的活跃用户的数量:日活(日活跃用户)。
MAU月活(月活跃用户)。

HTTP协议版本

http1.0http1.1http2.0http3.0
特点短连接,每次请求都需要重复建立断开连接加入长连接功能增加并发,访问更快基于udp更快,应用于流媒体
占用服务端资源keepalive(响应后不会立刻断开)
是否加密http不加密,https加密默认基于https
基于tcp/udptcptcptcpudp

HTTP协议交互

  1. 浏览器发送request请求
  2. 服务器回复response响应

HTTP 请求

---request begin---
GET / HTTP/1.1  
User-Agent: Wget/1.19.5 (linux-gnu) 
Accept: */*
Accept-Encoding: identity
Host: www.baidu.com
Connection: Keep-Alive

---request end---
请求报文起始行

GET / HTTP/1.1

  • 请求方法:用于指定客户端如何访问服务端(下载,上传,查看服务端信息)
常见的请求方法说明
GET下载(大部分请求),用于请求服务器发送指定资源。
POST上传(上传文件内容、登录),用于向服务器提交数据,通常导致服务器上的状态变化或副作用。
HEAD类似于GET,仅仅输出响应的头部信息(查看服务端的信息,一般用于检查)。
  • 资源的位置(URI): 这个资源在网站站点目录的哪个地方,叫什么名字.
    这里面写的/,斜线并非是Linux系统的根目录.这个/叫网站的站点目录

  • http协议版本

请求头

User-Agent: Wget/1.19.5 (linux-gnu)
Accept: /
Accept-Encoding: identity
Host: www.baidu.com
Connection: Keep-Alive

HTTP请求头解释
User-Agent: Wget/1.19.5 (linux-gnu)请求头,告诉服务器发起请求的应用类型和版本,这里是Wget命令行下载工具。
Accept: /请求头,告诉服务器客户端接受任何类型的 MIME 响应。
Accept-Encoding: identity请求头,告诉服务器客户端不接受任何编码转换,即希望服务器发送原始数据。
Host: www.baidu.com请求头,指定请求的服务器域名,这里是百度网站的域名。
Connection: Keep-Alive请求头,告诉服务器客户端希望保持连接活跃,以便为后续请求重用相同的TCP连接。

HTTP响应

---response begin---
HTTP/1.1 200 OK
Content-Length: 2381
Content-Type: text/html
Server: bfe
Date: Sun, 02 Jun 2024 12:49:11 GMT

---response end---
响应报文起始行

HTTP/1.1 200 OK

  • HTTP版本

  • 状态码 状态码描述
    状态码是反应服务器是否正确的处理用户请求
    |状态码 |含义|
    |-|-|
    |2xx |表示正常|
    |3xx |表示需要进行跳转,表示正常|
    |4xx |表示异常,客户端问题|
    |5xx |表示异常,服务端问题|

状态码状态码描述解释
200OK访问正常,服务器成功处理了请求。
301Moved Permanently永久跳转,请求的资源已被永久移动到新位置。
302Found或Moved Temporarily临时跳转,请求的资源临时移动到新位置。
304Not Modified浏览器缓存,请求的资源未修改,可以使用缓存的内容。
403Forbidden权限拒绝(拒绝访问)。权限问题,如未授权访问;首页文件问题,如配置错误导致无法访问首页。
404Not Found文件找不到,一般辅助错误日志排查,用于定位请求的资源不存在的问题。
500Internal Error内部错误,如服务器配置错误、应用程序错误等。SElinux开启时,可能会导致一些异常。一般需要辅助错误日志来排查。
502Bad Gateway网关错误,一般发生在负载均衡器中,请求发送到后端服务器,但后端服务器没有响应,导致网关返回错误。
503Service Unavailable服务临时不可用,后端负载异常等情况,或者人为设置(如系统升级)导致服务暂时不可用。
504Gateway Time-out网关超时,请求发送到网关,但网关在规定时间内没有响应。
响应头

Content-Length: 2381
Content-Type: text/html
Server: bfe
Date: Sun, 02 Jun 2024 12:49:11 GMT

HTTP响应组成部分解释
Content-Length: 2381响应头,指示实体主体的大小(以字节为单位),这里是2381字节。
Content-Type: text/html响应头,指示响应的内容类型,这里是HTML文本。
Server: bfe响应头,指示处理请求的服务器软件,这里是百度的前端服务器(Baidu Front End)。
Date: Sun, 02 Jun 2024 12:49:11 GMT响应头,指示消息发送的时间,这里是2024年6月2日星期日,格林尼治标准时间12:49:11。

Nginx

常见的Web服务

类型说明
WEB服务网站服务,部署并启动了这个服务,你就可以搭建一个网站。
WEB中间件等同于WEB服务,用于提供网站的托管和访问服务。
中间件范围更加广泛,指的负载均衡之后的服务,包括各种应用程序层和网络层的中间件。
数据库中间件数据库缓存、消息队列等功能的中间件,用于提高数据库性能和处理数据。

常见网站服务

网站服务说明官网
Nginx大部分使用nginx, Engine Xhttp://nginx.org/en/docs/
Tengine基于Nginx二开, 淘宝开源, 更多内置模块http://tengine.taobao.org/
Openresty基于Nginx二开, 加强Lua功能与模块http://openresty.org/
Tomcat运行Java环境的Web服务https://tomcat.apache.org/
Jboss运行Java环境的Web服务https://www.jboss.org/
Jetty运行Java环境的Web服务https://www.eclipse.org/jetty/
Weblogic运行Java环境的Web服务https://www.oracle.com/weblogic/
PHP运行PHP环境的Web服务,需要nginx (LNMP)https://www.php.net/

安装Nginx

从官网安装 nginx.org/en
在这里插入图片描述

在这里插入图片描述

  • Mainline 主线版本 – 奇数版本 - 开发&测试版本 --> 开发团队 - 二次开发
  • Stable 稳定版本 – 偶数版本 - 稳定&生产 - 中小企业
  • Lengacy 历史版本 – 偶数版本 - 稳定的

download->stable 最下面->RHEL and derivatives->yum
在这里插入图片描述

# 配置官网yum源(stable版)
[root@front ~]# 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 nginx

#检查
[root@front ~]# rpm -qa nginx
nginx-1.26.1-2.el8.ngx.x86_64
[root@front ~]# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/fastcgi_params
/etc/nginx/mime.types
/etc/nginx/modules
/etc/nginx/nginx.conf
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
/usr/lib/.build-id
/usr/lib/.build-id/3d
/usr/lib/.build-id/3d/2e2d5ec454efae0e8f8abeb6f056b4636a8a0c
/usr/lib/.build-id/4f
/usr/lib/.build-id/4f/adbe3f34e65c805137a92caf2f2035f1112f6a
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/libexec/initscripts/legacy-actions/nginx
/usr/libexec/initscripts/legacy-actions/nginx/check-reload
/usr/libexec/initscripts/legacy-actions/nginx/upgrade
/usr/sbin/nginx
/usr/sbin/nginx-debug
/usr/share/doc/nginx-1.26.1
/usr/share/doc/nginx-1.26.1/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx
/usr/share/nginx/html
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/cache/nginx
/var/log/nginx

Nginx目录结构

不同的安装方式,目录、文件会有所变化

目录结构说明
/etc/nginx/Nginx各种配置的目录
/etc/nginx/nginx.conf主配置文件
/etc/nginx/conf.d/子配置文件(网站)目录
/etc/nginx/conf.d/default.conf默认的子配置文件
/usr/sbin/nginxngx命令,用于启动、停止、重启Nginx服务器
/usr/share/nginx/html/ngx默认的站点目录,网站的根目录
/var/log/nginx/ngx日志:访问日志、错误日志、跳转日志
其他目录和文件说明
/etc/logrotate.d/nginx日志切割(防止文件过大)
/etc/nginx/mime.types媒体类型文件,定义了MIME类型和扩展名的映射
/etc/nginx/fastcgi_params包含FastCGI参数的文件,用于处理PHP请求
/etc/nginx/uwsgi_params包含uWSGI参数的文件,用于处理Python请求
/usr/lib/systemd/system/nginx.servicesystemctl配置文件,用于使用systemd管理Nginx服务
/var/cache/nginx/缓存目录,用于存储临时文件和缓存内容

Nginx启动管理

源码安装的nginx启动方式不同,建议菜鸟教程

# 启动
[root@front ~]# systemctl enable nginx --now

# 查看端口
[root@front ~]# ss -tunlp | grep 80
tcp   LISTEN 0      128          0.0.0.0:80         0.0.0.0:*    users:(("nginx",pid=2403,fd=6),("nginx",pid=2402,fd=6),("nginx",pid=2225,fd=6))                                                                      
# 查看进程
[root@front ~]# ps -ef | grep nginx
root        2225       1  0 16:56 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx       2402    2225  0 19:51 ?        00:00:00 nginx: worker process
nginx       2403    2225  0 19:51 ?        00:00:00 nginx: worker process
root        2851    1384  0 21:22 pts/0    00:00:00 grep --color=auto nginx

# 访问测试
[root@front ~]# curl 127.0.0.1
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

Nginx常用命令

命令选项解释
nginx -s stop停止Nginx服务器
nginx -s quit优雅地停止Nginx服务器
nginx -s reload重新加载Nginx配置文件
nginx -s reopen重新打开日志文件
nginx -s configtest测试Nginx配置文件的语法错误
nginx -t测试Nginx配置文件的语法错误
nginx -c filename指定配置文件路径
nginx -p path指定Nginx的运行路径
nginx -g “options”设置全局配置选项
nginx -V显示Nginx版本信息
nginx -h显示帮助信息

Nginx配置文件

主配置文件

在这里插入图片描述

指令说明
user指定Nginx进程运行的用户和组,默认是nobody
worker_processes指定工作进程的数量,默认为1
error_log指定错误日志文件的位置和级别,默认为/var/log/nginx/error.log
pid指定Nginx进程ID文件的位置,默认为/var/run/nginx.pid
events配置事件模块,包括工作进程的数量和类型
http配置HTTP模块,包括全局设置和监听端口
server配置虚拟主机,包括域名、根目录、日志文件等
location配置请求匹配的规则,包括资源路径、重定向、缓存等
access_log指定访问日志文件的位置和格式,默认为/var/log/nginx/access.log
error_log指定错误日志文件的位置和格式,默认为/var/log/nginx/error.log
sendfile指定是否开启sendfile传输模式,默认为on
tcp_nopush指定是否在发送数据前压缩缓冲区,默认为on
keepalive_timeout指定长连接的超时时间,默认为65
client_max_body_size指定客户端请求的最大体长,默认为1M
client_body_buffer_size指定客户端请求体长缓冲区的大小,默认为8k
gzip指定是否开启gzip压缩,默认为on
gzip_vary指定是否在响应头中包含gzip压缩的信息,默认为on
gzip_proxied指定是否压缩代理请求的响应,默认为any
gzip_types指定需要压缩的MIME类型,默认为text/html
gzip_disable指定不需要压缩的MIME类型,默认为msie6
fastcgi_pass指定FastCGI后端服务器的地址和端口,默认为127.0.0.1:9000
uwsgi_pass指定uWSGI后端服务器的地址和端口,默认为127.0.0.1:9000
proxy_pass指定代理后端服务器的地址和端口,默认为http://127.0.0.1:80
proxy_set_header指定代理请求的头部信息,默认为Host: $host
proxy_http_version指定代理请求的HTTP版本,默认为1.1
proxy_connect_timeout指定代理连接的超时时间,默认为60s
proxy_send_timeout指定代理发送请求的超时时间,默认为60s
proxy_read_timeout指定代理读取响应的超时时间,默认为60s
proxy_buffer_size指定代理请求缓冲区的大小,默认为4k
proxy_buffering指定是否启用代理缓冲区,默认为on
proxy_cache指定代理缓存的相关设置,默认为off
proxy_cache_key指定代理缓存键的生成规则,默认为 s c h e m e scheme schemeproxy_host$request_uri
proxy_cache_valid指定代理缓存的有效时间,默认为1d
proxy_cache_min_uses指定代理缓存的最小使用次数,默认为1
proxy_cache_use_stale指定代理缓存是否使用过时的数据,默认为off
proxy_cache_lock指定代理缓存是否锁定数据,默认为on
proxy_cache_lock_timeout指定代理缓存锁定数据的时间,默认为5s
proxy_cache_lock_age指定代理缓存锁定数据的时间,默认为5s
proxy_cache_lock_timeout指定代理缓存锁定数据的时间,默认为5s
proxy_cache_lock_age指定代理缓存锁定数据的时间,默认为5s
proxy_cache_lock_timeout指定代理缓存锁定

子配置文件

在这里插入图片描述

指令说明
listen指定监听端口
server_name指定域名,多个通过空格分割
location匹配请求中的uri(资源地址)
root指定站点目录(网站的根目录)
index指定站点的首页文件。用户访问的时候不加上任何的文件,展示首页文件
error_log指定错误状态码与对应的错误页面
  • 29
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值