Day45
作者:方
歸檔:筆記
時(shí)間:2019/5/5
Nginx Web應(yīng)用深入
Nginx功能模塊化:
模塊化是為了耦合度更低,易于管理念祭!工作中做事學(xué)會(huì)低耦合。
SQA架構(gòu)碍侦。RPC服務(wù)都屬于低耦合的技術(shù)模式粱坤。
配置文件了信息詳情:cat -n nginx.conf
實(shí)踐基于域名的虛擬主機(jī):
第一步:過濾空行生成新的配置文件
egrep -v "^$|#" nginx.conf.default >nginx.conf
第二步:vim nginx.conf 后四行干掉
第三步:創(chuàng)建站點(diǎn)目錄文件index.html,把域名輸入進(jìn)去
mkdir ../html/www
echo "www.etiantian.org" >../html/www/index.html
cat ../html/www/index.html
第四步:把域名和IP追加到 /etc/hosts
echo "10.0.0.8 www.etiantian.org" >>/etc/hosts
第五步:ping 域名祝钢,驗(yàn)證
tail -1 /etc/hosts
ping www.etiantian.org
配置變量:
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
WINDOWS下測(cè)試:
C:\Windows\System32\drivers\etc\hosts
10.0.0.8 www.etiantian.org
往配置文件里面添加server標(biāo)簽:
創(chuàng)建站點(diǎn)目錄文件比规,把域名和IP信息追加到/etc/hosts
nginx檢查語法,平滑重啟拦英,curl驗(yàn)證
基于域名的虛擬主機(jī)通信原理介紹
基于端口的虛擬主機(jī)實(shí)踐:
1.備份:
2.修改配置文件內(nèi)的端口即可
3.檢查語法蜒什,沒問題平滑重啟
4.檢查端口,驗(yàn)證配置結(jié)果(結(jié)尾需加端口)
基于IP的虛擬主機(jī):(了解即可)
第一步:配置網(wǎng)卡
第二步:檢查ping
第三步:修改配置文件
防止網(wǎng)站被惡意解析疤估,設(shè)置配置文件:
在配置里灾常,第一個(gè)標(biāo)簽添加:
實(shí)踐優(yōu)化nginx文件:
優(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)容。
配置server標(biāo)簽:
配置完成后進(jìn)行本機(jī)/etc/hosts解析铃拇。
#log_format main 'remote_user [
request" '
**# '$status $body_bytes_sent "$http_referer" '**
**# '"$http_user_agent" "$http_x_forwarded_for"';**
腳本日志切割
if uri then 相當(dāng)于一個(gè)判斷句
下面是官方給出的的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í)測(cè)試,配置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
rewrite實(shí)現(xiàn)偽靜態(tài)功能
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;
出現(xiàn)403的原因:
1躏救、 沒有首頁文件,index.xxxx
2螟蒸、 站點(diǎn)目錄權(quán)限太低 755