WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化-創(chuàng)新互聯(lián)

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

成都創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的南川網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

概述

Nginx ("engine x") 是一個高性能的HTTP和反向代理服務(wù)器,也是一個IMAP/POP3/SMTP服務(wù)器。Nginx是由Igor Sysoev為俄羅斯訪問量第二的Rambler.ru站點開發(fā)的,第一個公開版本0.1.0發(fā)布于2004年10月4日。其將源代碼以類BSD許可證的形式發(fā)布,因它的穩(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名。2011年6月1日,nginx 1.0.4發(fā)布。

Nginx是一款輕量級的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,并在一個BSD-like協(xié)議下發(fā)行。由俄羅斯的程序設(shè)計師Igor Sysoev所開發(fā),供俄國大型的入口網(wǎng)站及搜索引擎Rambler使用。其特點是占有內(nèi)存少,并發(fā)能力強,事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好,中國大陸使用nginx網(wǎng)站用戶有:百度、京東、新浪、網(wǎng)易、騰訊、淘寶等

Nginx的重要特性及應(yīng)用場合

1)Nginx重要特性

(1)可針對靜態(tài)資源告訴高并發(fā)訪問及緩存

(2)可使用反向代理加速,并且可進行數(shù)據(jù)緩存

(3)具有簡單的負(fù)載均衡,節(jié)點講課檢查和容錯功能

(4)支持原創(chuàng)fastcgi服務(wù)的緩存加速

(5)支持基于域名、端口和IP的多虛擬主機站點等

2)Nginx主要企業(yè)功能應(yīng)用

(1)作為web服務(wù)軟件

Nginx是個支持高性能、高并發(fā)的web軟件,作為web服務(wù)器,Nginx能夠支持更多的并發(fā)連接訪問,并且占用的資源很少,效率更高。

(2)作為反向代理或者負(fù)載均衡器

在反向代理或負(fù)載均衡服務(wù)方面,Nginx可以作為web服務(wù),php、java的辦法動態(tài)服務(wù)及memcache緩存的代理服務(wù)。

(3)前端業(yè)務(wù)數(shù)據(jù)緩存服務(wù)

Nginx可以通過自身的proxy_cache模塊實現(xiàn)類似squid等專業(yè)緩存軟件功能

Nginx的WEB服務(wù)

1)Nginx作為web服務(wù)器的主要應(yīng)用場景包括:

(1)使用N信息運行HTML、JS、CSS等靜態(tài)數(shù)據(jù)

(2)Nginx結(jié)合fastcgi運行PHP等動態(tài)程序(使用fastcgi_pass方式)

(3)Nginx 結(jié)合Tomcat、Resin等支持java動態(tài)程序(使用proxy_pass方式)

2)Nginx總體性能比Apache強

Nginx使用最新的epoll和kqueue異步網(wǎng)絡(luò)IO模型,而Apache使用的是傳統(tǒng)的select模型。而目前Linux下能夠承受的高并發(fā)訪問squid、memcache軟件采用都是epoll模型。戴亮連接讀寫時,Apache采用的select網(wǎng)絡(luò)IO模型比較低。

3)Apache select和Nginx epoll的技術(shù)對比

指標(biāo)

select

epoll

性能

隨著連接數(shù)的增加心梗幾句下滑

隨著連接數(shù)的增加,性能基本上沒有改變

連接數(shù)

連接數(shù)有限制,處理的大連接數(shù)不超過1024

連接數(shù)無限制

內(nèi)在處理機制

線性輪詢

回調(diào)callback

開發(fā)復(fù)雜性

Nginx技術(shù)的深入剖析

Nginx軟件之所以強大,是因為他具有眾多的通能模塊。

1、Nginx模塊主要有4種角色:
   (1) core(核心模塊):構(gòu)建nginx基礎(chǔ)服務(wù)、管理其他模塊。
   (2) handlers(處理模塊): 用于處理HTTP請求,然后產(chǎn)生輸出。
   (3) filters(過濾模塊): 過濾handler產(chǎn)生的輸出。
   (4) load-balancers(負(fù)載均衡模塊):當(dāng)有多于一臺的后端備選服務(wù)器時,選擇一臺轉(zhuǎn)發(fā)HTTP請求。

當(dāng)Nginx發(fā)送文件或者轉(zhuǎn)發(fā)請求到其他服務(wù)器,由handlers(處理模塊)或load-balancers(負(fù)載均衡模塊)提供服務(wù);

當(dāng)需要Nginx把輸出壓縮或者在服務(wù)端加一些東西,由filters(過濾模塊)提供服務(wù)。

2、Nginx的幾個常用模塊

1)Nginx的核心功能模塊Nginx core module

