1. 程式人生 > >堅持#第264天~nginx

堅持#第264天~nginx

正向代理和透明代理主要是上網

反向代理是安全性高、加速作用(訪問代理伺服器的快取,有的話直接使用快取,沒有就直接向真實的伺服器請求然後將快取存放在代理伺服器裡面)

客戶端在伺服器就是反向代理

squid反向代理使用了acl和http_access

ngix:

nginx簡介:和httpd、apache一樣都是網頁服務

ngix是由俄羅斯人提出的

單執行緒,高效能,高穩定,編譯安裝比apache簡單

為了解決萬集併發c10k

Nginx官網提供了三個型別的版本
Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發版
Stable version:最新穩定版,生產環境上建議使用的版本
Legacy versions:遺留的老版本的穩定版

我們使用的是1.10.3的版本

start:

useradd -M-s /sbin/nologin -r nginx

useradd -M-s /sbin/nologin nginx是上面那個

將nginx-1.10.3.tar.gz拷貝並解壓進入那個目錄

yum install-y gcc*

./configure--user=nginx --group=nginx --with-http_stub_status_module

yum install-y pcre-devel報錯需要PCRE庫

./configure--user=nginx --group=nginx --with-http_stub_status_module

yum install-y zlib-devel報錯需要zlib庫

./configure--user=nginx --group=nginx --with-http_stub_status_module

make&& make install

cd/usr/local/nginx

ls

cd sbin

ls

ln -s/usr/local/nginx/sbin/nginx /usr/bin

nginx -?{

-t 測試配置檔案有沒有語法錯誤

-T測試配置檔案,備份並退出

-q測試配置檔案時沒有錯誤不輸出錯誤資訊

-s 連線訊號,給主機發送主程序,nginx stop/quit/reopen重新開啟/reload重新載入配置檔案

-p prefix設定路徑,預設是/usr/local/nginx

-c 載入非預設位置配置檔案

-v顯示nginx版本

-V檢視編譯引數,新增模組,刪減功能

}

nginx回車啟動nginx

netstat-luantp | grep 80檢視是否起來了

開啟瀏覽器輸入網址為ip即可訪問nginx的網頁

vim/usr/local/nginx/conf/nginx.conf

nginx -t檢測配置檔案是否有語法錯誤

nginx -tq若沒有語法錯誤就不提示

nginx -v

nginx -V版本和編譯的引數

nginx -sstop關閉服務

nginx開啟服務

vim/usr/local/nginx/conf/nginx.conf這個裡面都是以分號結尾

user nginx;使用nginx使用者登入

worker_processes1;可以設定為auto,根據cpu數量來設定

錯誤日誌目錄是在/usr/local/nginx/log裡面

pid

全域性塊events{worker_connections1024;}:每個程序的最大連線數

events塊:

http塊:可以巢狀多個server,配置代理,快取,日誌定義等,是否使用sendfile傳輸檔案,連線超時時間,單連線請求數等{

includemime.types; #副檔名與檔案型別對映表
default_type application/octet-stream;

#log_format main '$remote_addr - $remote_user [$time_local]"$request" ' #定義訪問日誌格式

# '$status $body_bytes_sent "$http_referer"'
# '"$http_user_agent" "$http_x_forwarded_for"';左邊是用curl訪問的就記錄curl,用http訪問的就記錄http,右邊是如果沒有就不記錄

log_formattest '$remote_addr : $time_local : $http_user_agent';

聲名自定義日誌還要去server裡面呼叫access_loglogs/test.access.log test;

nginx -t

nginx -s reload重新載入配置檔案

tail -0f /usr/local/nginx/logs/test.access.log

換另一臺虛擬機器來訪問本機使用curl和火狐來訪問檢視監聽狀態

老師筆記:自己定義日誌:在日誌部分寫入
http全域性塊裡面寫
log_format test '$remote_addr : $time_local : $http_user_agent';
server塊裡面寫
access_log logs/access.log test; #cust:自定義的日誌名稱
測試自定義日誌: # curl 192.168.1.254
#cat /usr/local/nginx/logs/test.access.log
訪問的Ip:訪問的時間:訪問的客戶端的型別
tail -0f /usr/local/nginx/logs/test.access.log

$remote_addr 記錄客戶端的ip
$time_local 記錄訪問時間(看nginx的時間)
$http_user_agent 記錄客戶端瀏覽器的資訊
$status 記錄請求狀態(狀態碼資訊)
$remote_user 記錄遠端客戶端的名字
$request 記錄請求的URL或HTTP協議
$body_bytes_sent 記錄傳送給客戶端的檔案的內容的大小
$http_referer 記錄從哪個頁面連結訪問過來的
$http_x_forwarded_for 記錄客戶端的ip


#access_log logs/access.log main;呼叫剛才起的名字main

sendfile on; #開啟高效檔案傳輸模式

server塊(配置系你主機的引數,一個http中可以有多個server塊){

listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;

}

}

}

location塊  /訪問的位置(客戶端在瀏覽器輸入的是什麼)是根,以.php結尾(location ~ \.php$) {

root html;定義根目錄,/usr/local/nginx下面的

index index.htmlindex.htm;

error_page404 /50x.html;如果沒有找到就顯示這頁面

location =/50x.html{

root html;

}

}

apache怎樣定義日誌格式的?

yum install-y httpd

vim /etc/

LogFormat“%h %l ....”  combined下面有呼叫這個combined名字

apache在瀏覽器裡面輸入ip會預設進入第一個虛擬主機

虛擬主機:
基於域名的虛擬主機:

start:

vim /usr/local/nginx/conf/nginx.conf

在sever下面新增(試試不修改預設的server直接在下面新增會咋樣)

