目錄
Nginx概述
Linux的i/o
編譯安裝
nginx newshttp://nginx.org/
組態檔
全域配置
I/O 事件配置
http配置
日志格式
訪問狀態統計配置
修改nginx.conf 組態檔,指定訪問位置并添加 stub_status配置
?
基于授權密碼的訪問控制
基于域名的nginx虛擬主機
基于IP地址
基于埠
總結
Nginx概述
Nginx是一個高性能的HTTP和反向代理服務器,
是一款輕量級的web服務器/反向代理服務器/電子郵件(IMAP/POP3)代理服務器
單臺物理服務器可支持30 000~50 000個并發請求,
Linux的i/o
I/O在計算機中指Input/Output,lOPS (Input/Output Per Second)即每秒的輸入輸出量(或讀寫次數),是衡量磁盤性能的主要指標之一,IOPS是指單位時間內系統能處理的I/O請求數量,一般以每秒處理的IO請求數量為單位,I/O請求通常為讀或寫資料操作請求,
一次完整的I/O是用戶空間的行程資料與內核空間的內核資料的報文的完整交換,但是由于內核空間與用戶空間是嚴格隔離的,所以其資料交換程序中不能由用戶空間的行程直接呼叫內核空間的記憶體資料,而是需要經歷一次從內核空間中的記憶體資料copy到用戶空間的行程記憶體當中,所以簡單說I/O就是把資料從內核空間中的記憶體資料復制到用戶空間中行程的記憶體當中,
-
磁盤I/O:buff/cache 的區別
-
網路I/O:一切皆檔案,本質為對socket檔案的讀寫
buff(讀)cache(寫)
網路I/O

獲取請求資料,客戶端與服務器建立連接發出請求,服務器接受請求(1-3)
構建回應,當服務器接收完請求,并在用戶空間處理客戶端的請求,直到構建回應完成(4)
回傳資料,服務器將已構建好的回應再通過內核空間的網路I/0發還給客戶端(5-7)
同步/異步:關注的是訊息通信機制,即呼叫者在等待一件事情的處理結果時,被呼叫者是否提供完成狀態的通知,
-
同步: synchronous,被呼叫者并不提供事件的處理結果相關的通知訊息,需要呼叫者主動詢問事情是否處理完成
-
異步: asynchronous,被呼叫者通過狀態、通知或回呼機制主動通知呼叫者被呼叫者的運行狀態
阻塞/非阻塞:關注呼叫者在等待結果回傳之前所處的狀態
-
阻塞: blocking,指IO操作需要徹底完成后才回傳到用戶空間,呼叫結果回傳之前,呼叫者被掛起,干不了別的事情,
-
非阻塞: nonblocking,指IO操作被呼叫后立即回傳給用戶一個狀態值,而無需等到IO操作徹底完成,在最終的呼叫結果回傳之前,呼叫者不會被掛起,可以去做別的事情,
阻塞型

非阻塞型同步

多路復用I/O模型 ( epoll poll select)
poll是Linu中的字符設備驅動中的一個函式,Linux 2.5.44版本后,poll被epoll取代,和select實作的功能差不多,poll的作用是把當前的檔案指標掛到等待佇列,

信號驅動型

異步非阻塞I/O模型

編譯安裝
nginx官網地址,我這邊下載的是nginx -1.12.0.tar.gz包
nginx news
http://nginx.org/
解壓

安裝關系依賴包
[root@localhost opt]#yum install pcre-devel zlib-devel gcc-c++.x86_64 make.x86_64 -y

新建用戶 和組便于管理(nginx 服務程式默認 以 nobody 身份運行,建議為其創建專門的用戶賬戶,以便更準確的控制訪問權限)
[root@localhost opt]#useradd -M -s /sbin/nologin nginx
[root@localhost opt]#id nginx
uid=1001(nginx) gid=1001(nginx) 組=1001(nginx)
編譯安裝Nginx
cd到nginx包下面
[root@localhost nginx-1.12.0]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
--prefix=/usr/local/nginx \
#安裝路徑
--user=nginx \
#指定用戶名
--group=nginx \
#指定用戶組
--with-http_stub_status_module
#啟用此模塊支持狀態統計

[root@localhost nginx-1.12.0]#make && make install -j6