Nginx可信功能模塊負(fù)載全局應(yīng)用,主要對飲主配置文件的main區(qū)塊和events區(qū)塊區(qū)域,這里有很多Nginx必須的全局參數(shù)配置。

2)標(biāo)準(zhǔn)的http功能模塊集合

這些標(biāo)準(zhǔn)的http功能模塊,雖然不是Nginx軟件所必須的,但是都是很常用的,因此絕大部分默認(rèn)都會安裝到Nginx軟件中

在生產(chǎn)環(huán)節(jié)中,配置,調(diào)優(yōu)及優(yōu)化Nginx軟件,主要就是根據(jù)這些模塊的功能修改相應(yīng)的參數(shù)來實現(xiàn)的。

Nginx http功能模塊

模塊說明

ngx_http_core_moudle

包括一些核心的http參數(shù)配置,對應(yīng)的nginx的配置為http區(qū)塊

ngx_http_access_moudle

訪問控制模塊,用來控制網(wǎng)站用戶對Nginx的訪問

ngx_http_gzip_moudle

壓縮模塊,對Nginx返回的數(shù)據(jù)壓縮,屬于心梗優(yōu)化模塊

ngx_http_fastcgi_moudle

FastCGI模塊,和動態(tài)相關(guān)的模塊,如PHP/JAVA

ngx_http_proxy_moudle

Proxy代理模塊

ngx_http_upstream_moudle

負(fù)載均衡模塊,可以實現(xiàn)網(wǎng)站的負(fù)載均衡功能,和節(jié)點的健康檢查

ngx_http_rewrite_moudle

URL地址重寫模塊

ngx_http_limit_conn_moudle

限制用戶并發(fā)連接數(shù)及請求書模塊

ngx_http_limit_req_moudle

根據(jù)定義的key限制Nginx請求過程速率

ngx_http_log_moudle

訪問日志模塊,以指定格式記錄Nginx客戶訪問日志等信息

ngx_http_auth_basic_moudle

Web認(rèn)證模塊,設(shè)置web用戶通過賬戶,密碼訪問Nginx

ngx_http_ssl_moudle

Ssk模塊,用于加密的httpd連接,如https

ngx_http_stub_status_moudle

記錄Nginx基本訪問狀態(tài)信息等模塊

一、實驗?zāi)繕?biāo)

1、實戰(zhàn):Nginx基于域名、端口和IP的多個虛擬主機

2、實戰(zhàn):Nginx虛擬主機的別名功能

3、實戰(zhàn):Nginx基于多個虛擬地址和端口的優(yōu)化

4、實戰(zhàn):Nginx的狀態(tài)信息功能實戰(zhàn)

5、實戰(zhàn):Nginx的訪問認(rèn)證

6、實戰(zhàn):Nginx訪問日志及輪詢切割

二、實驗環(huán)境

系統(tǒng)環(huán)境

主機名

IP地址

軟件

Rhel6.5

yu63

192.168.1.63

Nginx

三、實驗步驟

1、安裝nginx

[root@yu63~]#service httpd stop

[root@yu63~]#service iptables stop

[root@yu63~]#yum install pcre pcre-devel openssl openssl-devel

[root@yu63~]#mkdir /opt/yu && cd /opt/yu/

[root@yu63 yu]#wget -qhttp://nginx.org/download/nginx-1.13.0.tar.gz

[root@yu63 yu]#tar -zxvf nginx-1.13.0.tar.gz

[root@yu63 yu]#cd nginx-1.13.0

[root@yu63 nginx-1.13.0]#useradd nginx -s /sbin/nologin

[root@yu63 nginx-1.13.0]#./configure --user=nginx --group=nginx --prefix=/usr/local/nginx--with-http_stub_status_module --with-http_ssl_module

[root@yu63nginx-1.13.0]#make -j 4

[root@yu63nginx-1.13.0]#make install

[root@yu63 nginx-1.13.0]# cd /usr/local/nginx/

[root@yu63 nginx]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

[root@yu63 nginx]#/usr/local/nginx/sbin/nginx -t

[root@yu63 nginx]#/usr/local/nginx/sbin/nginx

Nginx虛擬主機配置實戰(zhàn)

虛擬主機的概念

所謂的虛擬主機,在web服務(wù)里就是獨立的網(wǎng)站站點,這個站點對應(yīng)duld域名、IP、端口。具有獨立的程序及資源目錄,可以獨立的對外提供給服務(wù)供用戶訪問。

對于Apache來說,虛擬主機的標(biāo)簽通常被包含在<VirtualHost>~~~</VirtualHost>中,而Nginx軟件則是使用server{}標(biāo)簽來表示一個虛擬主機,一個web服務(wù)里可以有多個虛擬主機標(biāo)簽,即可以同時支持多個虛擬主機站點。

虛擬主機的類型

