Linux下Nginx基礎(chǔ)應(yīng)用

Nginx簡介:
- Nginx ("engine x") 是一個高性能的HTTP和反向代理服務(wù)器,也是一個IMAP/POP3/SMTP服務(wù)器。其將源代碼以類BSD許可證的形式發(fā)布,因它的穩(wěn)定性、豐富的功能集、示例配置文件和低系統(tǒng)資源的消耗而聞名。
- Nginx是一款輕量級的Web 服務(wù)器/反向代理服務(wù)器及電子郵件(IMAP/POP3)代理服務(wù)器,并在一個BSD-like 協(xié)議下發(fā)行。其特點(diǎn)是占有內(nèi)存少,并發(fā)能力強(qiáng),事實上nginx的并發(fā)能力確實在同類型的網(wǎng)頁服務(wù)器中表現(xiàn)較好。
- Nginx不但是一個優(yōu)秀Web服務(wù)軟件,還具有反向代理負(fù)載均衡功能和緩存服務(wù)功能,與lvs負(fù)載均衡及Haproxy等專業(yè)代理軟件相比,Nginx部署起來更為簡單,方便;在緩存功能方面,它又類似于Squid等專業(yè)的緩存服務(wù)軟件。
Nginx相對于傳統(tǒng)的Apache服務(wù)的優(yōu)缺點(diǎn):
- (1) 作為 Web 服務(wù)器:相比 Apache,Nginx 使用更少的資源,支持更多的并發(fā)連接,體現(xiàn)更高的效率,這點(diǎn)使 Nginx 尤其受到虛擬主機(jī)提供商的歡迎。在高連接并發(fā)的情況下,Nginx是Apache服務(wù)器不錯的替代品;
- (2) Nginx 配置簡潔,Apach復(fù)雜;Nginx靜態(tài)處理性能比Apache高3倍以上;Apache對PHP支持比較簡單,Nginx需要配合其他后端用;Apache的組件比Nginx多;
- (3) apache是同步多進(jìn)程模型,一個連接對應(yīng)一個進(jìn)程;nginx是異步的,多個連接(萬級別)可以對應(yīng)一個進(jìn)程;
- (4) nginx處理靜態(tài)文件好,耗費(fèi)內(nèi)存少,但apache目前也有它的優(yōu)勢,有很多豐富的特性,所以還需要搭配著來;當(dāng)然如果能確定nginx就適合需求,那么使用nginx會是更經(jīng)濟(jì)的方式;
- (5) nginx的負(fù)載能力比apache高很多,而且nginx改完配置能-t測試一下配置有沒有問題,apache重啟的時候發(fā)現(xiàn)配置出錯了,會很崩潰,改的時候都會非常小心翼翼現(xiàn)在看有好多集群站,前端nginx抗并發(fā),后端apache集群,配合的也不錯;
Nginx模塊化體系結(jié)構(gòu):

nginx的模塊根據(jù)其功能基本上可以分為以下幾種類型:
- event module: 搭建了獨(dú)立于操作系統(tǒng)的事件處理機(jī)制的框架,及提供了各具體事件的處理。包括ngx_events_module, ngx_event_core_module和ngx_epoll_module等。nginx具體使用何種事件處理模塊,這依賴于具體的操作系統(tǒng)和編譯選項。
- phase handler: 此類型的模塊也被直接稱為handler模塊。主要負(fù)責(zé)處理客戶端請求并產(chǎn)生待響應(yīng)內(nèi)容,比如ngx_http_static_module模塊,負(fù)責(zé)客戶端的靜態(tài)頁面請求處理并將對應(yīng)的磁盤文件準(zhǔn)備為響應(yīng)內(nèi)容輸出。
- output filter: 也稱為filter模塊,主要是負(fù)責(zé)對輸出的內(nèi)容進(jìn)行處理,可以對輸出進(jìn)行修改。例如,可以實現(xiàn)對輸出的所有html頁面增加預(yù)定義的footbar一類的工作,或者對輸出的圖片的URL進(jìn)行替換之類的工作。
- upstream: upstream模塊實現(xiàn)反向代理的功能,將真正的請求轉(zhuǎn)發(fā)到后端服務(wù)器上,并從后端服務(wù)器上讀取響應(yīng),發(fā)回客戶端。upstream模塊是一種特殊的handler,只不過響應(yīng)內(nèi)容不是真正由自己產(chǎn)生的,而是從后端服務(wù)器上讀取的。
- load-balancer: 負(fù)載均衡模塊,實現(xiàn)特定的算法,在眾多的后端服務(wù)器中,選擇一個服務(wù)器出來作為某個請求的轉(zhuǎn)發(fā)服務(wù)器。
Nginx進(jìn)程流程:

