目錄
一、nginx的最大并發(fā)數(shù)設置
二、nginx的狀態(tài)收集模塊
三、nginx的用戶身份驗證
四、nginx的location中root和alias的區(qū)別
五、nginx訪問黑白名單限制
六、nginx的虛擬主機設置
第一種:基于不同域名的虛擬主機
第二種:基于不同ip地址
第三種:基于不同的端口
一、nginx的最大并發(fā)數(shù)設置
分為應用程序的設置和系統(tǒng)內(nèi)核的設置
設置:
##應用程序的配置,修改配置文件
第一步:設置worker_processes進程的數(shù)量,一般為cpu的核數(shù),或者為auto;
第二步:進行worker_processes與cpu綁核,設置worker_cpu_affinity;
第三步:設置worker進程的最大文件打開數(shù)量worker_rlimit_nofile和worker進程的最大連接數(shù)量worker_connections;并且啟動epoll開發(fā)模型;
第四步:保存退出后nginx -t檢查一下語法,并重啟nginx服務,ps aux|grep nginx去查看nginx的worker進程的pid號
##修改linux系統(tǒng)內(nèi)核能打開的最大文件數(shù)量
在/etc/security/limits.conf中,設置最大的nofile文件數(shù)量
* soft nofile 81920
* hard nofile 81920
改完以后,使用ulimit -a 查看一下
##驗證
cat /usr/local/nginx/logs/nginx.pid
cat /proc/<pid號>/limits 進行查看
二、nginx的狀態(tài)收集模塊
在配置文件中添加模塊開啟
##訪問狀態(tài)統(tǒng)計目錄需要放到server模塊下面,去進行相應站點的統(tǒng)計
server {
...
location /nginx_status {
stub_status on;
access_log off;
}
}
瀏覽器訪問 http://192.168.20.30/nginx_status
Active connections:表示當前的活動連接數(shù),即當前與 Nginx 服務器建立的連接數(shù)。
##也就是netstat -natp|grep nginx|grep -c ESTABLI
server accepts handled requests :表示已經(jīng)處理的連接信息
三個數(shù)字依次表示服務器已接收的連接數(shù);服務器成功處理的連接數(shù);服務器累計處理的總請求數(shù)(在保持連接模式下,請求數(shù)量可能會大于連接數(shù)量)
Reading:表示當前正在從客戶端讀取數(shù)據(jù)的連接數(shù)。
Writing:表示當前正在向客戶端寫入數(shù)據(jù)的連接數(shù)。
Waiting:表示當前空閑并等待請求的連接數(shù)。
三、nginx的用戶身份驗證
比如說上一個實驗中的狀態(tài)統(tǒng)計數(shù)量,這種內(nèi)部數(shù)據(jù),是不希望其他人能看見的,那么可以添加一個用戶認證
第一步:安裝httpd-tools ##yum install -y httpd-tools
第二步:設置密碼 ##htpasswd -c /usr/local/nginx/userlist.db(授權(quán)用戶名) 用戶名
第三步:給該文件加上nginx用戶的權(quán)限和只給讀的權(quán)限
chown nginx /usr/local/nginx/userlist.db
chmod 400 /usr/local/nginx/userlist.db
##注意設置多個用戶的時候,需要去掉-c選項,否則會清空原有的用戶,-c表示新建
##如htpasswd /usr/local/nginx/userlist.db(授權(quán)用戶名) 用戶名2
第四步:修改nginx的配置文件,進行設置,這里是放在nginx的location模塊下
server{
....
location /nginx_status {
stub_status on;
access_log off;
auth_basic "username and password";
auth_basic_user_file /usr/local/nginx/userlist.db;
}
}
需要注意的是,這個auth模塊的配置是由三個地方的
##在 http{} (所有站點生效)
server{} (只在當前站點中生效)
location{} (只對當前站點的指定URL路徑生效) 配置
如果是訪問失敗,會返回狀態(tài)碼401,告知需要用戶身份認證
四、nginx的location中root和alias的區(qū)別
root指定的路徑處理方式是root路徑+location匹配的路徑為最終的URL路徑
alias指定的路徑處理方式是alias路徑直接替換location匹配路徑,也就是alias路徑為URL路徑
結(jié)果驗證
設置兩個location匹配,放在同一個server模塊下:
location /test {
root html;
}
location /cesh {
alias html;
}
五、nginx訪問黑白名單限制
基于上述的進行限制實驗
location /test {
root html;
deny 192.168.20.10;
}
location /cesh {
alias html;
allow 192.168.20.10;
deny all;
}
需要注意的是,這個黑白名單的設置也有三個地方的
http{} (所有站點生效)
server{} (只在當前站點中生效)
location{} (只對當前站點的指定URL路徑生效)
如果是訪問失敗,會返回狀態(tài)碼403,告知需要用戶權(quán)限不足
訪問控制規(guī)則如下:
deny IP/IP 段:拒絕某個 IP 或 IP 段的客戶端訪問。
allow IP/IP 段:允許某個 IP 或 IP 段的客戶端訪問。
規(guī)則從上往下執(zhí)行,如匹配則停止,不再往下匹配。
六、nginx的虛擬主機設置
第一種:基于不同域名的虛擬主機
server {
listen 192.168.20.30:80;
server_name www.accp.com; ##這個站點的域名為 www.accp.com
location / {
root html/accp; ##設置該域名的訪問默認網(wǎng)頁
index index.html index.htm;
}
}
server {
listen 192.168.20.30:80;
server_name www.benet.com;##這個站點的域名為 www.benet.com
location / {
root html/benet;##設置該域名的訪問默認網(wǎng)頁
index index.html index.htm;
}
}
第二種:基于不同ip地址
server {
listen 192.168.20.30:80;##這是ens33網(wǎng)卡
server_name www.accp.com;
location / {
root html/accp;
index index.html index.htm;
}
}
server {
listen 192.168.20.32:80;##這是ens33:0虛擬網(wǎng)卡,這里是用于測試
server_name www.benet.com;
location / {
root html/benet;
index index.html index.htm;
}
}
第三種:基于不同的端口
server {
listen 192.168.20.30:6060;##基于不同端口
server_name www.accp.com;
location / {
root html/accp;
index index.html index.htm;
}
}
server {
listen 192.168.20.30:8080;##基于不同端口
server_name www.benet.com;
location / {
root html/benet;
index index.html index.htm;
}
}
總結(jié):
掌握nginx的最大并發(fā)數(shù)量的設置
掌握nginx的狀態(tài)模塊收集,一般會關閉日志收集
掌握黑白名單和用戶認證的設置,這都是有三個不同的作用域http\server\location
掌握root和alias的區(qū)別,一個是追加location路徑 一個是直接替換location路徑
掌握三種不同方式虛擬主機,基于不同域名 基于不同ip 基于不同端口
原文鏈接:
本文內(nèi)容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。