2019-05-11 Nginx Web應(yīng)用深入

Day45

作者:方
歸檔:筆記
時(shí)間:2019/5/5

Nginx Web應(yīng)用深入

image.png
image.png
image.png

Nginx功能模塊化:

模塊化是為了耦合度更低,易于管理!工作中做事學(xué)會(huì)低耦合。

SQA架構(gòu)。RPC服務(wù)都屬于低耦合的技術(shù)模式。

image.png
image.png
image.png
image.png

配置文件了信息詳情:cat -n nginx.conf

image.png
image.png

實(shí)踐基于域名的虛擬主機(jī):

 第一步:過濾空行生成新的配置文件     

egrep -v "^$|#" nginx.conf.default >nginx.conf

image.png
第二步:vim nginx.conf   后四行干掉
image.png
第三步:創(chuàng)建站點(diǎn)目錄文件index.html,把域名輸入進(jìn)去

mkdir ../html/www

echo "www.etiantian.org" >../html/www/index.html

cat ../html/www/index.html

image.png
第四步:把域名和IP追加到 /etc/hosts

echo "10.0.0.8 www.etiantian.org" >>/etc/hosts

image.png

第五步:ping 域名,驗(yàn)證

tail -1 /etc/hosts

ping www.etiantian.org

image.png

配置變量:

echo 'PATH="/application/nginx/sbin:$PATH"' >>/etc/profile

. /etc/profile

echo $PATH

nginx 檢查語法:

nginx -t

nginx 平滑重啟:

nginx -s reload

驗(yàn)證成功:

curl www.etiantian.org

image.png

WINDOWS下測試:

C:\Windows\System32\drivers\etc\hosts

10.0.0.8 www.etiantian.org

image.png
往配置文件里面添加server標(biāo)簽:
image.png
創(chuàng)建站點(diǎn)目錄文件,把域名和IP信息追加到/etc/hosts
image.png
nginx檢查語法,平滑重啟,curl驗(yàn)證
image.png

基于域名的虛擬主機(jī)通信原理介紹

image.png

基于端口的虛擬主機(jī)實(shí)踐:

1.備份:
image.png
2.修改配置文件內(nèi)的端口即可
image.png
3.檢查語法,沒問題平滑重啟
image.png
4.檢查端口,驗(yàn)證配置結(jié)果(結(jié)尾需加端口)
image.png

基于IP的虛擬主機(jī):(了解即可)

第一步:配置網(wǎng)卡
image.png
第二步:檢查ping
image.png
第三步:修改配置文件
image.png
image.png

防止網(wǎng)站被惡意解析,設(shè)置配置文件:

在配置里,第一個(gè)標(biāo)簽添加:

image.png
image.png

實(shí)踐優(yōu)化nginx文件:

image.png
image.png

優(yōu)化nginx配置文件:

[root@web02 /application/nginx/conf]# mkdir extra

[root@web02 /application/nginx/conf]# sed -n '10,17p' nginx.conf

打印www.etiantian.org虛擬主機(jī)配置文件 server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf]#
sed -n '10,17p' nginx.conf >extra/01_www.conf

[root@web02 /application/nginx/conf]# sed -n '18,25p' nginx.conf
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf]#
sed -n '18,25p' nginx.conf >extra/02_bbs.conf

[root@web02 /application/nginx/conf]# sed -n '26,33p' nginx.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf]#
sed -n '26,33p' nginx.conf** >extra/03_blog.conf

[root@web02 /application/nginx/conf]# cd extra/

[root@web02 /application/nginx/conf/extra]# cat 01_www.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf/extra]# cat 02_bbs.conf
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf/extra]# cat 03_blog.conf
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf/extra]# cd ../

[root@web02 /application/nginx/conf]# sed -n '10,33p' nginx.conf
server {
listen 80;
server_name www.etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
}
server {
listen 80;
server_name bbs.etiantian.org;
location / {
root html/bbs;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.html index.htm;
}
}

[root@web02 /application/nginx/conf]# sed -i '10,33d' nginx.conf **

[root@web02 /application/nginx/conf]#
sed -i '10 i include extra/01_www.conf;\ninclude extra/02_bbs.conf;\ninclude extra/03_blog.conf;' nginx.conf

[root@web02 /application/nginx/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 extra/01_www.conf;
include extra/02_bbs.conf;
include extra/03_blog.conf;
}

[root@web02 /application/nginx/conf]# nginx -t
nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful

[root@web02 /application/nginx/conf]# nginx -s reload

[root@web02 /application/nginx/conf]# curl www.etiantian.org
www.etiantian.org