Nginx 服務(wù)器,正常運(yùn)行過程中:
- 多進(jìn)程:一個 Master 進(jìn)程、多個 Worker 進(jìn)程
- Master 進(jìn)程:管理 Worker 進(jìn)程
- 對外接口:接收外部的操作(信號)
- 對內(nèi)轉(zhuǎn)發(fā):根據(jù)外部的操作的不同,通過信號管理 Worker
- 監(jiān)控:監(jiān)控 worker 進(jìn)程的運(yùn)行狀態(tài),worker 進(jìn)程異常終止后,自動重啟 worker 進(jìn)程
- Worker 進(jìn)程:所有 Worker 進(jìn)程都是平等的
- 實際處理:網(wǎng)絡(luò)請求,由 Worker 進(jìn)程處理;
- Worker 進(jìn)程數(shù)量:在 nginx.conf 中配置,一般設(shè)置為核心數(shù),充分利用 CPU 資源,同時,避免進(jìn)程數(shù)量過多,避免進(jìn)程競爭 CPU 資源,增加上下文切換的損耗。
以下是常見http功能模塊:
| http類模塊名 | 模塊功能說明 | |
|---|---|---|
| ngx_http_core_module | http核心模塊,對應(yīng)配置文件中的http段,包含很多指令,如location指令 | |
| ngx_http_access_module | 訪問控制模塊,控制網(wǎng)站用戶對nginx的訪問,對應(yīng)于配置文件中的allow和deny等指令 | |
| ngx_http_auth_basic_module | 通過用戶名和密碼認(rèn)證的訪問控制,如訪問站點(diǎn)時需要數(shù)據(jù)用戶名和密碼,指令包括auth_basic和auth_basic_user_file | |
| ngx_http_charset_module | 設(shè)置網(wǎng)頁顯示字符集。指令之一為charset,如charset utf-8 | |
| ngx_http_fastcgi_module | fastcgi模塊,和動態(tài)應(yīng)用相關(guān)。該模塊下有非常多的子模塊。 | |
| ngx_http_flv_module | 支持flv視頻流的模塊,如邊下邊播 | |
| ngx_http_mp4_module | 同flv模塊 | |
| ngx_http_gzip_module | 壓縮模塊,用來壓縮nginx返回的響應(yīng)報文。一般只壓縮純文本內(nèi)容,因為壓縮比例非常大,而圖片等不會去壓縮 | |
| ngx_http_image_filter_module | 和圖片裁剪、縮略圖相關(guān)模塊,需要安裝gd-devel才能編譯該模塊 | |
| ngx_http_index_module | 定義將要被作為默認(rèn)主頁的文件,對應(yīng)指令為index。"index index.html,index.php" | |
| ngx_http_autoindex_module | 當(dāng)index指令指定的主頁文件不存在時,交給autoindex指令,將自動列出目錄中的文件autoindex {on/off} | |
| ngx_http_log_module | 和訪問日志相關(guān)的模塊,指令包括log_format和access_log | |
| ngx_http_memcached_module | 和memcached相關(guān)的模塊,用于從memcached服務(wù)器中獲取相應(yīng)響應(yīng)數(shù)據(jù) | |
| ngx_http_proxy_module | 和代理相關(guān),允許傳送請求到其它服務(wù)器 | |
| ngx_http_realip_module | 當(dāng)nginx在反向代理的后端提供服務(wù)時,獲取到真正的客戶端地址,否則獲取的是反向代理的IP地址 | |
| ngx_http_referer_module | 實現(xiàn)防盜鏈功能的模塊 | |
| ngx_http_rewrite_module | 和URL地址重寫相關(guān)的模塊,需要安裝pcre-devel才能編譯安裝該模塊 | |
| ngx_http_scgi_module | simple cgi,是cgi的替代品,和fastcgi類似,但更簡單 | |
| ngx_http_ssl_module | 提供ssl功能的模塊,即實現(xiàn)HTTPS | |
| ngx_http_stub_status_module | 獲取nginx運(yùn)行狀態(tài)信息 | |
| ngx_http_upstream | 和負(fù)載均衡相關(guān)模塊 |
Nginx官方站點(diǎn):【官方下載站點(diǎn):源碼包,rpm包,版本號A.B.C,B是偶數(shù)為穩(wěn)定版;奇數(shù)則為開發(fā)版】
第一部分:CentOS7下使用YUM安裝Nginx指定版本。
# 準(zhǔn)備環(huán)境 [root@Server-01 ~]# cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) [root@Server-01 ~]# uname -a Linux Server-01 3.10.0-862.el7.x86_64 #1 SMP Fri Apr 20 16:44:24 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux [root@Server-01 ~]# uname -r 3.10.0-862.el7.x86_64 [root@Server-01 ~]# setenforce 0 [root@Server-01 ~]# systemctl stop firewalld [root@Server-01 ~]# hostname -I 172.16.70.37 # 安裝nginx-1.14.2;目前(2021-07)最新版本為nginx-1.20.1 [root@nginxServer ~]# rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.14.2-1.el7_4.ngx.x86_64.rpm [root@nginxServer ~]# nginx -v nginx version: nginx/1.14.2 [root@nginxServer ~]# rpm -qa | grep nginx nginx-1.14.2-1.el7_4.ngx.x86_64 [root@nginxServer ~]# rpm -ql nginx | less /etc/logrotate.d/nginx /etc/nginx # 相關(guān)配置文件存放目錄 /etc/nginx/conf.d /etc/nginx/conf.d/default.conf /etc/nginx/fastcgi_params /etc/nginx/koi-utf /etc/nginx/koi-win /etc/nginx/mime.types /etc/nginx/modules /etc/nginx/nginx.conf /etc/nginx/scgi_params /etc/nginx/uwsgi_params /etc/nginx/win-utf /etc/sysconfig/nginx /etc/sysconfig/nginx-debug /usr/lib/systemd/system/nginx-debug.service /usr/lib/systemd/system/nginx.service /usr/lib64/nginx /usr/lib64/nginx/modules /usr/libexec/initscripts/legacy-actions/nginx /usr/libexec/initscripts/legacy-actions/nginx/check-reload /usr/libexec/initscripts/legacy-actions/nginx/upgrade /usr/sbin/nginx /usr/sbin/nginx-debug /usr/share/doc/nginx-1.14.2 /usr/share/doc/nginx-1.14.2/COPYRIGHT /usr/share/man/man8/nginx.8.gz /usr/share/nginx /usr/share/nginx/html # 默認(rèn)站點(diǎn)存放目錄 /usr/share/nginx/html/50x.html /usr/share/nginx/html/index.html /var/cache/nginx /var/log/nginx # 日志文件存放目錄 ----------------------------------------------------------------------------- # systemctl命令: 服務(wù)狀態(tài) systemctl status nginx 服務(wù)啟動 systemctl start nginx 服務(wù)停止 systemctl stop nginx 服務(wù)重啟 systemctl restart nginx # nginx命令語法及參數(shù): [root@nginxServer ~]# nginx -h nginx version: nginx/1.14.2 Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives] Options: -?,-h : this help -v : 輸出版本號 -V : 輸出版本號以及編譯選項 -t : 檢查配置文件的語法 -T : 檢查配置文件的語法并輸出配置的內(nèi)容 -q : 靜默模式,不輸出任何信息 -s signal : 向主進(jìn)程發(fā)送信號:stop, quit, reopen, reload -p prefix : 設(shè)置nginx的basedir(默認(rèn)為編譯時的prefix) -c filename : 指定配置文件 -g directives : 提前設(shè)置全局指令 ------------------------------------------------------------------------------- # 啟動,并設(shè)置開機(jī)啟動 [root@nginxServer ~]# systemctl start nginx && systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service. [root@nginxServer ~]# ps -ef |grep nginx root 1466 1 0 16:54 ? 00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf nginx 1467 1466 0 16:54 ? 00:00:00 nginx: worker process root 1486 1178 0 16:54 pts/0 00:00:00 grep --color=auto nginx [root@nginxServer ~]# netstat -nutlp | grep nginx tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1466/nginx: master
- Web檢測測試效果。

