Nginx相關(guān)配置使用

用戶訪問網(wǎng)站的流程

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 三步

[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

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
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)站變化
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末讼积,一起剝皮案震驚了整個(gè)濱河市肥照,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌勤众,老刑警劉巖舆绎,帶你破解...
    沈念sama閱讀 222,104評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異们颜,居然都是意外死亡吕朵,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,816評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門窥突,熙熙樓的掌柜王于貴愁眉苦臉地迎上來努溃,“玉大人,你說我怎么就攤上這事阻问∥嗨埃” “怎么了?”我有些...
    開封第一講書人閱讀 168,697評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵称近,是天一觀的道長(zhǎng)第队。 經(jīng)常有香客問我,道長(zhǎng)刨秆,這世上最難降的妖魔是什么凳谦? 我笑而不...
    開封第一講書人閱讀 59,836評(píng)論 1 298
  • 正文 為了忘掉前任,我火速辦了婚禮衡未,結(jié)果婚禮上尸执,老公的妹妹穿的比我還像新娘。我一直安慰自己缓醋,他們只是感情好剔交,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,851評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著改衩,像睡著了一般。 火紅的嫁衣襯著肌膚如雪驯镊。 梳的紋絲不亂的頭發(fā)上葫督,一...
    開封第一講書人閱讀 52,441評(píng)論 1 310
  • 那天竭鞍,我揣著相機(jī)與錄音,去河邊找鬼橄镜。 笑死偎快,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的洽胶。 我是一名探鬼主播晒夹,決...
    沈念sama閱讀 40,992評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼姊氓!你這毒婦竟也來了丐怯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,899評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤翔横,失蹤者是張志新(化名)和其女友劉穎读跷,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體禾唁,經(jīng)...
    沈念sama閱讀 46,457評(píng)論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡效览,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,529評(píng)論 3 341
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了荡短。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片丐枉。...
    茶點(diǎn)故事閱讀 40,664評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖掘托,靈堂內(nèi)的尸體忽然破棺而出瘦锹,到底是詐尸還是另有隱情,我是刑警寧澤烫映,帶...
    沈念sama閱讀 36,346評(píng)論 5 350
  • 正文 年R本政府宣布沼本,位于F島的核電站,受9級(jí)特大地震影響锭沟,放射性物質(zhì)發(fā)生泄漏抽兆。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,025評(píng)論 3 334
  • 文/蒙蒙 一族淮、第九天 我趴在偏房一處隱蔽的房頂上張望辫红。 院中可真熱鬧,春花似錦祝辣、人聲如沸贴妻。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,511評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽名惩。三九已至,卻和暖如春孕荠,著一層夾襖步出監(jiān)牢的瞬間娩鹉,已是汗流浹背攻谁。 一陣腳步聲響...
    開封第一講書人閱讀 33,611評(píng)論 1 272
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留弯予,地道東北人戚宦。 一個(gè)月前我還...
    沈念sama閱讀 49,081評(píng)論 3 377
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像锈嫩,于是被迫代替她去往敵國(guó)和親受楼。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,675評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容