引言:nginx可以用作web服務器,也可以用作反向代理服務器,本文介紹nginx的下載,安裝,配置及啓停等基本命令
1、nginx下載
可以直接在nginx的官網地址:https://nginx.org/en/download.html下載穩定版本(stable version)
或者直接使用centos的wget命令
# cd /usr/local
# wget http://nginx.org/download/nginx-1.16.1.tar.gz
2、nginx安裝
先執行以下命令,安裝nginx依賴庫,如果缺少依賴庫,可能會安裝失敗,具體可以參考文章後面的錯誤提示信息。
yum install gcc-c++
yum install pcre
yum install pcre-devel
yum install zlib
yum install zlib-devel
yum install openssl
yum install openssl-devel
解壓安裝包
tar -zxvf nginx-1.16.1.tar.gz
nginx被解壓到了/usr/local/nginx-1.16.1 目錄下(不要把壓縮包解壓到/usr/local/nginx目錄下,或者將解壓後的目錄重命名爲nginx,因爲nginx會默認安裝到/usr/local/nginx目錄下),切換到nginx-1.16.1目錄
cd /usr/local/nginx-1.16.1/
./configure
該操作會檢測當前系統環境,以確保能成功安裝nginx,執行該操作後可能會出現以下幾種提示:
checking for OS
+ Linux 3.10.0-123.el7.x86_64 x86_64
checking for C compiler ... not found
./configure: error: C compiler cc is not found
如果出現以上錯誤提示信息,執行yum install gcc-c++安裝gcc,
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
如果出現上面提示,表示缺少PCRE庫
./configure: error: the HTTP gzip module requires the zlib library.
You can either disable the module by using --without-http_gzip_module
option, or install the zlib library into the system, or build the zlib library
statically from the source with nginx by using --with-zlib=<path> option.
如果出現以上提示,表示缺少zlib庫。 如果沒有出現./configure: error提示,表示當前環境可以安裝nginx,執行make和make install編譯nginx
make
make install
沒有出錯的話,表示nginx已經成功安裝完成,默認安裝位置爲/usr/local/nginx。 如果出現cp: ‘conf/koi-win’ and ‘/usr/local/nginx/conf/koi-win’ are the same file,可能是你把安裝包解壓到了/usr/local/nginx目錄,解決辦法是將該目錄重命名爲其他名稱後再執行make,make install.
3、nginx啓動
/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
4、nginx配置
以上安裝方法nginx的配置文件位於
/usr/local/nginx/conf/nginx.conf
Nginx配置文件常見結構的從外到內依次是「http」「server」「location」等等,缺省的繼承關係是從外到內,也就是說內層塊會自動獲取外層塊的值作爲缺省值。
4.1 Server
接收請求的服務器需要將不同的請求按規則轉發到不同的後端服務器上,在 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的文件都包含進去啦。
4.2 Localtion
每個 url 請求都會對應的一個服務,nginx 進行處理轉發或者是本地的一個文件路徑,或者是其他服務器的一個服務路徑。而這個路徑的匹配是通過 location 來進行的。我們可以將 server 當做對應一個域名進行的配置,而 location 是在一個域名下對更精細的路徑進行配置。
以上面的例子,可以將root和index指令放到一個location中,那麼只有在匹配到這個location時纔會訪問root後的內容:
location / {
root /data/www/host2;
index index.html index.htm;
}
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
4.3 靜態文件映射
訪問文件的配置主要有 root 和 aliasp’s 兩個指令。這兩個指令的區別容易弄混:
alias
alias後跟的指定目錄是準確的,並且末尾必須加 /。
location /c/ {
alias /a/;
}
如果訪問站點http://location/c訪問的就是/a/目錄下的站點信息。
root
root後跟的指定目錄是上級目錄,並且該上級目錄下要含有和location後指定名稱的同名目錄纔行。
location /c/ {
root /a/;
}
這時訪問站點http://location/c訪問的就是/a/c目錄下的站點信息。
如果你需要將這個目錄展開,在這個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://nginx.org/en/docs/。
一個配置nginx請求轉發的config配置文件示例(刪除原nginx.conf的註釋部分):
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 9000;
server_name localhost;
location /{
root html;
index index.html index.htm;
}
location ^~ /api/ {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';
proxy_pass http://127.0.0.1:9003/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
文章主要參考以下文章並結合實踐以及整理:
Centos下 Nginx安裝與配置:https://www.jianshu.com/p/d5114a2a2052
CentOS 7 安裝Nginx 並配置自動啓動:https://blog.csdn.net/stinkstone/article/details/78082748