Nginx是一款輕量級的網頁服務器、反向代理服務器。相較於Apache、lighttpd具備佔有內存少,穩定性高等優點。**它最常的用途是提供反向代理服務。**
1.安裝gcc gcc-c++(如新環境,未安裝請先安裝)javascript
$ yum install -y gcc gcc-c++
2.安裝PCRE庫php
$ 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庫css
$ cd /usr/local/
$ wget http://zlib.net/zlib-1.2.11.tar.gz
$ tar -zxvf zlib-1.2.11.tar.gz
$ ./configure
$ make && make installhtml
4.安裝nginxjava
$ 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
時提示如下錯誤:linux
./configure: error: SSL modules require the OpenSSL library.nginx
支持此命令:c++
yum -y install openssl openssl-devel正則表達式
報錯:./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 已經安裝並運行成功。
部分命令以下:
重啓:
$ /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 的模塊文檔中找到詳細的配置說明。
*********************************************************************************
文章轉自:http://www.javashuo.com/article/p-encyalxj-cq.html
Nginx 是 C語言 開發,建議在 Linux 上運行,固然,也能夠安裝 Windows 版本,本篇則使用 CentOS 7 做爲安裝環境。
一. gcc 安裝
安裝 nginx 須要先將官網下載的源碼進行編譯,編譯依賴 gcc 環境,若是沒有 gcc 環境,則須要安裝:
yum install gcc-c++
二. PCRE pcre-devel 安裝
PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx也須要此庫。命令:
yum install -y pcre pcre-devel
三. zlib 安裝
zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫。
yum install -y zlib zlib-devel
四. OpenSSL 安裝
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。
nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫。
yum install -y openssl openssl-devel
1.直接下載.tar.gz
安裝包,地址:https://nginx.org/en/download.html
2.使用wget
命令下載(推薦)。確保系統已經安裝了wget,若是沒有安裝,執行 yum install wget 安裝。
wget -c https://nginx.org/download/nginx-1.12.0.tar.gz
我下載的是1.12.0版本,這個是目前的穩定版。
依然是直接命令:
tar -zxvf nginx-1.12.0.tar.gz cd nginx-1.12.0
其實在 nginx-1.12.0 版本中你就不須要去配置相關東西,默認就能夠了。固然,若是你要本身配置目錄也是能夠的。
1.使用默認配置
./configure
2.自定義配置(不推薦)
./configure \
--prefix=/usr/local/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --pid-path=/usr/local/nginx/conf/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-http_gzip_static_module \ --http-client-body-temp-path=/var/temp/nginx/client \ --http-proxy-temp-path=/var/temp/nginx/proxy \ --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \ --http-scgi-temp-path=/var/temp/nginx/scgi
注:將臨時文件目錄指定爲/var/temp/nginx,須要在/var下建立temp及nginx目錄
make make install
查找安裝路徑:
whereis nginx
![nginx-whereis.png](http://static.javashuo.com/static/loading.gif)
cd /usr/local/nginx/sbin/ ./nginx ./nginx -s stop ./nginx -s quit ./nginx -s reload
啓動時報80端口被佔用:
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
解決辦法:一、安裝net-tool 包:
yum install net-tools
./nginx -s quit
:此方式中止步驟是待nginx進程處理任務完畢進行中止。./nginx -s stop
:此方式至關於先查出nginx進程id再使用kill命令強制殺掉進程。
查詢nginx進程:
ps aux|grep nginx
1.先中止再啓動(推薦):
對 nginx 進行重啓至關於先中止再啓動,即先執行中止命令再執行啓動命令。以下:
./nginx -s quit ./nginx
2.從新加載配置文件:
當 ngin x的配置文件 nginx.conf 修改後,要想讓配置生效須要重啓 nginx,使用-s reload
不用先中止 ngin x再啓動 nginx 便可將配置信息在 nginx 中生效,以下:
./nginx -s reload
啓動成功後,在瀏覽器能夠看到這樣的頁面:
即在rc.local
增長啓動代碼就能夠了。
vi /etc/rc.local
增長一行 /usr/local/nginx/sbin/nginx
設置執行權限:
chmod 755 rc.local
到這裏,nginx就安裝完畢了,啓動、中止、重啓操做也都完成了,固然,你也能夠添加爲系統服務,我這裏就不在演示了。
--------------------------------------分割線 --------------------------------------
Nginx負載均衡配置實戰 http://www.linuxidc.com/Linux/2014-12/110036.htm
CentOS 6.2實戰部署Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm
使用Nginx搭建WEB服務器 http://www.linuxidc.com/Linux/2013-09/89768.htm
搭建基於Linux6.3+Nginx1.2+PHP5+MySQL5.5的Web服務器全過程 http://www.linuxidc.com/Linux/2013-09/89692.htm
CentOS 6.3下Nginx性能調優 http://www.linuxidc.com/Linux/2013-09/89656.htm
CentOS 6.3下配置Nginx加載ngx_pagespeed模塊 http://www.linuxidc.com/Linux/2013-09/89657.htm
CentOS 6.4安裝配置Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm
Nginx安裝配置使用詳細筆記 http://www.linuxidc.com/Linux/2014-07/104499.htm
Nginx日誌過濾 使用ngx_log_if不記錄特定日誌 http://www.linuxidc.com/Linux/2014-07/104686.htm
--------------------------------------分割線 --------------------------------------
Nginx 的詳細介紹:請點這裏
Nginx 的下載地址:請點這裏
參考:
https://blog.csdn.net/wxyjuly/article/details/79443432
https://www.cnblogs.com/boonya/p/7907999.html