用戶訪問網(wǎng)站的流程
- 瀏覽器中輸入www.baidu.com 到顯示頁面過程
1.域名解析ip DNS解析過程
2.tcp三次握手
3.http 請(qǐng)求報(bào)文
4.處理請(qǐng)求
5.http 響應(yīng)報(bào)文
6.tcp四次揮手
7.顯示頁面
使用curl命令顯示請(qǐng)求和響應(yīng)的過程
[root@wsm ~]# curl -v www.baidu.com
* About to connect() to www.baidu.com port 80 (#0)
* Trying 220.181.38.149...
* Connected to www.baidu.com (220.181.38.149) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.baidu.com
> 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: Thu, 13 Jun 2019 03:27:58 GMT
< Etag: "588604c8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
衡量網(wǎng)站訪問量大的單位
ip 網(wǎng)站每天有多少個(gè)獨(dú)立的io 訪問
uv unique user 獨(dú)立的用戶
pv page view 頁面訪問量
nginx實(shí)現(xiàn)http協(xié)議
利用nginx搭建網(wǎng)站環(huán)境
nginx編譯安裝
根據(jù)源代碼 ./configure make make install 三步
- 創(chuàng)建安裝目錄
mkdir -p /server/tools - 下載nginx
wget -P /server/tools/ http://nginx.org/download/nginx-1.14.2.tar.gz - 解壓nginx-1.14.2.tar.gz
[root@wsm tools]# tar xf nginx-1.14.2.tar.gz
[root@wsm nginx-1.14.2]# ll
total 732
drwxr-xr-x. 6 1001 1001 4096 Jun 14 12:29 auto
-rw-r--r--. 1 1001 1001 288742 Dec 4 2018 CHANGES
-rw-r--r--. 1 1001 1001 440121 Dec 4 2018 CHANGES.ru
drwxr-xr-x. 2 1001 1001 168 Jun 14 12:29 conf
-rwxr-xr-x. 1 1001 1001 2502 Dec 4 2018 configure
drwxr-xr-x. 4 1001 1001 72 Jun 14 12:29 contrib
drwxr-xr-x. 2 1001 1001 40 Jun 14 12:29 html
-rw-r--r--. 1 1001 1001 1397 Dec 4 2018 LICENSE
drwxr-xr-x. 2 1001 1001 21 Jun 14 12:29 man
-rw-r--r--. 1 1001 1001 49 Dec 4 2018 README
drwxr-xr-x. 9 1001 1001 91 Jun 14 12:29 src
- 安裝nginx所需的環(huán)境
yum install -y pcre-devel openssl-devel 保證nginx使用時(shí)可用正則 - ./configure
--prefix= 指定安裝位置
--user=www 指定nginx運(yùn)行用戶
--group=www 指定nginx運(yùn)行用戶組
--with-http_stub_status_module 安裝nginx狀態(tài)模塊
--with-http_ssl_module 安裝ssl模塊 用來實(shí)現(xiàn)https
最后執(zhí)行./configure --prefix=/application/nginx-1.14.2 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module
創(chuàng)建虛擬用戶 useradd www -s /bin/nologin
- make 編譯
- make install 安裝
- 安裝完成檢查 tree /application/nginx-1.14.2/
[root@wsm nginx-1.14.2]# tree -L 1 /application/nginx-1.14.2/
/application/nginx-1.14.2/
├── conf
├── html
├── logs
└── sbin
- 刪除原目錄存在的解壓文件椎木,進(jìn)入編譯后目錄
[root@wsm nginx-1.14.2]# pwd
/application/nginx-1.14.2
[root@wsm nginx-1.14.2]# rm -rf /server/tools/nginx-1.14.2/
- 測(cè)試nginx啟動(dòng)
[root@wsm nginx-1.14.2]# /application/nginx-1.14.2/sbin/nginx -t
nginx: the configuration file /application/nginx-1.14.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.2/conf/nginx.conf test is successful
- 啟動(dòng)nginx
[root@wsm nginx-1.14.2]# /application/nginx-1.14.2/sbin/nginx
- 檢查進(jìn)程與端口
[root@web nginx-1.14.2]# ps -ef |grep nginx
root 10887 1 0 14:00 ? 00:00:00 nginx: master process /application/nginx-1.14.2/sbin/nginx
www 10888 10887 0 14:00 ? 00:00:00 nginx: worker process
root 10890 7751 0 14:00 pts/0 00:00:00 grep --color=auto nginx
[root@web nginx-1.14.2]# ss -lntup |grep 80
tcp LISTEN 0 128 *:80
- 使用瀏覽器訪問web ip 提示 Welcome to nginx!表明部署成功
- nginx啟動(dòng)命令創(chuàng)建為軟鏈接
[root@web nginx-1.14.2]# ln -s /application/nginx-1.14.2/ /application/nginx
[root@web nginx-1.14.2]# ln -s /application/nginx-1.14.2/sbin/nginx /sbin/
[root@web nginx-1.14.2]# which nginx
/usr/sbin/nginx
nginx配置文件及目錄
drwxr-xr-x. 2 root root 4096 Jun 15 13:59 conf #配置文件 nginx.conf
drwxr-xr-x. 2 root root 40 Jun 15 13:59 html #站點(diǎn)目錄 網(wǎng)站根目錄
drwxr-xr-x. 2 root root 58 Jun 15 14:00 logs #日志 access.log訪問日志
drwxr-xr-x. 2 root root 19 Jun 15 13:59 sbin #nginx管理命令
nginx #啟動(dòng)命令
nginx -t #檢查語法
nginx -s reload #平滑重啟 (必須在開啟狀態(tài)下)
nginx -s stop #關(guān)閉nginx(必須在開啟狀態(tài)下)
- 配置文件
[root@web conf]# ll
-rw-r--r--. 1 root root 2656 Jun 15 13:59 nginx.conf
-rw-r--r--. 1 root root 2656 Jun 15 13:59 nginx.conf.default
- 文件內(nèi)容
過濾掉nginx.conf文件內(nèi)的#和空行并定義到nginx.conf內(nèi)
[root@test01 conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf
worker_processes 1; #worker進(jìn)程的數(shù)量
events { #事件區(qū)塊開始
worker_connections 1024; #最大進(jìn)程連接數(shù)
} #事件區(qū)塊結(jié)束
http { #http模塊開始
include mime.types;
default_type application/octet-stream;
sendfile on; #開啟高效的傳輸模式
keepalive_timeout 65; #連接超時(shí)時(shí)間
server { #第一個(gè)server區(qū)塊開始违柏,表示一個(gè)獨(dú)立的虛擬主機(jī)站點(diǎn)
listen 80; #默認(rèn)端口80
server_name localhost; #提供服務(wù)的域名主機(jī)名
location / {
root html;
index index.html index.htm; #默認(rèn)的首頁文件
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
虛擬主機(jī)--搭建多個(gè)站點(diǎn)
多個(gè)server標(biāo)簽
[root@test01 conf]# vim nginx.conf
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.wsm.com; #修改域名主機(jī)名
location / {
root html/www; #創(chuàng)建或修改站點(diǎn)目錄
index index.html index.htm; #修改首頁文件內(nèi)容
}
}
server {
listen 80;
server_name blog.wsm.com;#修改域名主機(jī)名
location / {
root html/blog;#創(chuàng)建或修改站點(diǎn)目錄
index index.html index.htm; #修改首頁文件內(nèi)容
}
}
"nginx.conf" 27L, 557C written
[root@test01 conf]# nginx -t
nginx: the configuration file /application/nginx-1.14.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.2/conf/nginx.conf test is successful
[root@test01 conf]# nginx -s reload
[root@test01 conf]# mkdir -p /application/nginx-1.14.2/html/{www,blog}
[root@test01 conf]# for i in www blog
do
echo $i gcy >/application/nginx-1.14.2/html/$i/index.html
done
[root@test01 conf]# for i in www blog ; do cat /application/nginx-1.14.2/html/$i/index.html ; done
www gcy
blog gcy
- 在本地修改hosts解析博烂,測(cè)試是否成功
nginx處理用戶的請(qǐng)求流程
- 1.用戶連接的是哪個(gè)端口
- 2.你要的站點(diǎn)是否存在 server_name
- 使用curl命令取出狀態(tài)碼
[root@test01 conf]# curl -s -w "%{http_code}\n" -o /dev/null 10.0.0.111
200
優(yōu)化nginx配置文件
- 在conf目錄下創(chuàng)建conf.d目錄,用來放置多站點(diǎn)server配置文件
[root@web conf]# mkdir -p conf.d
- 在conf.d先創(chuàng)建多個(gè)server對(duì)應(yīng)的配置文件并進(jìn)行修改
[root@web conf]# vim conf.d/www.conf
[root@web conf]# vim conf.d/blog.conf - 刪除主配置文件nginx.conf內(nèi)多站點(diǎn)server并在http模塊內(nèi)寫入 include conf.d/*.conf
[root@web conf]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include conf.d/*.conf;
}
nginx 訪問日志
- 格式
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 logs/access.log main;
log_format:規(guī)定格式
remote_addr:客戶端ip地址
remote_user:遠(yuǎn)程用戶 默認(rèn)是空
[time_local] :當(dāng)前時(shí)間
request:請(qǐng)求起始行
status:狀態(tài)碼
body_bytes_sent:服務(wù)器向客戶相應(yīng)的內(nèi)容的大小
http_referer:用戶從哪里跳轉(zhuǎn)
http_user_agent:用戶使用的瀏覽器
http_x_forwarded_for:記錄用戶真實(shí)的ip地址(使用負(fù)載均衡時(shí)涉及到)
location匹配規(guī)則 匹配uri
應(yīng)用場(chǎng)景
- 隱藏網(wǎng)站重要目錄(禁止訪問)
- 網(wǎng)站加速 讓用戶訪問緩存(CDN)
“~”用于區(qū)分大小寫(大小寫敏感)的匹配漱竖; ~ /images {}
“~” 用于不區(qū)分大小寫的匹配禽篱。還可以用邏輯操作符!對(duì)上面的匹配取反闲孤,即!~ 和 !~谆级。
“^~”作用是在常規(guī)的字符串匹配檢查之后,不做正則表達(dá)式的檢查
location = / { www.oldboy.org符合) www.oldboy.org/ 符合) www.oldboy.org/index.html (不符合)
[ configuration A ]
}
location / { 默認(rèn)規(guī)則 其他條件都不匹配的時(shí)候
[ configuration B ]
}
location /documents/ { 匹配路徑 www.oldboy.org/admin/
[ configuration C ]
}
location /admin/ { www.oldboy.org/images/admin
你不能訪問
}
nginx狀態(tài)模塊
root@web01 conf.d]# curl 10.0.0.7/status/
Active connections: 1 #與服務(wù)器已經(jīng)建立的連接 當(dāng)前并發(fā)數(shù)量
server accepts handled requests #總數(shù)
4 4 4
Reading: 0 Writing: 1 Waiting: 0
server accepts 一共接收多少請(qǐng)求
handled 一共處理多少請(qǐng)求
requests 客戶一共發(fā)起了多少次請(qǐng)求
Reading: 0 正在讀取多少個(gè)用戶的請(qǐng)求頭 (請(qǐng)求)
Writing: 1 正在響應(yīng)多少個(gè)用戶的請(qǐng)求 (響應(yīng))
Waiting: 0 有多少個(gè)用戶在等待
workerprocess
nginx簡(jiǎn)易的認(rèn)證
- auth_basic "closed site";
- auth_basic_user_file conf/htpasswd;
yum install httpd-tools -y
配置模塊內(nèi)容(注意使用絕對(duì)路徑)
location /status/ {
stub_status ;
auth_basic "oldboy trainning";
auth_basic_user_file conf/htpasswd;
}
htpasswd -bc /application/nginx-1.14.2/conf/htpasswd oldboy 123456
chmod 400 /application/nginx-1.14.2/conf/htpasswd
chown www /application/nginx-1.14.2/conf/htpasswd
- 修改vim conf.d/www.conf
server {
listen 80;
server_name www.wsm.com;
location / {
root html/www;
index index.html index.htm;
}
location /status/ {
stub_status ;
auth_basic "oldboy trainning";
auth_basic_user_file /application/nginx-1.14.2/conf/htpasswd;
}
}
- 檢查語法并重啟nginx
[root@test01 conf]# nginx -t
nginx: the configuration file /application/nginx-1.14.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.14.2/conf/nginx.conf test is successful
[root@test01 conf]# nginx -s reload
- 在瀏覽器上驗(yàn)證
訪問http://www.wsm.com/status/
頁面顯示
image.png
輸入用戶密碼登陸成功
image.png
nginx rewrite功能
nginx處理用戶請(qǐng)求(靜態(tài)頁面)
動(dòng)態(tài)頁面交給PHP代碼(php處理)或 java代碼(tomcat處理)
- 靜態(tài)頁面 用戶瀏覽器進(jìn)行處理 服務(wù)器只負(fù)責(zé) 請(qǐng)求與響應(yīng) 處理速度快 支持并發(fā)大
- 動(dòng)態(tài) 頁面 服務(wù)器進(jìn)行處理 把處理完成的結(jié)果 發(fā)送給用戶 速度慢 支持并發(fā)不高 一般涉及到數(shù)據(jù)庫
修改配置文件,添加rewrite
[root@test01 conf]# cat conf.d/www.conf
server {
listen 80;
server_name gcy.org;
rewrite (^.*$) http://www.wsm.com/$1 permanent;
}
server {
listen 80;
server_name www.wsm.com;
location / {
root html/www;
index index.html index.htm;
}
location /status/ {
stub_status ;
auth_basic "oldboy trainning";
auth_basic_user_file /application/nginx-1.14.2/conf/htpasswd;
}
}
執(zhí)行curl -Lv gcy.org 跟隨跳轉(zhuǎn)到新地址
[root@test01 conf]# curl -Lv gcy.org
* About to connect() to gcy.org port 80 (#0)
* Trying 10.0.0.111...
* Connected to gcy.org (10.0.0.111) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: gcy.org
> Accept: */*
>
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.14.2
< Date: Wed, 19 Jun 2019 21:14:55 GMT
< Content-Type: text/html
< Content-Length: 185
< Connection: keep-alive
< Location: http://www.wsm.com//
<
* Ignoring the response-body
* Connection #0 to host gcy.org left intact
* Issue another request to this URL: 'http://www.wsm.com//'
* About to connect() to www.wsm.com port 80 (#1)
* Trying 10.0.0.111...
* Connected to www.wsm.com (10.0.0.111) port 80 (#1)
> GET // HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.wsm.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Server: nginx/1.14.2
< Date: Wed, 19 Jun 2019 21:14:55 GMT
< Content-Type: text/html
< Content-Length: 8
< Last-Modified: Tue, 18 Jun 2019 23:01:15 GMT
< Connection: keep-alive
< ETag: "5d096d3b-8"
< Accept-Ranges: bytes
<
www gcy
* Connection #1 to host www.wsm.com left intact
rewrite 小結(jié)
- 1.用戶訪問的url和實(shí)際url不同 與開發(fā)對(duì)接 url規(guī)則
- 2.偽靜態(tài) 更容易被搜索引擎 收入
- 3.新老網(wǎng)站變化