Nginx默認(rèn)配置文件解析:
[root@nginxServer ~]# cat /etc/nginx/nginx.conf
user nginx; # 配置用戶或組
worker_processes 1; # worker允許生成的進(jìn)程數(shù)
error_log /var/log/nginx/error.log warn; # 制定日志路徑,級別。這個設(shè)置可以放入全局塊,http塊,server塊,級別以此為:debug|info|notice|warn|error|crit|alert|emerg
pid /var/run/nginx.pid; # 指定nginx進(jìn)程運(yùn)行文件存放位置
events { # 事件區(qū)塊
worker_connections 1024; # 每個worker進(jìn)程支持的最大連接數(shù)
}
http { # http區(qū)塊
include /etc/nginx/mime.types; # 支持的媒體類型庫文件包含
default_type application/octet-stream; # 默認(rèn)媒體類型
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 /var/log/nginx/access.log main; # 默認(rèn)log格式
sendfile on; # 允許sendfile方式傳輸文件,可以在http塊,server塊,location塊
#tcp_nopush on;
keepalive_timeout 65; # 連接超時時間,可以在http塊,server塊,location塊
#gzip on;
include /etc/nginx/conf.d/*.conf; # 生效包含自定義配置
}
[root@nginxServer nginx]# cat conf.d/default.conf
server { # 網(wǎng)站配置區(qū)域
listen 80; # 提供服務(wù)的端口,默認(rèn)80
server_name localhost; # 提供服務(wù)的域名主機(jī)名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / { # location區(qū)塊開始
root /usr/share/nginx/html; # 站點(diǎn)的根目錄(相對與nginx安裝目錄)
index index.html index.htm; # 默認(rèn)的首頁文件,多個用空格分開
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; # 出現(xiàn)對應(yīng)的http狀態(tài)碼時,使用50x.html回應(yīng)客戶
location = /50x.html { # location區(qū)塊開始,訪問50x.html
root /usr/share/nginx/html; # 指定對應(yīng)的站點(diǎn)目錄為html
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
-----------------------------------------------------------
# 整個nginx配置文件的核心框架如下
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
第二部分:設(shè)置虛擬主機(jī)。
- nginx使用server容器定義一個虛擬主機(jī)。在nginx中,沒有嚴(yán)格區(qū)分基于IP和基于名稱的虛擬主機(jī),它們通過listen指令和server_name指令結(jié)合起來形成不同的虛擬主機(jī)。
# 配置文件目錄結(jié)構(gòu)
[root@nginxServer ~]# tree /etc/nginx/
/etc/nginx/
├── conf.d
│ ├── default.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules -> ../../usr/lib64/nginx/modules
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf
2 directories, 10 files
說明:其中".default"后綴的是對應(yīng)前綴配置文件的備份配置文件,".params"是對應(yīng)前綴的參數(shù)文件。
# 創(chuàng)建站點(diǎn)目錄及站點(diǎn)內(nèi)容
[root@nginxServer ~]# for i in {1..3}; do mkdir -p /data/web/nginx/www$i; echo "Mysite-$i www$i.nginx.com" > /data/web/nginx/www$i/index.html; done
[root@nginxServer ~]# tree /data/web/nginx/
/data/web/nginx/
├── www1
│ └── index.html
├── www2
│ └── index.html
└── www3
└── index.html
3 directories, 3 files
[root@nginxServer ~]# cat /data/web/nginx/www*/*
Mysite-1 www1.nginx.com
Mysite-2 www2.nginx.com
Mysite-3 www3.nginx.com
[root@nginxServer ~]# cd /etc/nginx/conf.d/
[root@nginxServer conf.d]# mv default.conf default.conf_bak
# 新增虛擬主機(jī)配置文件
[root@nginxServer conf.d]# vim vhosts.conf
# 基于端口port
server {
listen 8081;
server_name www1.nginx.com;
location / {
root /data/web/nginx/www1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www1;
}
}
server {
listen 8082;
server_name www2.nginx.com;
location / {
root /data/web/nginx/www2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www2;
}
}
# 基于域名FQDN
server {
listen 80;
server_name www1.nginx.com;
location / {
root /data/web/nginx/www1;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www1;
}
}
server {
listen 80;
server_name www3.nginx.com;
location / {
root /data/web/nginx/www3;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www3;
}
}
#基于IP地址
server {
listen 80;
server_name 172.16.70.37;
location / {
root /data/web/nginx/www2;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www2;
}
}
server {
listen 80;
server_name 172.16.70.38;
location / {
root /data/web/nginx/www3;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www3;
}
}
# 新增nginx狀態(tài)信息配置文件
[root@nginxServer conf.d]# vim status.conf
# State information
server{
listen 80;
server_name status.nginx.com;
location / {
stub_status on;
access_log off;
}
}
# 基于域名,添加地址解析
[root@nginxServer conf.d]# echo '172.16.70.37 www1.nginx.com www2.nginx.com www3.nginx.com status.nginx.com' >> /etc/hosts
# 基于IP,添加IP地址
[root@nginxServer conf.d]# ip addr add 172.16.70.38/24 dev ens33
[root@nginxServer conf.d]# ip addr | grep ens33
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 172.16.70.37/24 brd 172.16.70.255 scope global noprefixroute dynamic ens33
inet 172.16.70.38/24 scope global secondary ens33
[root@nginxServer conf.d]# ping 172.16.70.38 -c2
PING 172.16.70.38 (172.16.70.38) 56(84) bytes of data.
64 bytes from 172.16.70.38: icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from 172.16.70.38: icmp_seq=2 ttl=64 time=0.059 ms
--- 172.16.70.38 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.015/0.037/0.059/0.022 ms
[root@nginxServer conf.d]# nginx -t # 檢測配置文件語法
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginxServer conf.d]# nginx -s reload # 重載nginx
[root@nginxServer conf.d]# netstat -ntupl | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1466/nginx: master
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1466/nginx: master
tcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 1466/nginx: master
# 檢測測試結(jié)果
[root@nginxServer conf.d]# hostname -I
172.16.70.37
# 基于端口
[root@nginxServer conf.d]# curl 172.16.70.37:8081
Mysite-1 www1.nginx.com
[root@nginxServer conf.d]# curl 172.16.70.37:8082
Mysite-2 www2.nginx.com
# 基于域名
[root@nginxServer conf.d]# curl www1.nginx.com
Mysite-1 www1.nginx.com
[root@nginxServer conf.d]# curl www3.nginx.com
Mysite-3 www3.nginx.com
# 基于IP
[root@nginxServer conf.d]# curl 172.16.70.37
Mysite-2 www2.nginx.com
[root@nginxServer conf.d]# curl 172.16.70.38
Mysite-3 www3.nginx.com
# 顯示狀態(tài)結(jié)果
[root@nginxServer conf.d]# curl status.nginx.com
Active connections: 10
server accepts handled requests
105 105 116
Reading: 10 Writing: 1 Waiting: 11
說明:
Active connections 當(dāng)前處于活動狀態(tài)的客戶端連接數(shù),包括正處于等待狀態(tài)的連接。
accepts 從服務(wù)啟動開始到現(xiàn)在已經(jīng)接收進(jìn)來的總的客戶端連接數(shù)。
handled 從服務(wù)啟動以來已經(jīng)處理過的連接數(shù),一般handled的值和accepts的值相等,除非作出了連接數(shù)限定。
requests 從服務(wù)啟動以來總的客戶端請求數(shù),一個連接可以有多個請求。(平均每次握手處理了1.3個數(shù)據(jù)請求)
Reading nginx正在讀取請求首部的數(shù)量,即正在從socket recv buffer中讀取數(shù)據(jù)的數(shù)量。
Writing nginx正在將響應(yīng)數(shù)據(jù)寫入socket send buffer以返回給客戶端的連接數(shù)量。
Waiting 等待空閑客戶端發(fā)起請求的客戶端數(shù)量,包括長連接狀態(tài)的連接以及已接入但socket recv buffer還未產(chǎn)生可讀事件的連接,這個值等于active - (reading+writing)
第三部分:Nginx Web認(rèn)證
- 基于用戶認(rèn)證。即需要輸入用戶名和密碼才能訪問站點(diǎn)的功能為web身份認(rèn)證功能。nginx中由ngx_http_auth_basic_module模塊提供該功能。指令包括auth_basic和auth_basic_user_file。這兩個指令可以在http根段、server段、location段使用。
[root@nginxServer conf.d]# pwd
/etc/nginx/conf.d
[root@nginxServer conf.d]# vim vhosts.conf
server {
listen 80;
server_name www1.nginx.com;
location / {
root /data/web/nginx/www1;
index index.html index.htm;
auth_basic "Auth your name";
auth_basic_user_file /etc/nginx/.ngxpasswd;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /data/web/nginx/www1;
}
}
# htpasswd命令由httpd-tools所提供
[root@nginxServer conf.d]# yum install -y httpd-tools
[root@nginxServer conf.d]# htpasswd -c -b -m /etc/nginx/.ngxpasswd user1 user1
Adding password for user user1
[root@nginxServer conf.d]# htpasswd -b -m /etc/nginx/.ngxpasswd user2 user2
Adding password for user user2
說明:
-c表示創(chuàng)建密碼文件,只能為第一個用戶使用該選項,否則后面使用會覆蓋前面已經(jīng)創(chuàng)建過的
-b選項是表示batch模式,不用交互輸入密碼
-m表示強(qiáng)制使用md5
[root@nginxServer conf.d]# cat /etc/nginx/.ngxpasswd
user1:$apr1$MaM8PAS.$FjeZesqyESHjUyUarao.q/
user2:$apr1$3Tvnt.Kp$ysOph6mgMyTHaqgLy6fsH1
[root@nginxServer conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@nginxServer conf.d]# nginx -s reload
- 檢測測試結(jié)果。
WIN10的hosts文件添加解析:將'172.16.70.37 www1.nginx.com www2.nginx.com www3.nginx.com'添加到C:\Windows\System32\drivers\etc\hosts