server {

    listen 80;

    server_name www.163.com;

    location / {這個裡面的東西等下要去建立(資料夾和網頁)

       root /163;

       index index.html index.htm;第一個單詞是索引頁

    }

}

server {

    listen 80;

    server_name www.baidu.com;

    location / {這個裡面的東西等下要去建立(資料夾和網頁)

       root html/baidu;

       index index.html index.htm;索引頁

    }

}

server {

    listen 80;

    server_name www.google.com;

    location / {這個裡面的東西等下要去建立(資料夾和網頁)

       root html/google;

       index index.html index.htm;索引頁

    }

}

mkdir /163

mkdir/usr/local/nginx/html/google

mkdir /usr/local/nginx/html/baidu

echo 163> /163/index.html

echo baidu> /usr/local/nginx/html/baidu/index.html

echo google> /usr/local/nginx/html/google/index.html

nginx -t

nginx -s stop;nginx重新載入配置檔案重啟服務

換一臺虛擬機器:

vim/etc/hosts

新增解析:

nginx主機ip    www.baidu.com

nginx主機ip    www.google.com

nginx主機ip    www.163.com(試一下將左邊改為www.1636.com來驗證一下server_name的作用,發現沒有該域名會訪問預設的網頁,預設的網頁是第一個,如果在listen埠號後面加一個default就變成預設的網頁了)

然後訪問curl www.baidu.com

curlwww.google.com

curl www.163.com

給伺服器發請求的時候要把我們這些包發到裡面去

header標頭檔案,在http報文裡面

tcpdump抓包工具:

start:

yum install-y tcpdump安裝

tcpdump -{

-n不要把主機地址轉換成名字,這樣看的資訊更清晰

-vv顯示詳細資訊

-i網絡卡

}

tcpdump -n-vv -i ens33 port 80 -w  tcpdumpname.dump名字(當前目錄下生成該檔案)

換一臺虛擬機器訪問本機

curlwww.163.com

curl www.baidu.com

換回nginx機

停止

ll tcpdump看不了,需要安裝下面的軟體才能開啟,還需要圖形化介面,沒有圖形化介面的話就將生成的檔案複製到有圖形化介面的虛擬機器裡面去看

yum install-y wireshark-gnome

然後在剛才當前目錄下雙擊開啟剛才生成的tcpdumpname

基於IP的虛擬主機:
ifconfig ens33:0 192.168.56.60
ifconfig ens33:1 192.168.56.61這兩個只是臨時生效的,ifdown ens33;ifupens33之後就沒了
共用一張網絡卡
server {
listen 192.168.56.60:80;使用了套接字
server_name www.baidu.com;
location / {
root html/baidu;
index index.html;
}
server {
listen 192.168.56.61:80;
server_name www.google.com;
location /{
root html/google;
index index.html;
}
}
重啟服務發現netstat -luantp | grep nginx沒有繫結套接字,nginx -s reload無效,就要先停止,再開啟:

nginx -s stop;nginx

切換客戶端的虛擬機器:curl 192.168.56.60

curl 192.168.56.61

基於埠的虛擬主機:
server {
listen 81;
server_name www.baidu.com;
location / {
root html/baidu;
index index.html;
}
}
server {
listen 82;
server_name www.google.com;
location /{
root html/google;
index index.html;
}
}
重啟服務nginx -s stop;nginx
瀏覽器裡面輸入域名:埠號來訪問:curl 192.168.10.11:81或curl www.baidu.com:81

nginx訪問控制:
使用者認證:(通過賬戶和密碼)
location / {
root html;
index index.html index.htm;
auth_basic "提示:hehe"; 提示資訊(auth是鑑別的意思)
auth_basic_user_file "/usr/local/nginx/passwd.db"; 從哪個位置讀取檔案
}
yum install -y httpd-tools
[[email protected] html]# htpasswd -c /usr/local/nginx/passwd.db tom生成使用者名稱和密碼,注意-c第一次用一次就行了,以後不要用-c,不然會覆蓋之前的
New password: 1
Re-type new password: 1
Adding password for user tom
使用客戶端firefox去訪問才行,curl無效,注意重新整理頁面清快取

訪問控制:(通過允許和拒絕)
針對 ip 、網段
location / {
root html;
index index.html index.htm;
allow 192.168.10.0/24;匹配到了就不執行下面的了
deny all;拒絕除了10.0以外的ip
}

使用客戶端firefox去訪問才行,curl無效,注意重新整理頁面清快取
nginx匹配到了就不執行下面的了,而httpd裡面的訪問控制orderallow,deny是匹配到了還會執行下面的,它是看order的順序

在http下面設定:

server_tokensoff;訪問失敗時不顯示nginx的版本號

狀態訪問統計:
server中新增如下行
location ~ /status {這個~是匹配的意思
stub_status on;開啟狀態
access_log off;關掉日誌,不要記錄到日誌裡面
}
在域名後面加上一個/status訪問
瀏覽器:http://192.168.10.11/status重新整理可得到如下變化結果

Active connections: 557
server accepts handled requests
36573075 36573075 43806112
Reading: 3 Writing: 16 Waiting: 538

Active connections
活動連線數
server accepts handled requests
nginx總共處理了36573075個連線, 成功建立36573075次握手 (相等表示中間沒有失敗的), 總共處理了43806112個請求 (平均每次握手處理了1.2個數據請求).

Reading
nginx讀取到客戶端的Header資訊數.
Writing
nginx返回給客戶端的Header資訊數.
Waiting
開啟keep-alive的情況下,這個值等於active - (reading +writing),意思就是Nginx說已經處理完正在等候下一次請求指令的駐留連線.