Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

在 Linux 上打造 Nginx 服務環境:編譯安裝、主設定檔與虛擬主機全解析

一、Nginx 的安裝

1. 先備套件:PCRE 與 zlib

Nginx 讀作 [engine x],出自俄羅斯工程師 Igor Sysoev 之手,以 BSD 授權釋出,官方主頁為 https://nginx.net。這套伺服器軟體早年就在俄羅斯多個大型網站上長期服役,穩定度與效率都經得起考驗。

正式編譯之前,必須先備妥兩個相依套件:PCRE 負責正則表達式與 rewrite 重寫規則,zlib 則提供 gzip 網頁壓縮能力。缺少它們,後續不少常用功能都開不起來。

PCRE 可自 https://www.pcre.org/ 取得,下載適合自己環境的版本後編譯安裝:

tar zxvf pcre-7.7.tar.gz
cd pcre-7.7
./configure
make
make install

zlib 的來源是 https://www.zlib.net/,做法相同:

tar zxvf zlib-1.2.3.tar.gz
cd zlib-1.2.3
./configure
make
make install

2. 編譯與安裝 Nginx

相依套件就位後,回到 https://www.nginx.net/ 下載原始碼並解開:

[root@oracle132 /]# tar zxvf nginx-0.6.31.tar.gz
[root@oracle132 /]# cd nginx-0.6.31

實務上最常踩到的地雷是:Nginx 一定要 PCRE 模組,但在 RHEL 系列下,即使 PCRE 已安裝完成,configure 階段依然找不到對應函式庫。变通做法是手動把檔案補進 /usr/include/pcre:

[root@oracle132 nginx-0.6.31]# mkdir /usr/include/pcre
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.a
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/libpcre.la
[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/pcre.h /usr/include/pcre/pcre.h
[root@oracle132 nginx-0.6.31]# mkdir /usr/include/pcre/.libs
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.a
[root@oracle132 nginx-0.6.31]# cp /usr/local/lib/libpcre.a /usr/include/pcre/.libs/libpcre.la
[root@oracle132 nginx-0.6.31]# cp /oracle/pcre-7.7/pcre.h /usr/include/pcre/.libs/pcre.h

補完之後即可正式編譯。這裡帶上 --with-pcre 指定 PCRE 路徑,並啟用 http_stub_status_module 以便日後觀察狀態:

[root@oracle132 nginx-0.6.31]# ./configure --with-pcre=/usr/include/pcre --with-http_stub_status_module
[root@oracle132 nginx-0.6.31]# vi ./objs/Makefile(刪除該檔案第 1006 行的 “./configure --disable-shared”)
[root@oracle132 nginx-0.6.31]# make
[root@oracle132 nginx-0.6.31]# make install

預設會安裝到 /usr/local/nginx,設定檔就在 /usr/local/nginx/conf 底下。

二、主設定檔 nginx.conf 逐段拆解

[root@oracle132 conf]# vi nginx.conf

user nobody nobody; # 執行 Nginx 的系統使用者
worker_processes 1; # 工作程序數量
#error_log logs/error.log;
error_log logs/error.log notice; # 全域錯誤日誌與等級
pid logs/nginx.pid;

events 區塊決定連線模型與上限。Linux 上選 epoll 才能拿到理想的併發表現:

events {
  use epoll;
  worker_connections 1024;
}

http 區塊是核心,負載均衡、壓縮、日誌格式都在這裡定義:

http {
  include mime.types;
  default_type application/octet-stream;
  #log_format main '$remote_addr - $remote_user [$time_local] $request ' '"$status" $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
  #access_log logs/access.log main;
  client_header_buffer_size 1k; # 請求標頭緩衝
  large_client_header_buffers 4 4k;
  sendfile on;
  tcp_nopush on;
  keepalive_timeout 65;

接著是網頁壓縮設定,gzip 能明顯降低文字型資源的傳輸量:

  gzip on;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_proxied any;
  gzip_types text/plain text/html text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  gzip_min_length 1100;
  gzip_buffers 4 8k;

upstream 用來宣告後端伺服器清單,weight 是權值,數字越大被分派到的機率越高:

upstream ixdba{
  server 192.168.8.1:3128 weight=5; # 本機 Squid 開在 3128
  server 192.168.8.2:80 weight=1;
  server 192.168.8.3:80 weight=6;
}

最後把反向代理與各虛擬主機的設定檔引入:

  include /usr/local/nginx/conf/proxy.conf;
  include /usr/local/nginx/conf/vhosts/www.test.com.conf;
}

三、虛擬主機設定檔

每台虛擬主機各自一個檔案,例如 /usr/local/nginx/conf/vhosts/www.test.com.conf:

server {
  listen 80; # 監聽埠
  server_name www.test.com; # 對外域名
  charset UTF-8;
  #access_log logs/host.access.log main;
  location ~ \.jpg$ { # 圖片交給 Nginx 直接回應
    root /cicro/cws3/vhosts/www.test.com/ROOT;
    expires 30d;
  }
  location ~ \.gif$ {
    root /cicro/cws3/vhosts/www.test.com/ROOT;
    expires 30d;
  }
  location ~ ^/(upload|html)/ { # 上傳目錄與靜態頁同樣由 Nginx 處理
    root /cicro/cws3/vhosts/www.test.com/ROOT;
    expires 30d;
  }
  location / { # 其餘請求全部轉給後端 8009
    root /cicro/cws3/vhosts/www.test.com/ROOT;
    index index.html;
    proxy_pass https://127.0.0.1:8009;
  }
  location /NginxStatus { # 狀態頁,需帳密驗證
    access_log off;
    auth_basic "NginxStatus";
    auth_basic_user_file ../htpasswd;
  }
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root html;
  }
}

這種寫法把靜態資源留在本機、動態請求往後端丟,是相當典型的 Nginx 反向代理佈局。

四、以 htpasswd 保護狀態頁

狀態頁若要對外開放,建議加上基本驗證。可借用 Apache 的 htpasswd 產生密碼檔:

[root@oracle132 vhosts]# /usr/local/bin/htpasswd -c htpasswd gaojf
New password:(輸入密碼)
Re-type new password:(再次輸入)
Adding password for user gaojf

其中 -c 表示建立新檔,htpasswd 是檔名,gaojf 為使用者帳號。若系統裡沒有這支程式,直接從 Apache 的 bin 目錄複製一份即可。

完成後開瀏覽器前往 https://www.test.com/nginxstatus/,輸入帳密即可看到類似內容:

Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324

第一行是當前活躍連線數;第三行的第三個數字是 Nginx 啟動至今累計處理的請求總數,若已逼近上限,就該調高 worker_connections 等相關數值了。

Copyright © Linux教程網 All Rights Reserved