做一個軟鏈接
[root@localhost nginx-1.12.0]#ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
檢查語法
[root@localhost nginx-1.12.0]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
啟動nginx
[root@localhost nginx-1.12.0]#nginx
[root@localhost nginx-1.12.0]#lsof -i :80
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
nginx 63588 root 6u IPv4 149782 0t0 TCP *:http (LISTEN)
nginx 63589 nginx 6u IPv4 149782 0t0 TCP *:http (LISTEN)
停止nginx
kill -3 <PID號>
kill -s QUIT <PID號>
killall -3 nginx
killall -s QUIT nginx#多載
kill -1 <PID號>kill -s HUP <PID號>
killall -1 nginx
killall -s HUP nginx
| 信號編號 | 信號名 | 含義 |
|---|---|---|
| 0 | EXIT | 程式退出時收到該資訊, |
| 1 | HUP | 掛掉電話線或終端連接的掛起信號,這個信號也會造成某些行程在沒有終止的情況下重新初始化, |
| 2 | INT | 表示結束行程,但并不是強制性的,常用的 "Ctrl+C" 組合鍵發出就是一個 kill -2 的信號, |
| 3 | QUIT | 退出, |
| 9 | KILL | 殺死行程,即強制結束行程, |
| 11 | SEGV | 段錯誤, |
| 15 | TERM | 正常結束行程,是 kill 命令的默認信號, |
添加nginx服務
[root@localhost nginx-1.12.0]#vim /etc/init
#!/bin/bash
#chkconfig: 35 99 20
#description:Nginx Service Control Script
cmd="/usr/local/nginx/sbin/nginx"
pid="/usr/local/nginx/logs/nginx.pid"
case $1 in
start)
$cmd
;;
stop)
kill -3 `cat $pid`
;;
reload)
kill -1 `cat $pid`
;;
restart)
$0 stop
$0 start
;;
*)
echo "plaese input start,stop,reload,restart"
exit 1
esac
exit 0

[root@localhost system]#vim nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
##配置引數解釋##
[Unit]
Description=nginx ####描述
After=network.target ####描述服務類別
[Service]
Type=forking ###后臺運行形式
PIDFile=/usr/local/nginx/logs/nginx.pid ###PID檔案位置
ExecStart=/usr/local/nginx/sbin/nginx ###啟動服務
ExecReload=/usr/bin/kill -s HUP $MAINPID ###根據PID多載配置
ExecStop=/usr/bin/kill -s QUIT $MAINPID ###根據PID終止行程
PrivateTmp=true
[Install]
WantedBy=multi-user.target
多載服務
記得要把之前行程殺掉然后再開啟
[root@localhost system]#systemctl daemon-reload
[root@localhost system]#systemctl start nginx