1)基于域名的虛擬主機

2)基于端口的虛擬主機

3)基于IP地址的虛擬主機

實戰(zhàn)1:基于域名和端口的多個虛擬主機

[root@yu63 nginx]# cd conf/

[root@yu63 conf]#egrep -v '#|^$' nginx.conf.default > nginx.conf

[root@yu63nginx]# rm -rf html/index.html

[root@yu63nginx]# mkdir -p html/mobanche

[root@yu63nginx]# mkdir -p html/mobanche1

[root@yu63nginx]# mkdir -p html/mobanche2

[root@yu63nginx]#echo 'mobanche'> html/index.html

[root@yu63nginx]# echo 'mobanche----1' > html/mobanche1/index.html

[root@yu63nginx]# echo 'mobanche----2' > html/mobanche2/index.html

[root@yu63nginx]#vim conf/nginx.conf

[root@yu63nginx]# 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.mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

        }

    server {

        listen       81;

        server_name  bbs.mobanche1.com;

        location / {

            root   html/mobanche1;

            index  index.html index.htm;

        }

        }

    server {

        listen       82;

        server_name  blog.mobanche2.com;

        location / {

            root   html/mobanche2;

            index  index.html index.htm;

        }

    }

}

[root@yu63nginx]# /usr/local/nginx/sbin/nginx -t

[root@yu63nginx]#/usr/local/nginx/sbin/nginx -s reload

[root@yu63nginx]# cat /etc/hosts

192.168.1.63 www.mobanche.com bbs.mobanche1.com blog.mobanche2.com

Windos下面的hosts文件

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

實戰(zhàn)2:Nginx虛擬主機的別名功能

[root@yu63nginx]# vim conf/nginx.conf

[root@yu63nginx]# 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.mobanche.com mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

        }

    server {

        listen       81;

        server_name  bbs.mobanche1.com mobanche1.com;

        location / {

            root   html/mobanche1;

            index  index.html index.htm;

        }

        }

    server {

        listen       82;

        server_name  blog.mobanche2.com mobanche2.com;

        location / {

            root   html/mobanche2;

            index  index.html index.htm;

        }

    }

}

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

實戰(zhàn)3:Nginx基于多個域名、ip和端口的優(yōu)化

[root@yu63 conf]#mkdir extra

      173  sed -n ' 11,18p ' nginx.conf > extra/mobanche.conf

  176  sed -n ' 19,26p ' nginx.conf > extra/mobanche1.conf

  177  sed -n ' 27,34p ' nginx.conf > extra/mobanche2.conf

[root@yu63nginx]#vim nginx.conf #刪除11-34行

 sed -i '10 i include extra/mobanche.conf;\ninclude extra/mobanche1.conf;\ninclude extra/mobanche2.conf;' nginx.conf

[root@yu63 nginx]# cat conf/extra/mobanche.conf

   server {

        listen       80;

        server_name  www.mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

        }

[root@yu63nginx]#/usr/local/nginx/sbin/nginx -t

[root@yu63nginx]#/usr/local/nginx/sbin/nginx

實戰(zhàn)4:Nginx的狀態(tài)信息功能實戰(zhàn)

Nginx軟件功能模塊中有一個ngx_http_stub_status_moudle模塊,這個模塊的主要功能就是記錄Nginx的節(jié)本訪問狀態(tài)信息,讓使用者了解Nginx的工作狀態(tài),例如連接數(shù)等信息。

[root@yu63 nginx]#cd extra

[root@yu63 extra]#

cat >> status.conf << EOFEOF

>># status

>>server {

>>        listen       80;

>>        server_name  status.mobanche.com;

>>        location / {

>>stub_status on;

>>access_log off;

>>        }

>>        }

[root@yu63 extra]#sed -i ' 13 i include extra/status.conf; ' ../nginx.conf

[root@yu63 extra]# ../../sbin/nginx -t

[root@yu63 extra]#. ../../sbin/nginx -s reload

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

實戰(zhàn)5:Nginx的訪問認(rèn)證

[root@yu63extra]# pwd

/usr/local/nginx/conf/extra

[root@yu63extra]#vim mobanche.conf

   server {

        listen       80;

        server_name  www.mobanche.com mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        auth_basic "welcome please enter user and passwd";

        auth_basic_user_file /usr/local/nginx/conf/htpasswd;

        }

        }

[root@yu63conf]# htpasswd -bc /usr/local/nginx/conf/htpasswd yu 123456

Adding password for user yu

[root@yu63conf]# chmod 400 htpasswd

[root@yu63conf]# chown nginx htpasswd

[root@yu63conf]# cat htpasswd

yu:c5XqWgCfsgKoo

[root@yu63extra]# ../../sbin/nginx -t

[root@yu63extra]# ../../sbin/nginx -s reload

WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化

實戰(zhàn)6:訪問日志及輪詢切割

Nginx軟件會把每個用戶的訪問網(wǎng)站的日志信息記錄到指定的入職文件中,供網(wǎng)站提供者分析用戶的瀏覽行為。

1、控制日志的參數(shù)

參數(shù)

說明

log_format

用來定義記錄日志的格式(可以定義多種日志格式)

access_log

用來指定日志文件的路徑及使用何種日志格式記錄日志

2、Nginx的訪問日志主要默認(rèn)參數(shù)

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

         '$status $body_bytes_sent "$http_referer" '

         '"$http_user_agent" "$http_x_forwarded_for"';

3、日志變量說明

日志變量

說明

$remote_addr

用以記錄客戶端的ip地址

$remote_user

用來記錄客戶端用戶名稱

$time_local

用來記錄訪問時間與時區(qū)

$request

用來記錄請求的url與http協(xié)議

$status

用來記錄請求狀態(tài);成功是200

$body_bytes_sent

服務(wù)器發(fā)送給客戶端響應(yīng)的body字節(jié)數(shù)

$http_referer

用來記錄從那個頁面鏈接訪問過來的

$http_user_agent

記錄客戶端瀏覽器的相關(guān)信息

$http_x_forwarded_for

當(dāng)前端有代理服務(wù)器時沒設(shè)置web節(jié)點記錄客戶端地址的配置,此參數(shù)生效的前提是代理服務(wù)器上也進行了x_forwarded_for設(shè)置

[root@yu63nginx]# cat conf/nginx.conf

[root@yu63 nginx]# cat conf/nginx.conf

worker_processes  1;

error_log  logs/error.log;

events {

    worker_connections  1024;

}

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"';

    sendfile        on;

    keepalive_timeout  65;

include extra/mobanche.conf;

include extra/mobanche1.conf;

include extra/mobanche2.conf;

    }

[root@yu63 nginx]# cat conf/extra/mobanche.conf

   server {

        listen       80;

        server_name  www.mobanche.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

 access_log  logs/access_mobanche.log  main;

        }

[root@yu63 nginx]# cat conf/extra/mobanche1.conf

   server {

        listen       81;

        server_name  www.mobanche1.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

access_log  logs/access_mobanche1.log  main;

        }

[root@yu63 nginx]# cat conf/extra/mobanche2.conf

   server {

        listen       82;

        server_name  www.mobanche2.com;

        location / {

            root   html/mobanche;

            index  index.html index.htm;

        }

access_log  logs/access_mobanche2.log  main;

        }

[root@yu63 nginx]# tail -1 logs/access_mobanche.log

192.168.1.63 - - [15/May/2017:22:25:42 +0800] "GET / HTTP/1.1" 200 9 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.3.0 zlib/1.2.3 libidn/1.18 libssh3/1.4.2" "-"

[root@yu63extra]# ../../sbin/nginx -t

[root@yu63extra]# ../../sbin/nginx -s reload

[root@yu63 nginx]# tail -3 logs/access_mobanche.log

192.168.1.105- - [15/May/2017:22:26:51 +0800] "GET /favicon.ico HTTP/1.1" 404 571 "http://www.mobanche.com/" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400" "-"

192.168.1.105 - - [15/May/2017:22:26:51 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400" "-"

192.168.1.105 - - [15/May/2017:22:26:51 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.104 Safari/537.36 Core/1.53.2669.400 QQBrowser/9.6.10990.400" "-"

注釋:在沒有開啟訪問實戰(zhàn)時,查看日志中的內(nèi)容,只是本機的IP地址,而配置之后,就可以看到客戶端的主機IP。

3、Nginx訪問日志輪詢切割腳本

[root@yu63 extra]# cat cut_nginx_log.sh

#!/bin/sh

Dateformat=`date +%Y%m%d`

Basedir="/usr/local/nginx"

Nginxlogdir="$Basedir/logs"

Logname="access_www"

[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1

[ -f ${Logname}.log ]||exit 1

/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log

/bin/mv access_mobanche1.log ${Dateformat}_access_mobanche1.log

/bin/mv access_mobanche2.log ${Dateformat}_access_mobanche2.log

$Basedir/sbin/nginx -s reload

[root@yu63 extra]# crontab -l  #crontab -e編寫

45 22 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

網(wǎng)頁題目:WEB服務(wù)器-Nginx之虛擬主機、日志、認(rèn)證及優(yōu)化-創(chuàng)新互聯(lián)
鏈接URL:http://bm7419.com/article4/ddhiie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作靜態(tài)網(wǎng)站、商城網(wǎng)站微信小程序、云服務(wù)器網(wǎng)頁設(shè)計公司

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

手機網(wǎng)站建設(shè)