Nginx是一款輕量級的網頁服務器、反向代理服務器。相較於Apache、lighttpd具備佔有內存少,穩定性高等優點。**它最常的用途是提供反向代理服務。**
1.安裝gcc gcc-c++(如新環境,未安裝請先安裝)javascript
$ yum install -y gcc gcc-c++
2.安裝PCRE庫html
$ cd /usr/local/
$ wget http://jaist.dl.sourceforge.net/project/pcre/pcre/8.33/pcre-8.33.tar.gz
$ tar -zxvf pcre-8.36.tar.gz
$ cd pcre-8.36
$ ./configure
$ make && make install
如報錯:configure: error: You need a C++ compiler for C++ support
解決:yum install -y gcc gcc-c++
3.安裝SSL庫java
$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxvf zlib-1.2.11.tar.gz
$ ./configure
$ make && make installnginx
4.安裝nginxc++
$ cd /usr/local/
$ wget http://nginx.org/download/nginx-1.8.0.tar.gz
$ tar -zxvf nginx-1.8.0.tar.gz
$ cd nginx-1.8.0
$ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_realip_module --with-http_sub_module --with-http_ssl_module
(注: --with-http_ssl_module:這個不加後面在nginx.conf配置ssl:on後,啓動會報nginx: [emerg] unknown directive "ssl" in /opt/nginx/conf/nginx.conf 異常)
$ make && make install
報錯:./configure: error: the HTTP gzip module requires the zlib library
在–prefix後面接如下命令:正則表達式
--with-pcre=/usr/local/pcre-8.36 指的是pcre-8.36 的源碼路徑。--with-zlib=/usr/local/zlib-1.2.8 指的是zlib-1.2.8 的源碼路徑。
5.啓動後端
$ /usr/local/nginx/sbin/nginx
檢查是否啓動成功:瀏覽器
打開瀏覽器訪問此機器的 IP,若是瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。ruby
部分命令以下:
重啓:
$ /usr/local/nginx/sbin/nginx –s reload
中止:
$ /usr/local/nginx/sbin/nginx –s stop
測試配置文件是否正常:
$ /usr/local/nginx/sbin/nginx –t
強制關閉:
$ pkill nginx
以上安裝方法nginx的配置文件位於
/usr/local/nginx/conf/nginx.conf
Nginx配置文件常見結構的從外到內依次是「http」「server」「location」等等,缺省的繼承關係是從外到內,也就是說內層塊會自動獲取外層塊的值做爲缺省值。
接收請求的服務器須要將不一樣的請求按規則轉發到不一樣的後端服務器上,在 nginx 中咱們能夠經過構建虛擬主機(server)的概念來將這些不一樣的服務配置隔離。
server {
listen 80;
server_name localhost;
root html;
index index.html index.htm;
}
例如咱們筆戈玩下的兩個子項目 passport 和 wan 就能夠經過在 nginx 的配置文件中配置兩個 server,servername 分別爲 passport.bigertech.com 和 wan.bigertech.com。這樣的話不一樣的 url 請求就會對應到 nginx 相應的設置,轉發到不一樣的後端服務器上。
這裏的 listen 指監聽端口,server_name 用來指定IP或域名,多個域名對應統一規則能夠空格分開,index 用於設定訪問的默認首頁地址,root 指令用於指定虛擬主機的網頁跟目錄,這個地方能夠是相對地址也能夠是絕對地址。
一般狀況下咱們能夠在 nginx.conf 中配置多個server,對不一樣的請求進行設置。就像這樣:
server {
listen 80;
server_name host1;
root html;
index index.html
index.htm;
}
server {
listen 80;
server_name host2;
root /data/www/html;
index index.html index.htm;
}
可是當 server 超過2個時,建議將不一樣對虛擬主機的配置放在另外一個文件中,而後經過在主配置文件 nginx.conf 加上 include 指令包含進來。更便於管理。
include vhosts/*.conf;
就能夠把vhosts的文件都包含進去啦。
Localtion
每一個 url 請求都會對應的一個服務,nginx 進行處理轉發或者是本地的一個文件路徑,或者是其餘服務器的一個服務路徑。而這個路徑的匹配是經過 location 來進行的。咱們能夠將 server 當作對應一個域名進行的配置,而 location 是在一個域名下對更精細的路徑進行配置。
以上面的例子,能夠將root和index指令放到一個location中,那麼只有在匹配到這個location時纔會訪問root後的內容:
location / {
}
location 匹配規則
~ 波浪線表示執行一個正則匹配,區分大小寫
~* 表示執行一個正則匹配,不區分大小寫
^~ ^~表示普通字符匹配,若是該選項匹配,只匹配該選項,不匹配別的選項,通常用來匹配目錄
= 進行普通字符精確匹配
匹配例子:
location = / {
# 只匹配"/". [ configuration A ]
}
location / {
# 匹配任何請求,由於全部請求都是以"/"開始 # 可是更長字符匹配或 者正則表達式匹配會優先匹配 [ configuration B ]
}
location ^~ /images/ {
# 匹配任何以 /images/ 開始的請求,並中止匹配 其它
location [ configuration C ]
}
location ~* .(gif|jpg|jpeg)$ {
# 匹配以 gif, jpg, or jpeg結尾的請求.
# 可是全部 /images/ 目錄的請求將由 [Configuration C]處理.
[ configuration D ]
}
請求:/ -> 符合configuration A
/documents/document.html -> 符合configuration B
/images/1.gif -> 符合configuration C
/documents/1.jpg ->符合 configuration D
靜態文件映射
訪問文件的配置主要有 root 和 aliasp’s 兩個指令。這兩個指令的區別容易弄混:
alias
alias後跟的指定目錄是準確的,而且末尾必須加 /。
location /c/ {
alias /a/;
}
root
root後跟的指定目錄是上級目錄,而且該上級目錄下要含有和location後指定名稱的同名目錄才行。
location /c/ {
root /a/;
}
若是你須要將這個目錄展開,在這個location的末尾加上「autoindex on; 」就能夠了
轉發
配置起來很簡單好比我要將全部的請求到轉移到真正提供服務的一臺機器的 8001 端口,只要這樣:
location / {
proxy_pass 172.16.1.1:8001;
}
這樣訪問host時,就都被轉發到 172.16.1.1的8001端口去了。
負載均衡
upstream myserver; {
ip_hash;
server 172.16.1.1:8001;
server 172.16.1.2:8002;
server 172.16.1.3;
server 172.16.1.4;
}
location / {
proxy_pass http://myserver;
}
咱們在 upstream 中指定了一組機器,並將這個組命名爲 myserver,這樣在 proxypass 中只要將請求轉移到 myserver 這個 upstream 中咱們就實現了在四臺機器的反向代理加負載均衡。其中的 ip_hash 指明瞭咱們均衡的方式是按照用戶的 ip 地址進行分配。另外還有輪詢、指定權重輪詢、fair、url_hash幾種調度算法。
以上是最簡單的經過 nginx 實現靜態文件轉發、反向代理和負載均衡的配置。在 nginx 中全部的功能都是經過模塊來實現的,好比當咱們配置 upstream 時是用 upstream 模塊,而 server 和 location 是在 http core 模塊,其餘的還有流控的 limt 模塊,郵件的 mail 模塊,https 的 ssl 模塊。他們的配置都是相似的能夠再 nginx 的模塊文檔中找到詳細的配置說明。