第1章 Nginx 網站服務
1.1 web網站服務介紹:
1.1.1 提供靜態服務的軟件
Apache:這是中小型Web服務的主流,Web服務器中的老大哥。 Nginx:大型網站Web服務的主流,曾經Web服務器中的初生牛犢,現已長大。 Nginx的分支Tengine(http://tengine.taobao.org/)目前也在飛速發展。 Lighttpd:這是一個不溫不火的優秀Web軟件,社區不活躍,靜態解析效率很高。在Nginx流行前,它是大併發靜態業務的首選,國內百度貼吧、豆瓣等衆多網站都有Lighttpd奮鬥的身影。 |
1.1.2 提供動態服務的軟件
PHP(FastCGI):大中小型網站都會使用,動態網頁語言PHP程序的解析容器。它可配合Apache解析動態程序,不過,這裏的PHP不是FastCGI守護進程模式,而是mod_php5.so(module)。也可配合Nginx解析動態程序,此時的PHP常用FastCGI守護進程模式提供服務。(.php) Tomcat:中小企業動態Web服務主流,互聯網Java容器主流(如jsp、do)。 Resin:大型動態Web服務主流,互聯網Java容器主流(如jsp、do)。 IIS(Internet information services):微軟windows下的Web服務軟件(如asp、aspx) |
1.2 Nginx web服務部署及說明
1.2.1 Nginx 軟件特點
1. 支持高併發:能支持幾萬併發連接(特別是靜態小文件業務環境) 2. 資源消耗少:在3萬併發連接下,開啓10個Nginx線程消耗的內存不到200MB 3. 可以做HTTP反向代理及加速緩存、即負載均衡功能,內置對RS節點服務器健康檢查功能, 這相當於專業的Haproxy軟件或LVS的功能。 4. 具備Squid等專業緩存軟件等的緩存功能。 5. 支持異步網絡I/O事件模型epoll(nginx)-select(apache)(Linux 2.6+) |
1.2.2 Nginx 軟件部署(編譯安裝)
#第一個里程:下載nginx程序軟件包並解壓 mkdir /server/tools -p cd /server/tools wget http://nginx.org/download/nginx-1.14.0.tar.gz tar xf nginx-1.14.0.tar.gz
|
#第二個里程:解決軟件依賴問題 yum install openssl-devel pcre-devel -y openssl-devel --- 爲了讓nginx服務可以實現https訪問的功能 pcre-devel --- 兼容perl語言的正則表達式(^ shell:以什麼開頭 perl:^/) nginx使用時會應用一個參數rewrite 正則表達式信息(perl) |
#第三個里程:創建worker進程的管理用戶 useradd -s /sbin/nologin -M www |
#第四個里程:編譯安裝軟件過程 #編譯安裝軟件三部曲 #01.進行軟件的配置 mkdir /server/tools -p cd /server/tools cd nginx-1.14.0 ./configure --prefix=/application/nginx-1.14 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module #02.進行軟件的編譯(將各個語言編寫代碼翻譯成系統可以識別的二進制信息) make
#03.進行編譯安裝(將軟件最終安裝到系統中) make install |
#重要配置參數總結 --prefix=PATH set installation prefix 設置軟件程序安裝到哪個目錄,指定的目錄不需要創建出來 --user=USER set non-privileged user for worker processes --group=GROUP set non-privileged group for worker processes --with-http_ssl_module enable ngx_http_ssl_module(可以實現https) --with-http_stub_status_module enable ngx_http_stub_status_module(主要用於監控服務運行狀態) |
#第五個里程:創建程序軟鏈接 ln -s /application/nginx-1.14/ /application/nginx [root@web01 application]# ll /application/nginx-1.14/ total 16 drwxr-xr-x 2 root root 4096 May 16 10:49 conf drwxr-xr-x 2 root root 4096 May 16 10:49 html drwxr-xr-x 2 root root 4096 May 16 10:49 logs drwxr-xr-x 2 root root 4096 May 16 10:49 sbin |
#第六個里程:啓動nginx服務 /application/nginx/sbin/nginx |
1.2.3 Nginx 安裝目錄及命令解釋
目錄 | 說明 |
conf | --- 所有nginx相關的配置文件保存目錄 |
nginx.conf | --- nginx程序主配置文件 |
html | --- 表示web服務站點目錄 |
logs | --- 日誌目錄 |
sbin | --- 程序命令保存目錄 |
Nginx命令參數說明: nginx –help -----查詢nginx的幫助信息 Options: -?,-h : this help -v : show version and exit 顯示軟件版本信息並退出 -V : show version and configure options then exit 顯示版本信息和配置參數信息並退出 -t : test configuration and exit 配置文件語法檢查(ansible語法檢查功能) -T : test configuration, dump it and exit 配置文件語法檢查,將配置文件信息顯示出來 -q : suppress non-error messages during configuration testing -s signal : send signal to a master process: stop, quit, reopen, reload 發送一個信號給master進程 -p prefix : set prefix path (default: /usr/share/nginx/) -c filename : set configuration file (default: /etc/nginx/nginx.conf) -g directives : set global directives out of configuration file |
1.2.4 Nginx 主配置文件nginx.conf
1.2.5 Nginx 站點目錄及首頁文件概念說明
1.3 Nginx 虛擬主機配置
1.3.1 虛擬主機的概念
所謂虛擬主機,在Web服務裏就是一個獨立的網站站點,這個站點對應獨立的域名 (也可能是IP或端口),具有獨立的程序及資源目錄,可以獨立地對外提供服務供用戶訪問 這個獨立的站點在配置裏是由一定格式的標籤段標記的,對於Apache軟件來說 一個虛擬主機的標籤段通常被包含在<Virtuallost></VirtualHost>內,而Nginx軟件則 使用一個server{}標籤來標示一個虛擬主機。一個Web服務裏可以有多個虛擬主機標籤 對,即可以同時支持多個虛擬主機站點。 |
1.3.2 虛擬主機的類型
·常見的虛擬主機類型有如下幾種。
(1)基於域名的虛擬主機 所謂基於域名的虛擬主機,意思就是通過不同的域名區分不同的虛擬主機,基於域 名的虛擬主機是企業應用最廣的虛擬主機類型,幾乎所有對外提供服務的網站使用的都 是基於域名的虛擬主機,例如:www.etiantian.org。 |
(2)基於端口的虛擬主機 同理,所謂基於端口的虛擬主機,意思就是通過不同的端口來區分不同的虛擬主 機,此類虛擬主機對應的企業應用主要爲公司內部的網站,例如:一些不希望直接對 外提供用戶訪問的網站後臺等,訪問基於端口的虛擬主機,地址裏要帶有端口,例如: http://www.etiantian.org:9000。 |
(3)基於IP的虛擬主機 同理,所謂基於IP的虛擬主機,意思就是通過不同的IP區分不同的虛擬主機,此 類虛擬主機對應的企業應用非常少見。一般不同的業務需要使用多IP的場景都會在負 載均衡器上進行VIP綁定,而不是在Web上綁定IP來區分不同的虛擬機。 |
1.3.3 基於域名的虛擬主機配置
第一步:申請域名信息 先進行購買,域名備案,在申請域名網站上,可以進行A記錄的配置 www.oldboy.com -- 服務器公網IP地址 |
第二步:編寫配置文件 ----- nginx.conf server { listen 80; server_name www.etiantian.org; root html/www; index index.html index.htm; } server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } server { listen 80; server_name blog.etiantian.org; root html/blog; index index.html index.htm; } |
第三步:檢查配置文件語法信息 nginx -t |
第四步:創建虛擬主機站點目錄,創建虛擬主機首頁文件 mkdir -p /application/nginx/html/{www,bbs,blog} [root@web01 nginx]# for name in www bbs blog;do echo "10.0.0.7 $name.etiantian.org" >/application/nginx/html/$name/index.html;done [root@web01 nginx]# for name in www bbs blog;do cat /application/nginx/html/$name/index.html;done 10.0.0.7 www.etiantian.org 10.0.0.7 bbs.etiantian.org 10.0.0.7 blog.etiantian.org |
第五步:進行DNS解析(配置hosts) 配置hosts文件: 01. windows中要進行配置 02. Linux中也要進行配置 |
第六步:配置文件修改後要進行重啓服務 nginx -s reload |
1.3.4 基於端口的虛擬主機配置
第一里程:修改配置文件 server { listen 80; server_name www.etiantian.org; root html/www; index index.html index.htm; } server { listen 81; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } server { listen 82; server_name blog.etiantian.org; root html/blog; index index.html index.htm; } |
第二里程:平滑重啓nginx服務 nginx -s reload |
第三里程:進行訪問測試 netstat -lntup|grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 19363/nginx tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 19363/nginx tcp 0 0 0.0.0.0:82 0.0.0.0:* LISTEN 19363/nginx |
1.3.5 基於IP的虛擬主機配置
第一里程:編寫配置文件,監聽本地網卡上有的IP地址 server { listen 10.0.0.7:80; server_name www.etiantian.org; root html/www; index index.html index.htm; } server { listen 10.0.0.8:80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } |
第二里程:重啓nginx服務 重點強調:nginx服務配置文件中只要涉及到IP地址的修改,都要重啓nginx服務,而不是平滑重啓 nginx -s stop && nginx |
第三里程:進行訪問測試 curl 10.0.0.7 curl 10.0.0.8 |
1.4 Nginx 常用功能配置
1.4.1 規範優化Nginx配置文件
·目的:每一個虛擬主機都生成一個單獨的配置文件
第一個里程:創建一個虛擬主機配置文件保存目錄 [root@web01 conf]# pwd /application/nginx/conf [root@web01 conf]# mkdir extra |
第二個里程: 生成虛擬主機配置文件 /application/nginx/conf [root@web01 conf]# sed -n '10,15p' nginx.conf >extra/www.conf [root@web01 conf]# sed -n '16,21p' nginx.conf >extra/bbs.conf [root@web01 conf]# sed -n '22,27p' nginx.conf >extra/blog.conf [root@web01 conf]# cat extra/* server { listen 80; server_name bbs.etiantian.org; root html/bbs; index index.html index.htm; } server { listen 80; server_name blog.etiantian.org; root html/blog; index index.html index.htm; } server { listen 80; server_name www.etiantian.org; root html/www; index index.html index.htm; } 說明:虛擬主機文件配置完畢,要將nginx中的虛擬主機配置信息刪除
|
第三個里程:編寫nginx主配置文件,加載虛擬主機配置文件 ---nginx.conf worker_processes 2; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/blog.conf; include extra/bbs.conf; } ####也可用 include extra/*; 表示extra目錄下的所有配置文件。 |
第四個里程:重啓nginx服務 nginx -s reload |
1.4.2 配置Nginx 服務別名功能(主要用於測試)
#####修改配置文件 server { listen 80; server_name www.etiantian.org www.et.org; root html/www; index index.html index.htm; } #####檢查語法 nginx -t #####平滑重啓nginx nginx -s reload ####說明:配置別名也需要做好DNS解析 |
1.4.3 Nginx 狀態模塊頁面配置
Nginx軟件在編譯時有一個with-http_stub_status_module模塊,這個模塊功能是記錄Nginx的基本訪問狀態信息,讓使用者瞭解Nginx的工作狀態。要想使用狀態模塊,在編譯時必須增加--with-http_stub_status_module參數。
可通過如下方法檢測編譯安裝Nginx時是否設定上述模塊支持: |
[root@web01 ~]# nginx -V nginx version: nginx/1.14.0 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/application/nginx-1.14.0 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module |
第一個里程:編寫配置文件 # 編寫一個status虛擬主機配置文件 cp extra/www.conf extra/status.conf server { listen 80; server_name status.etiantian.org; stub_status; #root html/www; #index index.html index.htm; } # 編寫主配置文件,調用status虛擬主機配置文件 worker_processes 2; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; include extra/www.conf; include extra/bbs.conf; include extra/blog.conf; include extra/status.conf; } |
第二個里程:重啓nginx服務 nginx -t #--------檢查語法 nginx -s reload ###-------平滑重啓 |
第三個里程:進行DNS解析處理 |
第四個里程:進行測試訪問 [root@web01 ~]# curl status.etiantian.org Active connections: 1 server accepts handled requests 6926 6926 7067 Reading: 0 Writing: 1 Waiting: 0 |