組態檔
由各種配置陳述句組成,不使用特定的界定標記,全域配置部分包括 Nginx 服務的運行 用戶、作業行程數、錯誤日志、PID 存放位置等基本設定
[root@localhost system]#cd /usr/local/nginx/conf/
[root@localhost conf]#ls
fastcgi.conf koi-utf nginx.conf uwsgi_params
fastcgi.conf.default koi-win nginx.conf.default uwsgi_params.default
fastcgi_params mime.types scgi_params win-utf
fastcgi_params.default mime.types.default scgi_params.default
全域配置
修改組態檔最好,備份一份
[root@localhost conf]#cp nginx.conf nginx.conf.bak
[root@localhost conf]#vim nginx.conf
#user nobody; #運行用戶
worker_processes 1; #作業行程數,可配置成服務器內核數*2,如果網站訪問量不大,一般設為1就夠用了#error_log logs/error.log; #錯誤日志檔案的位置
#pid logs/nginx.pid; #PID檔案的位置
I/O 事件配置
events {
use epoll; #使用 epoll 模型以提高性能,2.6 以上版本建議使用
worker_connections 4096; #每個行程處理4096個連接
}
epoll(socket描述符)是Linux內核]為處理大批量檔案描述符而作了改進的poll,是Linux下多路復用IO介面select/poll的增強版本,它能顯著提高程式在大量并發連接中只有少量活躍的情況下的系統CPU利用率
若作業行程數為 8,每個行程處理 4 096 個連接,則允許 Nginx 正常提供服務的連接數 已超過 3 萬個(4 096×8=32 768),當然具體還要看服務器硬體、網路帶寬等物理條件的性 能表現,
http配置
使用“http { }”界定標記,包括訪問日志、HTTP 埠、網頁目錄、默認字符集、連接保 持,以及后面要講到的虛擬 Web 主機、PHP 決議等一系列設定,其中大部分配置陳述句都包 含在子界定標記“server { }”內
http {
include mime.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"'; #日志格式設定
#access_log logs/access.log main;
#訪問日志位置
sendfile on;
#支持檔案發送(下載)
tcp_nopush on;
#此選項允許或禁止使用socketde TCP_CORK的選項(發送資料包前先快取資料),此選項僅在使用 sendfile時使用
keepalive_timeout 0;
keepalive_timeout 65;
#連接保持超時時間,單位是秒
gzip on;
#gzip模塊設定,設定是否開啟gzip壓縮輸出
###web服務的監聽設定
server {
listen 80;
#監聽地址及埠
server_name localhost;
#站點域名,可以有多個,用空格隔開
#charset koi8-r;
#網頁的默認字符集
#access_log logs/host.access.log main;location / {
root html;
#網站根目錄的位置/usr/local/nginx/html
index index.html index.htm;
#默認首頁檔案名
}#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
#內部錯誤反饋頁面
location = /50x.html {
#錯誤頁面設定
root html;
}
日志格式
-
與http_x_forwarded_for用以記錄客戶端的ip地址;
-
$remote_user:用來記錄客戶端用戶名稱;
-
$time_local:用來記錄訪問時間與時區;
-
$request:用來記錄請求的url與http協議;
-
$status:用來記錄請求狀態:成功是200,300,400,500
-
$body bytes_sent :記錄發送給客戶端檔案主體內容大小;
-
$http_referer:用來記錄從哪個頁面鏈接訪問過來的;
-
$http_user_agent:記錄客戶瀏覽器的相關資訊;
通常web服務器放在反向代理的后面,這樣就不能獲取到客戶的IP地址了,通過$remote add拿到的IP地址是反向代理服務器的IP地址,反向代理服務器在轉發請求的http頭資訊中,可以增加x forwarded for資訊,用以記錄原有客戶端的IP地址和原來客戶端的請求的服務器地址,
location常見配置指令,root、alias、 proxy _pass
例如:
[root@localhost conf]#cd /usr/local/nginx/html
[root@localhost html]#ls
50x.html index.html
[root@localhost html]#vim demo.html
<h1>this is nginxweb <h1>
編輯組態檔
添加倆個location
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf

重啟服務
[root@localhost html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]#systemctl restart nginx.service

如果 是root 就表示 /test 就代表/usr/local/nginx/html/test/ 目錄下的檔案
是alias 就表示 就是/usr/local/nginx/html/test.html
訪問狀態統計配置
nginx 內置了 HTTP_STUB_STATUS 狀態統計模塊,用來反饋當前的 Web 訪問情況, 配置編譯引數時可添加--with-http_stub_status_module 來啟用此模塊支持,可以使用命令
可以使用命令/usr/local/nginx/sbin/nginx –V 查看已安裝的 Nginx 是否包含 HTTP_STUB_STATUS 模塊,
[root@localhost html]#/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
修改nginx.conf 組態檔,指定訪問位置并添加 stub_status配置
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf

重啟服務
[root@localhost html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]#systemctl restart nginx.service

[root@localhost html]#curl http://192.168.37.101/status
Active connections: 2#當前連接數兩個
server accepts handled requests
2 2 2#表示已經處理的連接資訊,三個數字一次表示已處理的連接數、成功的TCP握手次數、已處理的請求數
Reading: 0 Writing: 1 Waiting: 1
基于授權密碼的訪問控制
和apache也差不多,都是改改組態檔,生成用戶密碼認證檔案
現在這臺機器沒有安裝lamp平臺,所有要安裝一個http工具
[root@localhost nginx]#cd conf/
[root@localhost conf]#yum install httpd-tools.x86_64 -y
[root@localhost conf]#htpasswd -c /usr/local/nginx/passwd.db wangwu
New password:
Re-type new password:
Adding password for user wangwu
加密的密碼已經生成
[root@localhost conf]#cat /usr/local/nginx/passwd.db
wangwu:$apr1$6EolDU2q$5BROZGhkgxOi8GucEv23Y1
[root@localhost conf]#vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.html index.htm;
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
改變/usr/local/nginx/passwd.db檔案的用戶
[root@localhost conf]#chown nginx /usr/local/nginx/passwd.db
[root@localhost conf]#ll /usr/local/nginx/passwd.db
-rw-r--r--. 1 nginx root 45 11月 8 21:23 /usr/local/nginx/passwd.db
修改權限,只讀
[root@localhost conf]#chmod 400 /usr/local/nginx/passwd.db
[root@localhost conf]#ll /usr/local/nginx/passwd.db
-r--------. 1 nginx root 45 11月 8 21:23 /usr/local/nginx/passwd.db
重啟服務i
[root@localhost conf]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]#systemctl restart nginx.service