Nginx訪問日志輪詢切割:
- 默認(rèn)情況下Nginx會把所有的訪問日志生成到一個指定的訪問日志文件access.log里,但這樣一來,時間長了就會導(dǎo)致日志個頭很大,不利于日志的分析和處理,因此,有必要對Nginx日志,按天或按小時進(jìn)行切割,使其分成不同的文件保存。
[root@nginxServer nginx]# pwd
/etc/nginx
[root@nginxServer nginx]# cat cut_nginx_log.sh
#!/bin/bash
#日志切割腳本可掛定時任務(wù),每天00點(diǎn)整執(zhí)行
Dateformat=`date +%Y%m%d`
Nginxlogdir="/var/log/nginx"
Logname="access"
[ -d $Nginxlogdir ] && cd $Nginxlogdir || exit 1
[ -f ${Logname}.log ] || exit 1
/usr/bin/mv ${Logname}.log ${Logname}.log_${Dateformat}
/usr/sbin/nginx -s reload
# 新增計劃任務(wù)
[root@nginxServer nginx]# cat /etc/cron.d/cut_nginx_log.cron
#cut nginx access log
00 00 * * * root /usr/bin/bash /etc/nginx/cut_nginx_log.sh >/dev/null 2>&
********** 如果您認(rèn)為這篇文章還不錯或者有所收獲,請點(diǎn)擊右下角的【推薦】/【贊助】按鈕,因為您的支持是我繼續(xù)創(chuàng)作分享的最大動力! **********
作者:講文張字
出處:http://www.rzrgm.cn/zhangwencheng
版權(quán):本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出 原文鏈接
出處:http://www.rzrgm.cn/zhangwencheng
版權(quán):本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出 原文鏈接
浙公網(wǎng)安備 33010602011771號