一、知識點回顧
臨時:關閉當前正在運行的 /etc/init.d/iptables stop 永久:關閉開機自啟動 chkonfig iptables off ll /var/log/secure # 用戶訪問系統的記錄,誰在什么時間鏈接了你的服務器,什么時候鏈接了 #安裝lrzsz yum install lrzsz -y rz #windows文件上傳到linux sz #吧linux文件下載到windows rpm -pa lrzsz #查看安裝好了沒有 .tar.gz .zip #這種格式的壓縮包是linux和windows默認支持的 unzip secure-20161219.zip #解壓 xshell #遠程連接 xftp #ftp工具 https://www.netsarang.com/download/down_form.html?code=523 #查看文件內容用cat ,,看日志的時候不用這個 #查看日志 1、less 2、head/dail 3、grep f或空格 #向下翻頁 b #想上翻頁 grep 'Failed password' secure-20161219
二、練習題
1、簡述一下linux的啟動過程
- 按下電源
- 開機自檢(檢查內存cpu 硬件是否有問題)
- MBR引導 #默認從硬盤的MBR啟動
- GRUB菜單 #啟動的時候3 2 1 那個就是CRUB菜單
- 加載內核
- 運行INIT進程 #linux的第一個進程
- 讀取/etc/inittab配置文件
- 執行/etc/rc.d/rc.sysinit腳本
- 執行/etc/rc.d/rc腳本 #根據系統的運行級別,在開機的時候啟動不同的軟件
- 啟動mungetty進程
2、http的請求與響應過程
- 1、域名解析
- 2、瀏覽器發送tcp請求,建立tcp三次握手
- 3、發送http請求,
- 4、服務器收到請求返回給瀏覽器
- 5、瀏覽器收到返回的結果,在頁面進行渲染,解析HTML代碼
- 6、關閉連接
請求包括的:
- 請求首行
- 請求頭
- 空行
- 請求體
響應包括的:
- 響應首行
- 響應頭
- 空行
- 響應體
curl www.baidu.com #顯示響應的內容 curl -v www.baidu.com #顯示請求和響應的過程 > 表示請求 < 表示響應
三、nginx
Ngix是web服務器,跟apache一樣,它可以做動態請求轉發、web端負載均衡、反向代理等等; tomcat是應用服務器,當然如果非用逼良為娼,也可做web服務器用,它主要是做Servlet容器用的,一般用在應用層,運行后台邏輯代碼,訪問數據庫服務器等; 一般常見的網站,采用apache+tomcat+數據庫或是Ngix+tomcat+數據庫這三層物理架構;如果是大型網站應用,上面還會有一層硬件負載均衡F5。 從性能角度來講Ngix的性能似乎要比apache略好,但從使用方便上來看,apache配置起來更方便,功能上都很強大,因為apache名氣似乎更大一些,用的人就更多一些。
1、下載
[root@s6haiyanvm ~]# wget http://nginx.org/download/nginx-1.12.2.tar.gz ll -h nginx-1.12.2.tar.gz #查看多大 tar xf nginx-1.12.2.tar.gz #解壓 cd nginx-1.12.2.tar.gz ls -l 12一般偶數版的是穩定版本 ,基數是測試版
2、編譯安裝nginx前的准備
useradd -s /sbin/nplogin -M www #添加一個用戶,-s和-M是吧用戶變成虛擬用戶號 id www su - www
3、安裝nginx的依賴包
#pcre-devel perl語言的兼容正則表達式 #openssl-devel https yum install pcre-devel -y yum install openssl-devel -y rpm -qa pcre-devel
4、編譯安裝nginx
pwd #應該是在這個路徑下/application/nginx-1.12.2 ./configure --help (1)./configure --user=www --group=www --prefix=/application/nginx-1.12.2 --with-http_stub_status_module --with-http_ssl_module --user :用戶 --group : 用戶組 --prefix :安裝到哪里 兩個模塊: --with-http_stub_status_module --with-http_ssl_module tree -L echo $? #表示以上一次的執行情況 ,返回0表示正確,返回1表示錯誤 (2)make #根據一些配置進行編譯 (3)make install md5sum nginx/1.12.2.tar.gz #給這個壓縮包創建個指紋,相當於身份證
5、查看
/application/nginx-1.12.2/sbin/nginx -t #檢查是否語法錯誤 /application/nginx-1.12.2/sbin/nginx #執行命令 ss -lntup|grep 80 #查看安裝好了沒有 10.0.0.200 #在瀏覽器里面看頁面,看搭建好了沒有 curl -v 10.0.0.200 #在linux里面查看
6、nginx的常用配置
[root@oldboyedu-s6 nginx-1.12.2]# pwd /application/nginx-1.12.2 [root@oldboyedu-s6 nginx-1.12.2]# ll total 36 drwx------ 2 www root 4096 Mar 15 10:31 client_body_temp drwxr-xr-x 2 root root 4096 Mar 15 10:00 conf nginx配置文件的目錄 drwx------ 2 www root 4096 Mar 15 10:31 fastcgi_temp drwxr-xr-x 2 root root 4096 Mar 15 10:00 html nginx站點目錄 網站的根目錄 drwxr-xr-x 2 root root 4096 Mar 15 10:31 logs nginx日志 drwx------ 2 www root 4096 Mar 15 10:31 proxy_temp drwxr-xr-x 2 root root 4096 Mar 15 10:00 sbin nginx命令 drwx------ 2 www root 4096 Mar 15 10:31 scgi_temp drwx------ 2 www root 4096 Mar 15 10:31 uwsgi_temp
├── client_body_temp
├── conf
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf #nginx的主配置文件
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp
├── html
│ ├── 50x.html
│ └── index.html #網站默認的首頁文件
├── logs
│ ├── access.log #訪問日志
│ ├── error.log
│ └── nginx.pid
├── proxy_temp
├── sbin
│ └── nginx
├── scgi_temp
└── uwsgi_temp
#修改配置文件 egrep '#' #查看包含#的 egrep '^$' #空行 egrep '^$|#' conf/nginx.conf #排除空行或者# egrep '^$|#' conf/nginx.conf.default > conf/nginx.conf
修改了配置文件記得重啟一下 /application/nginx-1/sbin/nginx -s reload #優雅的重啟 /application/nginx-1/sbin/nginx -s stop #優雅的停止(迫不得已才用的) /application/nginx-1/sbin/nginx
四、搭建一個小網站
1、修改nginx.conf文件
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 www.etiantian.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
2.創建環境
mkdir -p /application/nginx-1.12.2/html/{www,bbs,blog} #創建多級目錄
for name in www bbs blog;do echo $name.etiantian.org> /application/nginx-1.12.2/html/$name/index.html ;done for name in www bbs blog;do cat /application/nginx-1.12.2/html/$name/index.html ;done
3.檢查語法並重啟
[root@oldboyedu-s6 nginx-1.12.2]# /application/nginx-1.12.2/sbin/nginx -t nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful [root@oldboyedu-s6 nginx-1.12.2]# /application/nginx-1.12.2/sbin/nginx -s reload
4.windows測試 瀏覽器(注意緩存)
(1)方式一:cat /etc/hosts #修改host,域名解析 10.0.0.200 www.etiantian.org bbs.etiantian.org blog.etiantian.org www.eitiantian.org #瀏覽器輸入測試 (2)方式二: curl -H Host:www.etiantian.org 10.0.0.200 #修改host里面的host
5、排錯流程:
1.linux命令行是否能顯示 curl -vH Host:www.etiantian.org 10.0.0.200 2.windows 本地shell ping
6、搭建多個
[root@oldboyedu-s6 nginx-1.12.2]# cat conf/nginx.conf 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 www.etiantian.org; location / { root html/www; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name bbs.etiantian.org; location / { root html/bbs; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } server { listen 80; server_name blog.etiantian.org; location / { root html/blog; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } } [root@oldboyedu-s6 nginx-1.12.2]# curl -H Host:bbs.etiantian.org 10.0.0.200 bbs.etiantian.org [root@oldboyedu-s6 nginx-1.12.2]# curl -H Host:blog.etiantian.org 10.0.0.200 blog.etiantian.org
五、參數location的理解
“~”用於區分大小寫(大小寫敏感)的匹配; ~ /images {} “~*” 用於不區分大小寫的匹配。還可以用邏輯操作符!對上面的匹配取反,即!~ 和 !~*。 “^~”作用是在常規的字符串匹配檢查之后,不做正則表達式的檢查,即如果最明確的那個字符串匹配的location配置中有此前綴,那么不做正則表達式的檢查。
[root@oldboyedu-s6 nginx-1.12.2]# cat conf/nginx.conf 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 www.etiantian.org etiantian.org; root html/www; location / { return 401; } location /documents/ { return 403; } location ^~ /images/ { return 404; } location ~* \.(gif|jpg|jpeg)$ { return 500; } } } ^~ ~* /documents/ / 第1名:“location ~* \.(gif|jpg|jpeg)$ {” 正則匹配 第2名:“location /documents/ {” 匹配常規字符串,如果有正則則優先匹配正則。 第3名:“location / {” 所有location都不能匹配后的默認匹配。 [root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200 HTTP/1.1 401 Unauthorized Server: nginx/1.12.2 Date: Thu, 15 Mar 2018 04:13:41 GMT Content-Type: text/html Content-Length: 195 Connection: keep-alive [root@oldboyedu-s6 nginx-1.12.2]# [root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200/documents/index.html HTTP/1.1 403 Forbidden Server: nginx/1.12.2 Date: Thu, 15 Mar 2018 04:14:42 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive [root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200/documents/w.jpg HTTP/1.1 500 Internal Server Error Server: nginx/1.12.2 Date: Thu, 15 Mar 2018 04:15:56 GMT Content-Type: text/html Content-Length: 193 Connection: close [root@oldboyedu-s6 nginx-1.12.2]# [root@oldboyedu-s6 nginx-1.12.2]# curl -I 10.0.0.200/images/www.jpg HTTP/1.1 404 Not Found Server: nginx/1.12.2 Date: Thu, 15 Mar 2018 04:16:52 GMT Content-Type: text/html Content-Length: 169 Connection: keep-alive