Nginx基礎配置及構建web虛擬主機
阿新 • • 發佈:2022-03-13
Nginx基礎配置及構建web虛擬主機
一,Nginx服務的主配置檔案 nginx.conf
vim /usr/local/nginx/conf/nginx.conf 1.全域性配置 #user nobody; #執行使用者,若編譯時未指定則預設為 nobody worker_processes 1; #工作程序數量,可配置成伺服器核心數 * 2,如果網站訪問量不大,一般設為1就夠用了 #error_log logs/error.log; #錯誤日誌檔案的位置 #pid logs/nginx.pid; #PID 檔案的位置 2.I/O 事件配置 events { use epoll; #使用 epoll 模型,2.6及以上版本的系統核心,建議使用epoll模型以提高效能 worker_connections 4096; #每個程序處理 4096 個連線 } 臨時設定: #如提高每個程序的連線數還需執行“ulimit -n 65535”命令臨時修改本地每個程序可以同時開啟的最大檔案數。 #在Linux平臺上,在進行高併發TCP連線處理時,最高的併發數量都要受到系統對使用者單一程序同時可開啟檔案數量的限制(這是因為系統為每個TCP連線都要建立一個socket控制代碼,每個socket控制代碼同時也是一個檔案控制代碼)。 #可使用ulimit -a命令檢視系統允許當前使用者程序開啟的檔案數限制。 永久設定: /etc/security/limits.conf 末尾新增: soft nofile 59000 hard nofile 60000 #epoll是Linux核心為處理大批控制代碼而作改進的poll,是Linux下多路複用IO介面select/poll的增強版本,它能顯著的減少程式在大量併發連線中只有少量活躍的情況下的系統CPU利用率。 若工作程序數為 8,每個程序處理 4 096 個連線,則允許 Nginx 正常提供服務的連線數 已超過 3 萬個(4 096×8=32 768),當然具體還要看伺服器硬體、網路頻寬等物理條件的性 能表現。 3.HTTP 配置 使用“http { }”界定標記,包括訪問日誌、HTTP 埠、網頁目錄、預設字符集、連線保 持,以及後面要講到的虛擬 Web 主機、PHP 解析等一系列設定,其中大部分配置語句都包 含在子界定標記“server { }”內 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; ##支援檔案傳送(下載) sendfile on; ##此選項允許或禁止使用socket的TCP_CORK的選項(傳送資料包前先快取資料),此選項僅在使用sendfile的時候使用 #tcp_nopush on; ##連線保持超時時間,單位是秒 #keepalive_timeout 0; keepalive_timeout 65; ##gzip模組設定,設定是否開啟gzip壓縮輸出 #gzip on; ##Web 服務的監聽配置 server { ##監聽地址及埠 listen 80; ##站點域名,可以有多個,用空格隔開 server_name www.ceshi.com; ##網頁的預設字符集 charset utf-8; ##根目錄配置 location / { ##網站根目錄的位置/usr/local/nginx/html root html; ##預設首頁檔名 index index.html index.php; } ##內部錯誤的反饋頁面 error_page 500 502 503 504 /50x.html; ##錯誤頁面配置 location = /50x.html { root html; } } } ---------------------------------------------------------------------------------------------------------- 日誌格式設定: $remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址; $remote_user:用來記錄客戶端使用者名稱稱; $time_local: 用來記錄訪問時間與時區; $request: 用來記錄請求的url與http協議; $status: 用來記錄請求狀態;成功是200, $body_bytes_sent :記錄傳送給客戶端檔案主體內容大小; $http_referer:用來記錄從哪個頁面連結訪問過來的; $http_user_agent:記錄客戶瀏覽器的相關資訊; 通常web伺服器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了, 通過$remote_add拿到的IP地址是反向代理伺服器的iP地址。 反向代理伺服器在轉發請求的http頭資訊中,可以增加x_forwarded_for資訊, 用以記錄原有客戶端的IP地址和原來客戶端的請求的伺服器地址。 location常見配置指令,root、alias、proxy_pass root(根路徑配置):root /var/www/html 請求www.ceshi.com/test/1.html,會返回檔案/var/www/html/test/1.html alias(別名配置):alias /var/www/html 請求www.ceshi.com/test/1.html,會返回檔案/var/www/html/1.html proxy_pass(反向代理配置)
二,訪問狀態統計配置
1.先使用命令/usr/local/nginx/sbin/nginx -V 檢視已安裝的 Nginx 是否包含 HTTP_STUB_STATUS 模組 cat /opt/nginx-1.12.0/auto/options | grep YES #可檢視 nginx 已安裝的所有模組 2.修改 nginx.conf 配置檔案,指定訪問位置並新增 stub_status 配置 cd /usr/local/nginx/conf cp nginx.conf nginx.conf.bak vim /usr/local/nginx/conf/nginx.conf ...... http { ...... server { listen 80; server_name www.ceshi.com; charset utf-8; location / { root html; index index.html index.php; } ##新增 stub_status 配置## location /status { #訪問位置為/status stub_status on; #開啟狀態統計功能 access_log off; #關閉此位置的日誌記錄 } } } 3.重啟服務,訪問測試 systemctl restart nginx 瀏覽器訪問 http://192.168.10.10/status Active connections :表示當前的活動連線數; server accepts handled requests :表示已經處理的連線資訊, 三個數字依次表示已處理的連線數、成功的TCP握手次數、 已處理的請求數。 可 curl -s http://192.168.10.10/status | awk '/Active/{print $3}' | if 結合 awk與if 語句進行效能監控。
三,基於授權的訪問控制
1.生成使用者密碼認證檔案 yum install -y httpd-tools htpasswd -c /usr/local/nginx/passwd.db zhangsan chown nginx /usr/local/nginx/passwd.db chmod 400 /usr/local/nginx/passwd.db 2.修改主配置檔案相對應目錄,新增認證配置項 vim /usr/local/nginx/conf/nginx.conf ...... server { location / { ...... ##新增認證配置## auth_basic "secret"; #設定密碼提示框文字資訊 auth_basic_user_file /usr/local/nginx/passwd.db; } } 3.重啟服務,訪問測試 nginx -t systemctl restart nginx 瀏覽器訪問 http://192.168.10.10
四,基於客戶端的訪問控制
訪問控制規則如下:
deny IP/IP 段:拒絕某個 IP 或 IP 段的客戶端訪問。
allow IP/IP 段:允許某個 IP 或 IP 段的客戶端訪問。
規則從上往下執行,如匹配則停止,不再往下匹配。
vim /usr/local/nginx/conf/nginx.conf
......
server {
location / {
......
##新增控制規則##
allow 192.168.10.200; #允許訪問的客戶端 IP
deny all; #拒絕其它IP客戶端訪問
}
}
systemctl restart nginx
五,三種Nginx虛擬主機
1.基於域名的 Nginx 虛擬主機
1.為虛擬主機提供域名解析
echo "192.168.10.10 www.ceshi.com www.mhy.com" >> /etc/hosts
2.為虛擬主機準備網頁文件
mkdir -p /var/www/html/mhy
mkdir -p /var/www/html/ceshi
echo "<h1>www.ceshi.com</h1>" > /var/www/html/ceshi/index.html
echo "<h1>www.mhy.com</h1>" > /var/www/html/mhy/index.html
3.修改Nginx的配置檔案
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 10;
server_name www.ceshi.com; #設定域名www.ceshi.com
charset utf-8;
access_log logs/www.ceshi.access.log; #設定日誌名
location / {
root /var/www/html/ceshi; #設定www.ceshi.com 的工作目錄
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 10;
server_name www.mhy.com; #設定域名www.mhy.com
charset utf-8;
access_log logs/www.mhy.access.log;
location / {
root /var/www/html/mhy;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
4.重啟服務,訪問測試
systemctl restart nginx
瀏覽器訪問
http://www.ceshi.com
http://www.mhy.com
2.基於IP 的 Nginx 虛擬主機
ifconfig ens33:0 192.168.10.11 netmask 255.255.255.0
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.10.10:10; #設定監聽地址192.168.10.10
server_name www.ceshi.com;
charset utf-8;
access_log logs/www.ceshi.access.log;
location / {
root /var/www/html/ceshi;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.10.11:10; #設定監聽地址192.168.10.11
server_name www.mhy.com;
charset utf-8;
access_log logs/www.mhy.access.log;
location / {
root /var/www/html/mhy;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
瀏覽器訪問
http://192.168.10.10
http://192.168.10.11
3.基於埠的 Nginx 虛擬主機
vim /usr/local/nginx/conf/nginx.conf
......
http {
......
server {
listen 192.168.10.10:1010; #設定監聽 1010 埠
server_name www.ceshi.com;
charset utf-8;
access_log logs/www.ceshi.access.log;
location / {
root /var/www/html/ceshi;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
server {
listen 192.168.10.10:8888; #設定監聽 8888 埠
server_name www.mhy.com;
charset utf-8;
access_log logs/www.mhy.access.log;
location / {
root /var/www/html/mhy;
index index.html index.php;
}
error_page 500 502 503 504 /50x.html;
location = 50x.html{
root html;
}
}
}
systemctl restart nginx
瀏覽器訪問
http://192.168.10.11:1010
http://192.168.10.11:8888