1. 程式人生 > >視頻學習記錄和規劃day12

視頻學習記錄和規劃day12

記錄

2017年5月23日 周二 第一章 前1h
2017年5月24日 周三 第一章 後2.5h
2017年5月25日 周四 第二章 前2h年5月26日 周五 第二章 後2h
2017年5月27日 周六第三章 4h (我擦,俄噶看得完呀!)



顯示解析的整個過程
叠代就是遞歸的一部分!

[[email protected] ~]# curl -I www.baidu.com #只看報文頭
HTTP/1.1 200 OK
Server: bfe/1.0.8.18
Date: Wed, 24 May 2017 13:06:35 GMT
Content-Type: text/html
Content-Length: 277

Last-Modified: Mon, 13 Jun 2016 02:50:12 GMT
Connection: Keep-Alive
ETag: "575e1f64-115"
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Pragma: no-cache
Accept-Ranges: bytes

http://oldboy.blog.51cto.com/search.php? 回頭看看去
報文 即 數據包!


技術復雜很多,性能還低很多,這很奇怪嗎? 不奇怪,因為它提供的功能也很多!
偽靜態缺點: 性能不升反降!


PV就是一個用戶打開的頁面數量

2017年5月25日 20:28:24-

2017年5月26日 16:12:18-
linux裏面軟件安裝方法:
1、rpm -ivh 包名.rpm
有依賴問題,安裝A,需要先安裝B
2、yum安裝自動解決rpm安裝的依賴問題,安裝更簡單化。
優點:簡單、易用、高效
缺點:不能定制
3、編譯(C語言源碼-編譯二進制等)
./configure(配置),make(編譯),make install(安裝)
優點:可以定制
缺點:復雜、效率底。
4、定制化制作rpm包,搭建yum倉庫,把我定制的rpm包放到yum倉庫,進行yum安裝
優點:結合了2的優點和3的優點。
缺點:復雜

Nginx web服務器的安裝:
yum install gcc gcc-c++ ncurses-devel perl -y #解決編譯的問題
yum install openssl openssl-devel -y #安裝openssl
yum -y install pcre pcre-devel #安裝nginx環境
mkdir /home/oldboy/tools
cd /home/oldboy/tools
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
tar -zxvf nginx-1.6.3.tar.gz
cd nginx-1.6.3
useradd www -s /sbin/nologin -M
./configure --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --prefix=/application/nginx-1.6.3/
make
make install
ln -s /application/nginx-1.6.3/ /application/nginx

[[email protected] nginx-1.6.3]# netstat -lntup |grep 80 #代表成功
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 8582/nginx
[[email protected] nginx-1.6.3]# lsof -i :80 #代表成功
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 8582 root 6u IPv4 22950 0t0 TCP *:http (LISTEN)
nginx 8583 www 6u IPv4 22950 0t0 TCP *:http (LISTEN)



檢測是否安裝成功 用curl 127.1 或者wget 127.1

故障分析:
如果安裝出現在下面的錯誤是缺少編譯環境。
./configure: error: C compiler cc is not found

yum install gcc gcc-c++ ncurses-devel perl #安裝編譯源碼所需的工具和庫

yum groupinstall -y "Base" "Compatibility libraries" "Debugging Tools" "Development tools"
#其實更大的原因是因為之前沒有按照老男孩老師講的去安裝 相關環境包,後期可以直接補上把!

[[email protected] conf]# egrep -v "^$|#" nginx.conf.default #最小化學習 過濾註釋
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}




[[email protected] conf]# egrep -v "^$|#" nginx.conf.default #最小化學習 過濾註釋
worker_processes 1; #work進程數量(服務員數量)
events { #模型
worker_connections 1024; #每個進程最大連接數(接客數量)
}
http {
include mime.types; #包含多媒體類型(類型在mime.types文件裏面)
default_type application/octet-stream; #默認的一個媒體類型
sendfile on; #開啟高效模式(後期優化會講到)
keepalive_timeout 65; #連接超時時間
server { #對應一個網站
listen 80; #默認監聽的網站端口
server_name localhost; #域名
location / { #匹配
root html; #站點根目錄
index index.html index.htm; #首頁文件
}
error_page 500 502 503 504 /50x.html; #出現錯誤的頁面 優雅顯示的內容
location = /50x.html {
root html;
}
}
}

這個做法很好 以後可以借鑒到我平常的工作文檔處理中去

當你輸入域名,hosts解析域名得到ip,發起tcp連接到80端口,再建立http連接,http請求頭,含有主機名,當請求頭(www.etiantian.org)到達服務器,查找nginx.conf確定是哪個虛擬主機,再讀站點目錄,再到首頁文件,沒有首頁文件,返回403狀態碼!

兩種添加ip別名的方法:
[[email protected] conf]# ifconfig eth0:0 10.0.0.101/24 up
[[email protected] conf]# ip addr add 10.0.0.102/24 dev eth0 label eth0:1


[[email protected] extra]# /application/nginx/sbin/nginx -s stop #停止nginx
[[email protected] extra]# /application/nginx/sbin/nginx #開啟nginx
[[email protected] extra]# lsof -i :80 #驗證開啟
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 21817 root 6u IPv4 28915 0t0 TCP *:http (LISTEN)
nginx 21818 www 6u IPv4 28915 0t0 TCP *:http (LISTEN)