基于域名的nginx虛擬主機
利用虛擬主機,不用為每個要運行的網站提供一臺單獨的 Nginx 服務器或單獨運行一 組 Nginx 行程,虛擬主機提供了在同一臺服務器,同一組 Nginx 行程上運行多個網站的功 能,跟 Apache 一樣,Nginx 也可以配置多種型別的虛擬主機,分別是基于 IP 的虛擬主機、 基于域名的虛擬主機、基于埠的虛擬主機, 使用 Nginx 搭建虛擬主機服務器時,每個虛擬 Web 站點擁有獨立的“server{}”配置段, 各自監聽的 IP 地址、埠號可以單獨指定,當然網站名稱也是不同的,
[root@localhost var]#mkdir -p www/html/{xbin,MyNginx} [root@localhost www]#cd html/ [root@localhost html]#ls MyNginx xbin [root@localhost html]#echo "this is xbin web" > xbin/index.html [root@localhost html]#echo "this is MyNginx web" > MyNginx/index.html [root@localhost html]#cat xbin/index.html this is xbin web
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf

#gzip on;
server {
listen 80;
server_name www.MyNginx.com;charset utf-8;
access_log logs/MyNginx.access.log;
location / {
server {
listen 80;
server_name www.MyNginx.com;server_name www.MyNginx.com;
charset utf-8;
access_log logs/MyNginx.access.log;
location / {
root /var/www/html/MyNginx;
index index.html index.htm;
}
#error_page 404 /404.html;# redirect server error pages to the static page /50x.html
#
index index.html index.htm;
}
#error_page 404 /404.html;# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
#server_name localhost;server_name www.xbin.com;
charset utf-8;
access_log logs/xbin.access.log;
#access_log logs/host.access.log main;location / {
root /var/www/html/xbin;
index index.html index.htm;
}#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
重啟服務
[root@localhost html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]#systemctl restart nginx.service
然后再window系統中修改dns決議
地址:C:\Windows\System32\drivers\etc\hosts

用記事本打開hosts檔案,修改

最后測驗
成功!!!
基于IP地址
[root@localhost html]#vim /usr/local/nginx/conf/nginx.conf


因為我們所添加的網段系統是無法識別的,所以我們要添加一塊虛擬網卡
[root@localhost html]#ifconfig ens33:0 192.168.37.111
重啟服務
[root@localhost html]#nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost html]#systemctl restart nginx.service
去網頁輸入IP地址

這樣就搞定了
基于埠
基于埠,就在IP地址后面加埠號就可,步驟和基于IP沒啥區別,就不多贅述了

總結
Nginx是一個高性能的HTTP和反向代理服務器,是一款輕量級的web服務器/反向代理服務器/電子郵件(IMAP/POP3)代理服務器,
而Apache是以行程為基礎的結構,行程要比執行緒消耗更多的系統開支,不太適用于多處理器環境,因此,在一個apache Web站點擴容時,通常是增加服務器或擴充群集節點而不是增加處理器,
兩者的優缺點比較:
(1)nginx相對于apache的優點:
-
輕量級,同樣是web服務,比apache占用更少的記憶體及資源
-
抗并發,nginx處理請求是異步非阻塞的,而apache是阻塞型的高并發下,nginx能保持低資源低消耗高性能
-
高度模塊化的設計,撰寫模塊相對簡單
(2)apache相對于nginx的優點:
-
Rewrite比nginx的rewrite強大 ###rewrite的主要功能就是實作統一資源定位符(URL)的跳轉
-
模塊多,基本想到的都可以找到
-
少bug,nginx的bug相對較多
-
超穩定
存在的理由:一般來說,需要性能的web服務,用nginx,若不需要性能只求穩定,就選用apache,
2、作為web服務器:
相比apache,nginx使用更少的資源,支持更多的并發連接,體現更高的效率,
-
Nginx作為負載均衡服務器:nginx既可以在內部直接支持rails和php程式對外進行服務,也可以支持http代理服務器對外進行服務,
-
Nginx采用C進行撰寫,不論是系統資源開銷還是CPU使用效率都比較好,
-
作為郵件代理服務器:最早開發這個產品的目的之一也是作為郵件代理服務器,
3、nginx配置簡潔, apache較復雜
4、最核心的區別在于:
-
apache是同步多行程模型,一個連接對應一個行程,nginx是異步的,多個連接可以對應一個行程,
-
Nginx處理靜態檔案好,耗費記憶體少,只適合靜態和反向,
-
Apache在處理動態有優勢,
-
nginx并發性比較好,CPU占用記憶體低,如果rewrite頻繁,選用apache最佳,
-
總的來說,apache依然是大部分公司的首選,(當然國內還用nginx,只能說國外公司多,人少吧)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/354590.html
標籤:其他
上一篇:Nginx之安裝

