目錄
- HTTP簡介
- HTTP原理
- 查看訪問網站的詳細流程
- curl -v
- wget --debug
- 查看網站訪問量
- HTTP協議版本
- HTTP協議交互
- HTTP 請求
- 請求報文起始行
- 請求頭
- HTTP響應
- 響應報文起始行
- 響應頭
- Nginx
- 常見的Web服務
- 常見網站服務
- 安裝Nginx
- Nginx目錄結構
- Nginx啟動管理
- Nginx常用命令
- Nginx配置文件
- 主配置文件
- 子配置文件
HTTP簡介
HTTP原理
- WebServer為Client提供網頁瀏覽等服務,又稱HTTPServer或WWWServer。
- HTTP采用B/S模型,Client和Server通過HTTP或HTTPS進行數據的傳輸。
- HTTP采用TCP鏈接,HTTP默認監聽80端口,HTTPS默認監聽443端口。
- HTTP的工作原理:
- 鏈接:Browser和WebServer建立鏈接,通過打開socket表明鏈接建立成功。
- 請求:Browser通過socket向WebServer提交請求,一般為HTTP的GET或POST。
- 應答:Server通過特定端口接收監聽到的Web頁面請求,若請求存在再通過HTTP發送Browser需要的信息;若請求不存在則返回錯誤提示。
- 關閉連接:每完成一次應答,就會斷開一次鏈接。
查看訪問網站的詳細流程
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&tpl=mn&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>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必讀</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意見反饋</a> 京ICP證030173號 <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 0s2024-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.0 | http1.1 | http2.0 | http3.0 | |
---|---|---|---|---|
特點 | 短連接,每次請求都需要重復建立斷開連接 | 加入長連接功能 | 增加并發,訪問更快 | 基于udp更快,應用于流媒體 |
占用服務端資源 | keepalive(響應后不會立刻斷開) | |||
是否加密 | http不加密,https加密 | 默認基于https | ||
基于tcp/udp | tcp | tcp | tcp | udp |
HTTP協議交互
- 瀏覽器發送request請求
- 服務器回復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 |表示異常,服務端問題|
狀態碼 | 狀態碼描述 | 解釋 |
---|---|---|
200 | OK | 訪問正常,服務器成功處理了請求。 |
301 | Moved Permanently | 永久跳轉,請求的資源已被永久移動到新位置。 |
302 | Found或Moved Temporarily | 臨時跳轉,請求的資源臨時移動到新位置。 |
304 | Not Modified | 瀏覽器緩存,請求的資源未修改,可以使用緩存的內容。 |
403 | Forbidden | 權限拒絕(拒絕訪問)。權限問題,如未授權訪問;首頁文件問題,如配置錯誤導致無法訪問首頁。 |
404 | Not Found | 文件找不到,一般輔助錯誤日志排查,用于定位請求的資源不存在的問題。 |
500 | Internal Error | 內部錯誤,如服務器配置錯誤、應用程序錯誤等。SElinux開啟時,可能會導致一些異常。一般需要輔助錯誤日志來排查。 |
502 | Bad Gateway | 網關錯誤,一般發生在負載均衡器中,請求發送到后端服務器,但后端服務器沒有響應,導致網關返回錯誤。 |
503 | Service Unavailable | 服務臨時不可用,后端負載異常等情況,或者人為設置(如系統升級)導致服務暫時不可用。 |
504 | Gateway 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 X | http://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/nginx | ngx命令,用于啟動、停止、重啟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.service | systemctl配置文件,用于使用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 | 指定錯誤狀態碼與對應的錯誤頁面 |