1.1、 介紹:
- Nginx (engine x) 是一個高性能的HTTP和反向代理web服務器,同時也提供了IMAP/POP3/SMTP服務。Nginx是由伊戈爾·賽索耶夫爲俄羅斯訪問量第二的Rambler.ru站點(俄文:Рамблер)開發的,第一個公開版本0.1.0發佈於2004年10月4日。
- 其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。2011年6月1日,nginx 1.0.4發佈。
- 在連接高併發的情況下,Nginx是Apache服務不錯的替代品:Nginx在美國是做虛擬主機生意的老闆們經常選擇的軟件平臺之一。能夠支持高達 50,000 個併發連接數的響應,感謝Nginx爲我們選擇了 epoll and kqueue作爲開發模型。
1.2、優點
- Nginx 可以在大多數 UnixLinux OS 上編譯運行,並有 Windows 移植版。 Nginx 的1.4.0穩定版已經於2013年4月24日發佈,一般情況下,對於新建站點,建議使用最新穩定版作爲生產版本,已有站點的升級急迫性不高。Nginx 的源代碼使用 2-clause BSD-like license。
- Nginx 是一個很強大的高性能Web和反向代理服務,它具有很多非常優越的特性:
- 在連接高併發的情況下,Nginx是Apache服務不錯的替代品:Nginx在美國是做虛擬主機生意的老闆們經常選擇的軟件平臺之一。能夠支持高達 50,000 個併發連接數的響應,感謝Nginx爲我們選擇了 epoll and kqueue作爲開發模型。
1.3、nginx服務器
- Nginx作爲負載均衡服務:Nginx 既可以在內部直接支持 Rails 和 PHP 程序對外進行服務,也可以支持作爲 HTTP代理服務對外進行服務。Nginx採用C進行編寫,不論是系統資源開銷還是CPU使用效率都比 Perlbal 要好很多。
- 處理靜態文件,索引文件以及自動索引;打開文件描述符緩衝。
- 無緩存的反向代理加速,簡單的負載均衡和容錯。
- FastCGI,簡單的負載均衡和容錯。
- 模塊化的結構。包括 gzipping, byte ranges, chunked responses,以及 SSI-filter 等 filter。如果由 FastCG或其它代理服務器處理單頁中存在的多個 SSI,則這項處理可以並行運行,而不需要相互等待。
- 支持 SSL 和 TLSSNI。
1.4、nginx代碼
- Nginx代碼完全用C語言從頭寫成,已經移植到許多體系結構和操作系統,包括:Linux、FreeBSD、Solaris、Mac OS X、AIX以及Microsoft Windows。Nginx有自己的函數庫,並且除了zlib、PCRE和OpenSSL之外,標準模塊只使用系統C庫函數。而且,如果不需要或者考慮到潛在的授權衝突,可以不使用這些第三方庫。
1.5、代理服務器
- 作爲郵件代理服務:Nginx 同時也是一個非常優秀的郵件代理服務(最早開發這個產品的目的之一也是作爲郵件代理服務器),Last.fm 描述了成功並且美妙的使用經驗。
- Nginx 是一個安裝非常的簡單、配置文件非常簡潔(還能夠支持perl語法)、Bug非常少的服務。Nginx 啓動特別容易,並且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啓動。你還能夠不間斷服務的情況下進行軟件版本的升級。
接下來開始nginx的環境部署
接下來進行nginx的安裝
nginx安裝包鏈接如下鏈接:https://pan.baidu.com/s/1iAI0ZVY13lvdMSgtdiAU5Q
提取碼:1prj
- 主機分配如下
服務 | IP |
---|---|
nginx | 192.168.20.30 |
2、nginx的安裝命令
[root@lpj3 ~]# useradd -M -s /sbin/nologin nginx
[root@lpj3 ~]# yum -y install gcc gcc-c++ openssl-devel zlib-devel pcre-devel
[root@lpj3 ~]# tar -zxf nginx-1.11.5.tar.gz
[root@lpj3 ~]# cd nginx-1.11.5
[root@lpj3 nginx-1.11.5]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
[root@lpj3 nginx-1.11.5]# make && make install
[root@lpj3 nginx-1.11.5]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
查看當前nginx的版本
[root@lpj3 nginx-1.11.5]# nginx -v
nginx version: nginx/1.11.5
nginx的常用命令如下
檢測配置文件語法: nginx -t
啓動: nginx
關閉: nginx -s stop
重新加載: nginx -s reload
重啓:先停止 再開啓
啓動成功後 訪問本機IP 則訪問到如下頁面 證明nginx安裝成功
訪問本機IP:http://192.168.20.30/
如果訪問本機IP不成功的話放行端口進行操作
[root@lpj3 nginx-1.11.5]# firewall-cmd --zone=public --add-port=80/tcp --permanent ##添加端口80
success
[root@lpj3 nginx-1.11.5]# firewall-cmd --reload 刷新
success
3.、nginx中配置文件的層次
events {
events裏的配置項
}
http {
server {
location / {
}
}
}
nginx中各模塊的含義
- main 用於nginx全局信息的配置
- events nginx工作模式的配置(nginx支持的模型有select poll epoll - - kqueue(BSD系統) kqueue效率很高 類似於epoll)
- http http協議信息的配置
- upstream 負載均衡模塊(默認沒有 需要自己手寫 與server是平行模塊)
- server 服務器訪問信息配置
- location 用於進行訪問的路由 location會帶有獨特的匹配表示 用於匹配不同的請求
4、 基於nginx的虛擬主機的實現
- 虛擬主機定義:
- 在web服務中就是一個獨立的網站站點,這個獨立的站點可能會有獨立的域名、IP、端口,具有獨立的程序和資源目錄,可以獨立的對外提供訪問服務
下面進行操作 - 基於域名的虛擬主機
[root@lpj3 nginx-1.11.5]# vim /usr/local/nginx/conf/nginx.conf
在原有的server下在添加一個server 內容如下
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
更改後內容如下:
server {
listen 80;
server_name www.xiyangyang.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/meiyangyang;
index index.html index.htm;
}
}
server {
listen 80;
server_name nuanyangyang;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/feiyangyang;
index index.html index.htm;
}
創建相對應的目錄並做域名解析
[root@lpj3 nginx-1.11.5]# mkdir /usr/local/nginx/html/{meiyangyang,feiyangyang}
[root@lpj3 nginx-1.11.5]# echo "喜羊羊和灰太狼" > /usr/local/nginx/html/meiyangyang/index.html
[root@lpj3 nginx-1.11.5]# echo "狼太灰和洋洋喜" > /usr/local/nginx/html/meiyangyang/index.html
[root@lpj3 nginx-1.11.5]# vim /etc/hosts
192.168.20.30 www.xiyangyang.com
192.168.20.30 www.nuanyangyang.com
檢測語法並重新加載nginx
[root@lpj3 nginx-1.11.5]# 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@lpj3 nginx-1.11.5]# nginx -s reload
進行驗證
- curl http://www.xiyangyang.com
- curl http://www.nuanyangyang.com
- [root@lpj3 nginx-1.11.5]# curl http://www.xiyangyang.com
- 喜羊羊和灰太狼
- [root@lpj3 nginx-1.11.5]# curl http://www.nuanyangyang.com
- 狼太灰和洋洋喜
基於IP的虛擬主機
- 添加一個IP用於測試
[root@lpj3 ~]# ifconfig ens33:2 192.168.20.170/24
[root@lpj3 ~]# ifconfig | grep 170
inet 192.168.20.170 netmask 255.255.255.0 broadcast 192.168.20.255
修改配置文件
server {
listen 80;
server_name 192.168.20.170;#域名更改爲IP
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/meiyangyang;
index index.html index.htm;
}
}
server {
listen 80;
server_name 192.168.20.30; #把域名更改爲IP
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/feiyangyang;
index index.html index.htm;
}
檢測語法並重新加載nginx
[root@lpj3 nginx-1.11.5]# 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@lpj3 nginx-1.11.5]# nginx -s reload
進行驗證
[root@lpj3 ~]# curl http://192.168.20.30
狼太灰和洋洋喜
[root@lpj3 ~]# curl http://192.168.20.170
喜羊羊和灰太狼
基於不同端口的虛擬主機的實現
更改配置文件
server {
listen 80;#這個端口保持不變
server_name www.xiyangyang.com;IP更改爲域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/meiyangyang;
index index.html index.htm;
}
}
server {
listen 9999;#默認端口爲80,隨機更改 更改端口數字最好大於9000
server_name www.nuanyangyang.com; #把IP更改爲域名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /usr/local/nginx/html/feiyangyang;
index index.html index.htm;
}
檢測語法並重新加載nginx
[root@lpj3 nginx-1.11.5]# 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@lpj3 ~]# curl http://192.168.20.30
喜羊羊和灰太狼
[root@lpj3 ~]# curl http://192.168.20.30:9999
狼太灰和洋洋喜