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!!! 利于排错,但是不安全!