linux學習:【第4篇】之nginx

 

1、知識點回顧html

複製代碼
臨時:關閉當前正在運行的
/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 ,,看日誌的時候不用這個
#查看日誌
    一、less
    二、head/dail
    三、grep 
f或空格 #向下翻頁
b #想上翻頁
grep 'Failed password' secure-20161219
複製代碼

2、練習題linux

一、簡述一下linux的啓動過程nginx

  • 按下電源
  • 開機自檢(檢查內存cpu 硬件是否有問題)
  • MBR引導 #默認從硬盤的MBR啓動
  • GRUB菜單 #啓動的時候3 2 1 那個就是CRUB菜單
  • 加載內核
  • 運行INIT進程 #linux的第一個進程
  • 讀取/etc/inittab配置文件
  • 執行/etc/rc.d/rc.sysinit腳本
  • 執行/etc/rc.d/rc腳本 #根據系統的運行級別,在開機的時候啓動不一樣的軟件
  • 啓動mungetty進程

二、http的請求與響應過程web

  • 一、域名解析
  • 二、瀏覽器發送tcp請求,創建tcp三次握手
  • 三、發送http請求,
  • 四、服務器收到請求返回給瀏覽器
  • 五、瀏覽器收到返回的結果,在頁面進行渲染,解析HTML代碼
  • 六、關閉鏈接

請求包括的:正則表達式

  • 請求首行
  • 請求頭
  • 空行
  • 請求體

響應包括的:shell

  • 響應首行
  • 響應頭
  • 空行
  • 響應體
    curl www.baidu.com  #顯示響應的內容
    curl -v www.baidu.com  #顯示請求和響應的過程
    > 表示請求
    < 表示響應

3、nginx數據庫

Ngix是web服務器,跟apache同樣,它能夠作動態請求轉發、web端負載均衡、反向代理等等;
tomcat是應用服務器,固然若是非用逼良爲娼,也可作web服務器用,它主要是作Servlet容器用的,通常用在應用層,運行後臺邏輯代碼,訪問數據庫服務器等;

通常常見的網站,採用apache+tomcat+數據庫或是Ngix+tomcat+數據庫這三層物理架構;若是是大型網站應用,上面還會有一層硬件負載均衡F5。

從性能角度來說Ngix的性能彷佛要比apache略好,但從使用方便上來看,apache配置起來更方便,功能上都很強大,由於apache名氣彷佛更大一些,用的人就更多一些。

一、下載apache

[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通常偶數版的是穩定版本 ,基數是測試版

二、編譯安裝nginx前的準備windows

useradd -s /sbin/nplogin -M www #添加一個用戶,-s和-M是吧用戶變成虛擬用戶號
id www
su - www

三、安裝nginx的依賴包瀏覽器

#pcre-devel     perl語言的兼容正則表達式
#openssl-devel  https
yum install pcre-devel -y 
yum install openssl-devel -y 
rpm -qa pcre-devel

四、編譯安裝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 #給這個壓縮包建立個指紋,至關於身份證
複製代碼

五、查看

/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裏面查看

六、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

4、搭建一個小網站

一、修改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
複製代碼

五、排錯流程:

1.linux命令行是否能顯示 
curl -vH Host:www.etiantian.org 10.0.0.200

2.windows 本地shell 
ping

六、搭建多個

複製代碼
[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
複製代碼

5、參數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
複製代碼
相關文章
相關標籤/搜索