nginx調優:
[[email protected] extra]# 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/*.conf;
}
[[email protected] extra]# tree ./
./
├── bbs.conf
├── blog.conf
└── www.conf

0 directories, 3 files


監控nginx狀態:
[[email protected] extra]# cat status.conf
###status
server{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
}
}
測試監控nginx:
[[email protected] ~]# curl status.etiantian.org
Active connections: 1
server accepts handled requests
62 62 128
Reading: 0 Writing: 1 Waiting: 0
這個稍微重要點!
添加監控模塊:
[[email protected] conf]# vim nginx.conf

worker_processes 1;
events {
worker_connections 1024;
}
error_log logs/error.log error;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/*.conf;
}

設置日誌參數等:
[[email protected] conf]# vim nginx.conf

worker_processes 1;
events {
worker_connections 1024;
}
error_log logs/error.log error;
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
include extra/*.conf;
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;
}

mv 只是涉及到 文件的指向 不涉及到文件本身 不消耗磁盤IO

nginx的日誌切割一般使用的是cron+scripts (定時任務和腳本):
[[email protected] scripts]# cat cut_nginx_log.sh
cd /application/nginx/logs
/bin/mv access.log access_$(date +%F).log
/application/nginx/sbin/nginx -s reload
[[email protected] scripts]# crontab -l
#time sync by 20has at 2017-5-9
*/5 * * * * /usr/sbin/ntpdate time.nist.gov &>/dev/null
#
#bak.sh by 20has at 20170514
00 00 * * * /bin/sh /server/scripts/bak.sh &>/dec/null
#
#cron+scripts to cut nginx logs by 20has at 20170527
00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh &>/dec/null

切割日誌然後rsync推送到backup服務器成功!!!
[[email protected] scripts]# sh -x cut_nginx_log.sh
+ cd /application/nginx/logs
++ date +%F
+ /bin/mv access.log access_2017-05-27.log
+ /application/nginx/sbin/nginx -s reload
++ date +%F
+ rsync -az /application/nginx/logs/access_2017-05-27.log [email protected]::webbackup --password-file=/etc/rsync.password
+ xargs rm -f
+ find /application/nginx/logs -type f -name ‘^access*.log‘ -mtime +180
[[email protected] ~]# ll /webbackup/
總用量 0
-rw-r--r-- 1 rsync rsync 0 2017-05-27 23:28 access_2017-05-27.log


2017年5月29日 11:03:01-

測試狀態碼的前提:
[[email protected] ~]# cat /application/nginx/conf/extra/www.conf
server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
location /documents/ {
return 403;
}
location ^~ /images/ {
return 405;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}
只取狀態碼的命令行:
[[email protected] ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/
index.html
200
[[email protected] ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/documents/
403
[[email protected] ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/images/
405
[[email protected] ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://www.etiantian.org/
a.jpeg
500



小實例:
[[email protected] extra]# vim www.conf

server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html/www;
index index.html index.htm;
}
location /documents/ {
root html/www;
index index.html;
}
location ^~ /images/ {
return 405;
}
location ~* \.(gif|jpg|jpeg)$ {
return 500;
}
}
[[email protected] ~]# curl www.etiantian.org/documents/
doc
[[email protected] ~]# curl www.etiantian.org
www


範例:
[[email protected] extra]# vim www.conf

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

location ^~ /images/ {
rewrite ^/(.*) http://blog.etiantian.org/$1 permanent;
}
}
老域名轉到新域名,用rewrite比較好,頁面會顯示新網址!

別名方式,效率高!
[[email protected] ~]# curl -s -o /dev/null -I -w "%{http_code}\n" http://jd.com
302
摘取自:http://oldboy.blog.51cto.com/2561410/1774260

[[email protected] ~]# rpm -qf /usr/bin/htpasswd
httpd-tools-2.2.15-59.el6.centos.x86_64
[[email protected] ~]# rpm -qa httpd-tools
httpd-tools-2.2.15-59.el6.centos.x86_64


[[email protected] conf]# vim extra/www.conf #配置用戶訪問和密碼驗證

server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html/www;
index index.html;
auth_basic "oldboy traning";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
location /jd {
rewrite http://jd.com permament;
}
}

#配置用戶訪問和密碼文件
[[email protected] conf]# htpasswd -cb /application/nginx/conf/htpasswd 20has 123456
Adding password for user 20has
[[email protected] conf]# chmod 400 /application/nginx/conf/htpasswd

#檢查語法和重啟nginx服務
[[email protected] conf]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3//conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3//conf/nginx.conf test is successful
[[email protected] conf]# /application/nginx/sbin/nginx -s reload



出現403報錯的原因和故障重現:
1、index.html文件丟失
2、index.html沒有讀取權限(默認644)
3、www.conf沒有設置index路徑為index.html (或者說沒有設置首頁文件路徑)
4、*.conf裏面多了 autoindex on 會導致網頁像ftp界面那樣 呈現列表下載模式
拓展閱讀:http://oldboy.blog.51cto.com/2561410/1633952 Nginx 403 forbidden多種原因及故障模擬重現
我發現如果把autoindex放到www.conf的location最下面的話 執行完前面的root和index後就不會執行autoindex了 只有前面執行有問題的時候後面才會執行autoindex!!! 利於排錯,但是不安全!






視頻學習記錄和規劃day12