[root@web02 /application/nginx/conf]# curl bbs.etiantian.org
bbs.etiantian.org

[root@web02 /application/nginx/conf]# curl blog.etiantian.org
blog.etiantian.org

別名:一個(gè)名字另外一個(gè)名字

兩個(gè)域名都可以訪問到相同的內(nèi)容。

image.png
image.png
image.png
配置server標(biāo)簽:
image.png
配置完成后進(jìn)行本機(jī)/etc/hosts解析。
image.png
image.png
image.png
image.png
image.png
image.png
image.png

#log_format main 'remote_addr -remote_user [time_local] "request" '

**#                  '$status $body_bytes_sent "$http_referer" '**

**#                  '"$http_user_agent" "$http_x_forwarded_for"';**
image.png

腳本日志切割

image.png
image.png

if uri then 相當(dāng)于一個(gè)判斷句

image.png
image.png
image.png
image.png

下面是官方給出的的location示例,我們通過實(shí)例來驗(yàn)證不同的location標(biāo)簽生效的順序,Nginx的配置文件為:
[root@web01 conf]# cp extra/01_www.conf{,.ori}

[root@web01 conf]# cat extra/01_www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
root html/www;
location / {
return 401;
}
location = / {
return 402;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 404;

匹配任何以/images/開頭的任何查詢并且停止搜索。任何正則表達(dá)式匹配將不會(huì)被檢查。
"^~" 這個(gè)前綴的作用:在常規(guī)的字符串匹配檢查之后,不做正則表達(dá)式的檢查,即如果最明確的那個(gè)字符串匹配的location配置中有此前綴,那么不會(huì)做正則表達(dá)式的檢查。
}
}
location ~* .(gif|jpg|jpeg)$ {
#匹配任何以 gif、jpg 或 jpeg 結(jié)尾的請(qǐng)求。
return 500;
}
access_log logs/access_www.log main gzip buffer=32k flush=5s;
}

檢查語法并使得修改的配置生效:

[root@web01 conf]# nginx -t

[root@web01 conf]# nginx -s reload

然后以Linux客戶端為例對(duì)上述location匹配進(jìn)行真實(shí)測試,配置hosts文件如下。
[root@web01 conf]# tail -1 /etc/hosts
10.0.0.7 www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org

實(shí)驗(yàn)結(jié)果如下:
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org
402
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/
402
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/index.html
401
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/document.html
403
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/1.gif
404
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/1.jpg
500
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/oldboy/
401
[root@web01 conf]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/abc/
401

image.png

rewrite實(shí)現(xiàn)偽靜態(tài)功能

image.png

301跳轉(zhuǎn):

[root@web02 /application/nginx/conf/extra]# cat 01_www.conf
server {
listen 80;
server_name etiantian.org;
rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
}

server {
    listen      80;
    server_name www.etiantian.org;
    location / {
        root   html/www;
        index  index.html index.htm;
    }

access_log logs/access_www.log main;
}

access_log logs/access_www.log main;

image.png
image.png
image.png

出現(xiàn)403的原因:

1、 沒有首頁文件,index.xxxx

2、 站點(diǎn)目錄權(quán)限太低 755

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • nginx 配置文件 Day44: 2.Nginx3大主要功能 1)網(wǎng)頁服務(wù):自身是靜態(tài)Web服務(wù), apache...
    山有木兮_8adb閱讀 187評(píng)論 0 0
  • LNMP Web服務(wù)搭建 mysql與php安裝 1、JAVA Web環(huán)境(企業(yè)更多) [tomcat(jvm)]...
    高博666閱讀 686評(píng)論 0 0
  • 每日媒介轉(zhuǎn)化量及轉(zhuǎn)化成本分析 需要數(shù)據(jù)來源如下 廣告端:分日關(guān)鍵詞表、賬戶結(jié)構(gòu) 業(yè)務(wù)端:轉(zhuǎn)化分日表、日期表 ---...
    拓拓熊閱讀 976評(píng)論 0 0
  • 昨天參加公司年會(huì),同時(shí)參加的還有我的老大,我的代理。我們素萬的女神慧總監(jiān)代表公司出面,給我們講解了素萬2019年的...
    水墨丹青28閱讀 175評(píng)論 0 0
  • 要寫這篇文,才想起來要看一下日期,原來已經(jīng)是這個(gè)月最后一天了。 前一天加班留下的疲憊還在,可以晚一小時(shí)上班也算一點(diǎn)...
    縛己的顧離閱讀 128評(píng)論 0 0

友情鏈接更多精彩內(nèi)容