1.Nginx 配置文件結(jié)構(gòu)
Nginx的核心配置文件默認是放在/usr/local/nginx/conf/nginx.conf
:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
# 訪問目錄/usr/local/nginx/html/index.html文件
location / {
root html;
index index.html index.htm;
}
# 當(dāng)后臺報錯500 502 503 504 時訪問目錄/usr/local/nginx/html/50x.html文件
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
nginx.conf 配置文件中默認有三大塊:全局塊、events塊、http塊。其中http 塊中可以配置多個server塊,每個server塊又可以配置多個location塊。
# 全局塊,主要設(shè)置Nginx服務(wù)器整體運行的配置指令
指令名 指令值;
# events塊,主要設(shè)置,Nginx服務(wù)器與用戶的網(wǎng)絡(luò)連接,這一部分對Nginx服務(wù)器的性能影響較大
events {
指令名 指令值;
}
# http塊,是Nginx服務(wù)器配置中的重要部分,代理、緩存、日志記錄、第三方模塊配置...
http {
指令名 指令值;
# server塊,是Nginx配置和虛擬主機相關(guān)的內(nèi)容
server {
指令名 指令值;
# location塊,基于Nginx服務(wù)器接收請求字符串與location后面的值進行匹配,對特定請求進行處理
location / {
指令名 指令值;
}
}
...
}
2. Nginx 全局塊的指令
01. user 指令
user指令也可以用于指定Nginx服務(wù)器worker進程的運行用戶和用戶組。它的語法如下:
user username [groupname];
其中,username是要指定的用戶名,可以是用戶名或用戶ID。groupname是可選的,用于指定用戶所屬的用戶組。如果未指定用戶組,則默認使用與用戶名相同的用戶組。
① 修改nginx配置文件 nginx.conf,使用user指令指定nginx工作進程的用戶為www:
[root@192 sbin]# cat /usr/local/nginx/conf/nginx.conf
user www;
worker_processes 1;
events {
worker_connections 1024;
}
http {
# ...
}
這將使Nginx工作進程以www用戶的身份運行,并使用與該用戶相同的用戶組。
② 新建用戶 www:
# 1. 測試nginx配置文件語法是否正確
[root@192 sbin]# ./nginx -t
nginx: [emerg] getpwnam("www") failed in /usr/local/nginx/conf/nginx.conf:2
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
# 2. 添加用戶www
[root@192 sbin]# useradd www
[root@192 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
# 3. worker進程的默認用戶名為nobody
[root@192 sbin]# ps -ef | grep nginx
root 128666 1 0 22:29 ? 00:00:00 nginx: master process ./nginx
nobody 128667 128666 0 22:29 ? 00:00:00 nginx: worker process
root 129163 2727 0 22:41 pts/0 00:00:00 grep --color=auto nginx
# 4. 重新加載nginx配置文件
[root@192 sbin]# ./nginx -s reload
# 5. worker進程的用戶名為www
[root@192 sbin]# ps -ef | grep nginx
root 128666 1 0 22:29 ? 00:00:00 nginx: master process ./nginx
www 129167 128666 0 22:41 ? 00:00:00 nginx: worker process
root 129169 2727 0 22:41 pts/0 00:00:00 grep --color=auto nginx
③ 新建 /root/html/index.html 頁面,并修改配置文件 nginx.conf:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
<p><em>I am WWW</em></p>
</body>
</html>
# 訪問root/html/index.html文件
location / {
root /root/html;
index index.html index.htm;
}
④ 重新加載配置文件,并測試訪問:http://192.168.38.33/,頁面會報403拒絕訪問的錯誤,因為當(dāng)前用戶沒有訪問 /root/html 目錄的權(quán)限。
⑤ 將 index.html 文件創(chuàng)建到 /home/www/html/index.html
,并修改配置文件nginx.conf:
[root@192 sbin]# mkdir -p /home/www/html
[root@192 html]# cp -r /root/html/ /home/www/
[root@192 sbin]# ./nginx -s reload
# 訪問 /home/www/html/index.html 文件
location / {
root /home/www/html;
index index.html index.htm;
}
⑥ 重新加載配置文件,并測試訪問:http://192.168.38.33/
# 創(chuàng)建用戶www時,默認會創(chuàng)建/home/www目錄,用戶和用戶組都是www,而/root/html目錄的用戶和用戶組都是root
[root@192 home]# ll
drwxr-x--- 4 www www 89 9月 1 21:06 www
因此使用user指令可以指定啟動運行工作進程的用戶及用戶組,這樣對于系統(tǒng)的權(quán)限訪問控制的更加精細,也更加安全。
02. master_process 指令
nginx 的 master_process 指令用于控制是否啟用 worker 進程。在 nginx 中,master 進程是一個管理進程,它負責(zé)啟動和停止 worker 進程,并且處理一些信號和事件。如果啟用了 master 進程,那么在啟動 nginx 時會先啟動一個 master 進程,然后由 master 進程啟動 worker 進程。如果禁用了 master 進程,那么 nginx 將直接啟動 worker 進程。
master_process 指令的語法如下:
master_process on | off;
當(dāng) master_process 被設(shè)置為 on 時,Nginx 將啟動一個主進程來管理所有的 worker 進程。當(dāng) master_process 被設(shè)置為 off 時,Nginx 將不會啟動主進程,而是直接啟動一個 worker 進程來處理請求。在一些特殊的情況下,比如在容器中運行 Nginx,可能需要將 master_process 設(shè)置為 off。但是在大多數(shù)情況下,我們建議將 master_process 設(shè)置為 on,以便 Nginx 能夠更好地管理 worker 進程。
[root@192 sbin]# ps -ef | grep nginx
www 1447 128666 0 23:53 ? 00:00:00 nginx: worker process
root 1488 2727 0 23:54 pts/0 00:00:00 grep --color=auto nginx
root 128666 1 0 22:29 ? 00:00:00 nginx: master process ./nginx
[root@192 sbin]# vi /usr/local/nginx/conf/nginx.conf
master_process off;
user www;
worker_processes 1;
# ...
[root@192 sbin]# ./nginx -s stop
[root@192 sbin]# ./nginx
[root@192 sbin]# ps -ef | grep nginx
root 53700 1 0 21:19 ? 00:00:00 ./nginx
root 53776 2727 0 21:20 pts/0 00:00:00 grep --color=auto nginx
03. worker_processes 指令
nginx 的 worker_processes 指令用于設(shè)置 Nginx 啟動時的 worker 進程數(shù)量。每個 worker 進程都是一個獨立的進程,用于處理客戶端請求。
worker_processes 指令的語法如下:
worker_processes number;
其中,number 是一個整數(shù),表示要啟動的 worker 進程數(shù)量。通常情況下,建議將 number 設(shè)置為 CPU 核心數(shù)的兩倍,以充分利用服務(wù)器的資源。
例如,如果服務(wù)器有 4 個 CPU 核心,可以將 worker_processes 設(shè)置為 8:
worker_processes 8;
需要注意的是,過多的 worker 進程數(shù)量可能會導(dǎo)致資源競爭和性能下降,因此在設(shè)置 worker_processes 時需要根據(jù)服務(wù)器的硬件配置和實際負載情況進行調(diào)整。
[root@192 sbin]# vi /usr/local/nginx/conf/nginx.conf
# master_process off;
user www;
worker_processes 2;
# ...
[root@192 sbin]# ./nginx -s stop
[root@192 sbin]# ./nginx
[root@192 sbin]# ps -ef | grep nginx
root 53904 1 0 21:23 ? 00:00:00 nginx: master process ./nginx
www 53905 53904 0 21:23 ? 00:00:00 nginx: worker process
www 53906 53904 0 21:23 ? 00:00:00 nginx: worker process
root 53910 2727 0 21:23 pts/0 00:00:00 grep --color=auto nginx
04. deamon 指令
在 Nginx 的全局塊中,可以使用 daemon 指令來設(shè)置 Nginx 是否以守護進程的方式運行。守護進程是一種在后臺運行的進程,它不會占用終端或控制臺,并且可以在系統(tǒng)啟動時自動啟動。
daemon 指令的語法如下:
daemon on|off;
其中,on 表示以守護進程的方式運行 Nginx,off 表示以前臺進程的方式運行 Nginx。默認情況下,Nginx 會以守護進程的方式運行。
如果將 Nginx 設(shè)置為以前臺進程的方式運行,則在終端或控制臺中啟動 Nginx 的命令行將會一直占用該終端或控制臺,直到手動停止 Nginx。因此,通常情況下建議將 Nginx 設(shè)置為以守護進程的方式運行。
05. pid 指令
pid 指令用來配置Nginx當(dāng)前master進程的進程號ID存儲的文件路徑。默認為 /usr/local/nginx/logs/nginx.pid。
06. error_log 指令
Nginx 的全局塊是指在 Nginx 配置文件中,位于 http 塊外的指令。其中,error_log 指令用于設(shè)置 Nginx 的錯誤日志文件路徑和日志級別。
error_log 指令的語法如下:
error_log file [level];
file 參數(shù)指定錯誤日志文件的路徑,可以是絕對路徑或相對路徑。如果路徑以斜杠 / 開頭,則表示絕對路徑;否則表示相對于 Nginx 安裝目錄的路徑。例如:
error_log /var/log/nginx/error.log;
error_log logs/error.log;
level 參數(shù)可選,用于設(shè)置錯誤日志的級別。Nginx 支持以下 8 個日志級別:
- debug:調(diào)試級別,記錄詳細的調(diào)試信息。
- info:信息級別,記錄一般的信息。
- notice:注意級別,記錄需要注意的信息。
- warn:警告級別,記錄警告信息。
- error:錯誤級別,記錄錯誤信息。
- crit:嚴(yán)重級別,記錄嚴(yán)重錯誤信息。
- alert:警報級別,記錄需要立即采取行動的信息。
- emerg:緊急級別,記錄系統(tǒng)崩潰等嚴(yán)重錯誤信息。
如果不指定 level 參數(shù),則默認為 error 級別。
error_log logs/error.log;
error_log logs/error.log notice;
error_log logs/error.log info;
這塊建議大家設(shè)置的時候不要設(shè)置成info以下的等級,因為會帶來大量的磁盤I/O消耗,影響Nginx的性能。
07. include 指令
在 Nginx 的全局塊中,可以使用 include 指令來引入其他配置文件,這些配置文件可以包含其他塊,如 http、server、location 等。這樣可以將配置文件分成多個文件,方便管理和維護。
例如,可以在 Nginx 的全局塊中使用 include 指令來引入一個名為 nginx.conf.d 的目錄下的所有以 .conf 結(jié)尾的文件:
include /etc/nginx/nginx.conf.d/*.conf;
這樣,Nginx 就會讀取該目錄下的所有配置文件,并將它們合并到主配置文件中。這樣做的好處是,可以將不同的配置分散到不同的文件中,便于管理和維護。
3. Nginx events塊的指令
01. accept_mutex 指令
accept_mutex 指令用于控制 worker 進程在處理連接請求時的互斥鎖機制。在 Nginx 中,多個 worker 進程可以同時處理連接請求,但是為了避免競爭條件,每個 worker 進程在處理連接請求時需要獲取一個互斥鎖,以確保同一時刻只有一個 worker 進程在處理連接請求。
accept_mutex on|off;
accept_mutex 指令的作用是控制 worker 進程在獲取互斥鎖時的行為。默認情況下,accept_mutex 指令的值為 on,表示啟用互斥鎖機制。在這種情況下,每個 worker 進程在處理連接請求時都會嘗試獲取互斥鎖,如果獲取失敗,則會進入睡眠狀態(tài),等待其他 worker 進程釋放互斥鎖后再次嘗試獲取。
如果將 accept_mutex 指令的值設(shè)置為 off,則表示禁用互斥鎖機制。在這種情況下,每個 worker 進程在處理連接請求時都不會嘗試獲取互斥鎖,而是直接處理連接請求。這種方式可以提高 worker 進程的并發(fā)處理能力,但是也可能會導(dǎo)致競爭條件的發(fā)生,需要謹慎使用。
總之,accept_mutex 指令的作用是控制 worker 進程在處理連接請求時的互斥鎖機制,可以根據(jù)實際情況進行配置。
02. multi_accept 指令
multi_accept 指令用來設(shè)置是否允許同時接收多個網(wǎng)絡(luò)連接,如果multi_accept被禁止了,nginx一個工作進程只能同時接受一個新的連接。否則,一個工作進程可以同時接受所有的新連接。
multi_accept on|off;
但是,開啟 multi_accept 會增加 CPU 的負載,因為 worker 進程需要在短時間內(nèi)處理大量連接。因此,如果服務(wù)器的 CPU 資源有限,建議不要開啟 multi_accept。另外,如果服務(wù)器的網(wǎng)絡(luò)帶寬較小,也不建議開啟 multi_accept,因為這可能會導(dǎo)致連接的等待時間變長,反而降低并發(fā)處理能力。
03. worker_connections 指令
worker_connections 是一個用于配置 Nginx 服務(wù)器的指令,它用于設(shè)置每個 worker 進程可以同時處理的最大連接數(shù)。
語法如下:
worker_connections number;
默認情況下,worker_connections 的值為 512。這意味著每個 worker 進程最多可以同時處理 512 個連接。如果達到了這個限制,新的連接將被延遲處理,直到有可用的連接槽。
要根據(jù)服務(wù)器的負載和性能需求來調(diào)整 worker_connections 的值。如果服務(wù)器經(jīng)常遇到連接超過默認限制的情況,可以適當(dāng)增加這個值。但是要注意,過高的值可能會導(dǎo)致服務(wù)器資源消耗過大,因此需要根據(jù)實際情況進行調(diào)整。
04. use 指令
use 指令用來設(shè)置Nginx服務(wù)器選擇哪種事件驅(qū)動來處理網(wǎng)絡(luò)消息。
use method;
默認值根據(jù)操作系統(tǒng)來定,此處所選擇事件處理模型是Nginx優(yōu)化部分的一個重要內(nèi)容,method的可選值有select/poll/epoll/kqueue等。
05. events指令配置實例
打開Nginx的配置文件 nginx.conf,添加如下配置:
events{
accept_mutex on;
multi_accept on;
worker_commections 1024;
use epoll;
}
啟動測試
./nginx -t
./nginx -s reload
4. Nginx http塊的指令
01. 定義 MIME Type
我們知道瀏覽器中可以顯示的內(nèi)容有HTML、XML、GIF等種類繁多的文件、媒體等資源,瀏覽器為了區(qū)分這些資源,就需要使用MIME Type。MIME類型是一種標(biāo)準(zhǔn),用于指示在Web上傳輸?shù)奈募念愋?。Nginx作為web服務(wù)器,也需要能夠識別前端請求的資源類型。
在Nginx的配置文件中,默認有兩行配置:
http {
include mime.types;
default_type application/octet-stream;
}
在上面的示例中,include 指令用于包含默認的 MIME Type 定義文件 mime.types,default_type 指令用于設(shè)置默認的 MIME Type。
例如,有些時候請求某些接口的時候需要返回指定的文本字符串或者json字符串,如果邏輯非常簡單或者干脆是固定的字符串,那么可以使用nginx快速實現(xiàn),這樣就不用編寫程序響應(yīng)請求了,可以減少服務(wù)器資源占用并且響應(yīng)性能非常快。
user www;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
# 返回指定的文本字符串
location /get_text {
default_type text/html;
return 200 "This is nginx's text";
}
# 返回json字符串
location /get_json{
default_type application/json;
return 200 '{"name":"TOM","age":18}';
}
location / {
root /home/www/html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
測試訪問:
[root@192 sbin]# curl -i http://192.168.38.33/get_text
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 31 Aug 2023 14:51:04 GMT
Content-Type: text/html
Content-Length: 20
Connection: keep-alive
This is nginx's text
測試訪問:
[root@192 sbin]# curl -i http://192.168.38.33/get_json
HTTP/1.1 200 OK
Server: nginx/1.16.1
Date: Thu, 31 Aug 2023 14:49:51 GMT
Content-Type: application/json
Content-Length: 23
Connection: keep-alive
{"name":"TOM","age":18}
02. access.log 和 log_format 指令
nginx的access.log和error.log是用來記錄服務(wù)器訪問和錯誤信息的日志文件。
access.log 記錄了每個請求到達服務(wù)器的詳細信息,包括請求的時間、客戶端IP地址、請求的URL、HTTP狀態(tài)碼、響應(yīng)大小等。這個日志文件可以用來分析服務(wù)器的訪問情況,比如統(tǒng)計訪問量、分析用戶行為等。
error.log 記錄了服務(wù)器處理請求過程中出現(xiàn)的錯誤信息,比如請求的文件不存在、權(quán)限不足、服務(wù)器內(nèi)部錯誤等。這個日志文件可以幫助管理員及時發(fā)現(xiàn)和解決服務(wù)器的問題,保證服務(wù)器的正常運行。
nginx支持對服務(wù)日志的格式、大小、輸出等進行設(shè)置,需要使用到兩個指令:access_log 指令和 log_format 指令。
① nginx 的 access_log 指令用于配置訪問日志的輸出路徑和格式。位置http
, server
, location
,它的語法如下:
access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]];
其中,path 指定了日志輸出的路徑,可以是一個文件或者是一個 Unix 域套接字。format 指定了日志的格式,可以使用預(yù)定義的格式或者自定義格式。如果指定了 buffer 參數(shù),則表示啟用緩沖,size 指定了緩沖區(qū)的大小。如果指定了 gzip 參數(shù),則表示啟用 gzip 壓縮,level 指定了壓縮級別。如果指定了 flush 參數(shù),則表示定期刷新緩沖區(qū),time 指定了刷新的時間間隔。如果指定了 if 參數(shù),則表示只有滿足條件的請求才會被記錄。
② log_format 指令用于定義日志格式,位置 http
,它的語法如下:
log_format name string ...;
其中,name 是日志格式的名稱,string 是日志格式的字符串表示。string 中可以包含變量,變量以 $ 開頭,例如 r e m o t e a d d r 表示客戶端的 I P 地址, remote_addr 表示客戶端的 IP 地址, remotea?ddr表示客戶端的IP地址,request_time 表示請求處理時間。預(yù)定義的變量可以在 nginx 的官方文檔中找到,也可以自定義變量。定義好日志格式后,可以在 access_log 指令中使用該格式。
③ nginx配置文件中默認的服務(wù)器訪問日志路徑和格式:
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;
server {
# ...
}
}
查看服務(wù)器的訪問日志:
[root@192 sbin]# tail -f /usr/local/nginx/logs/access.log
192.168.38.1 - - [30/Aug/2023:23:23:38 +0800] "GET / HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
192.168.38.1 - - [30/Aug/2023:23:23:38 +0800] "GET /favicon.ico HTTP/1.1" 403 555 "http://192.168.38.33/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
④ nginx配置文件中自定義服務(wù)器訪問日志路徑和格式:
http {
include mime.types;
default_type application/octet-stream;
log_format my_log_format '========>$remote_addr=======>';
access_log logs/access.log my_log_format;
server {
# ...
}
}
查看服務(wù)器的訪問日志:
[root@192 sbin]# tail -f /usr/local/nginx/logs/access.log
========>192.168.38.1=======>
========>192.168.38.1=======>
03. sendfile 指令
用來設(shè)置Nginx服務(wù)器是否使用sendfile()傳輸文件,該屬性可以大大提高Nginx處理靜態(tài)資源的性能
sendfile 指令用于在發(fā)送文件時提高性能。它允許 Nginx 將文件數(shù)據(jù)直接從磁盤發(fā)送到網(wǎng)絡(luò)套接字,而無需將數(shù)據(jù)從內(nèi)核空間復(fù)制到用戶空間,從而減少了 CPU 和內(nèi)存的使用。
在 Nginx 中,sendfile 指令默認是開啟的??梢酝ㄟ^在 nginx.conf 文件中設(shè)置 sendfile 指令來控制其行為。例如,可以使用以下指令來禁用 sendfile:
sendfile off;
需要注意的是,sendfile 指令只在發(fā)送靜態(tài)文件時才有效,對于動態(tài)生成的內(nèi)容,它不起作用。此外,sendfile 指令在某些情況下可能會導(dǎo)致問題,例如在某些操作系統(tǒng)上,它可能會導(dǎo)致文件描述符泄漏。因此,在使用 sendfile 指令時,需要仔細測試和評估其性能和穩(wěn)定性。
04. keepalive_timeout 指令
keepalive_timeout 指令用于設(shè)置客戶端與服務(wù)器之間的 keep-alive 連接超時時間。當(dāng)客戶端與服務(wù)器之間的連接處于 keep-alive 狀態(tài)時,客戶端可以在同一連接上發(fā)送多個請求,而無需每次都建立新的連接。這可以減少連接建立和關(guān)閉的開銷,提高性能。
keepalive_timeout 指令的語法如下:
keepalive_timeout timeout;
其中,timeout 參數(shù)指定 keep-alive 連接的超時時間,單位為秒。如果在指定的時間內(nèi)沒有新的請求到達,連接將被關(guān)閉。
為什么要使用keepalive?
我們都知道HTTP是一種無狀態(tài)協(xié)議,客戶端向服務(wù)端發(fā)送一個TCP請求,服務(wù)端響應(yīng)完畢后斷開連接。如果客戶端向服務(wù)端發(fā)送多個請求,每個請求都需要重新創(chuàng)建一次連接,效率相對來說比較多,使用keepalive模式,可以告訴服務(wù)器端在處理完一個請求后保持這個TCP連接的打開狀態(tài),若接收到來自這個客戶端的其他請求,服務(wù)端就會利用這個未被關(guān)閉的連接,而不需要重新創(chuàng)建一個新連接,提升效率,但是這個連接也不能一直保持,這樣的話,連接如果過多,也會是服務(wù)端的性能下降,這個時候就需要我們進行設(shè)置其的超時時間。
例如,以下配置將 keep-alive 連接的超時時間設(shè)置為 60 秒:
keepalive_timeout 60s;
05. keepalive_requests 指令
keepalive_requests 指令用于設(shè)置在一個 keep-alive 連接中處理的最大請求數(shù)量。當(dāng)客戶端與 Nginx 建立 keep-alive 連接時,可以在同一個連接中發(fā)送多個 HTTP 請求,以減少連接建立和關(guān)閉的開銷,提高性能。
該指令的語法如下:
keepalive_requests number;
其中,number 表示在一個 keep-alive 連接中處理的最大請求數(shù)量。默認值為 100。
當(dāng)一個 keep-alive 連接中的請求數(shù)量達到 keepalive_requests 指定的值時,Nginx 會自動關(guān)閉該連接,以避免連接過長時間占用資源。可以通過增加該值來提高性能,但需要注意,過大的值可能會導(dǎo)致連接過長時間占用資源,從而影響服務(wù)器的性能。
5. Nginx 配置為文件案例解析
需求如下:
- 訪問 http://192.168.200.133:8081/server1/location1,訪問的是:index_sr1_location1.html。
- 訪問 http://192.168.200.133:8081/server1/location2,訪問的是:index_sr1_location2.html。
- 訪問 http://192.168.200.133:8082/server2/location1,訪問的是:index_sr2_location1.html。
- 訪問 http://192.168.200.133:8082/server2/location2,訪問的是:index_sr2_location2.html。
- 如果訪問的資源不存在,返回自定義的404頁面。
- 將/server1和/server2的配置使用不同的配置文件,將文件放到/home/www/conf.d目錄下,然后使用include合并。
- 為/server1和/server2各自創(chuàng)建一個訪問日志文件。
① 創(chuàng)建相關(guān)文件:
[root@192 sbin]# tree /home/www
/home/www
├── conf.d
│ ├── server1.conf
│ └── server2.conf
└── myweb
├── 404.html
├── server1
│ ├── location1
│ │ └── index_sr1_location1.html
│ ├── location2
│ │ └── index_sr1_location2.html
│ └── logs
│ └── access.log
└── server2
├── location1
│ └── index_sr2_location1.html
├── location2
│ └── index_sr2_location2.html
└── logs
└── access.log
[root@192 home]# cat /home/www/myweb/404.html
<h1>This is 404.html in myweb</h1>
[root@192 home]# cat /home/www/myweb/server1/location1/index_sr1_location1.html
<h1>This is index_server1_location1.html</h1>
[root@192 home]# cat /home/www/myweb/server1/location2/index_sr1_location2.html
<h1>This is index_server1_location2.html</h1>
[root@192 home]# cat /home/www/myweb/server2/location1/index_sr2_location1.html
<h1>This is index_server2_location1.html</h1>
[root@192 home]# cat /home/www/myweb/server2/location2/index_sr2_location2.html
<h1> This is index_server2_location2.html</h1>
② 配置文件 /user/local/nginx/nginx.conf:
[root@192 conf]# vi nginx.conf
# 配置允許運行Nginx工作進程的用戶和用戶組
user www;
# 配置運行Nginx進程生成的worker進程數(shù)
worker_processes 2;
# 配置Nginx服務(wù)器運行對錯誤日志存放的路徑
error_log logs/error.log;
# 配置Nginx服務(wù)器允許時記錄Nginx的master進程的PID文件路徑和名稱
pid logs/nginx.pid;
events{
# 設(shè)置Nginx網(wǎng)絡(luò)連接序列化
accept_mutex on;
# 設(shè)置Nginx的worker進程是否可以同時接收多個請求
multi_accept on;
# 設(shè)置Nginx的worker進程最大的連接數(shù)
worker_connections 1024;
# 設(shè)置Nginx使用的事件驅(qū)動模型
use epoll;
}
http{
# 定義MIME-Type
include mime.types;
default_type application/octet-stream;
# 配置允許使用sendfile方式運輸
sendfile on;
# 配置連接超時時間
keepalive_timeout 65;
# 配置請求處理日志格式
log_format server1 '===>server1 access log';
log_format server2 '===>server2 access log';
include /home/www/conf.d/*.conf;
}
[root@192 sbin]# ./nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@192 sbin]# ./nginx -s reload
③ 配置文件 /home/www/conf.d/server1.conf:
[root@192 conf.d]# cat server1.conf
server{
# 配置監(jiān)聽端口和主機名稱
listen 8081;
server_name localhost;
# 配置請求處理日志存放路徑
access_log /home/www/myweb/server1/logs/access.log server1;
# 配置錯誤頁面
error_page 404 /404.html;
# 配置處理/server1/location1請求的location
location /server1/location1{
root /home/www/myweb;
index index_sr1_location1.html;
}
# 配置處理/server1/location2請求的location
location /server1/location2{
root /home/www/myweb;
index index_sr1_location2.html;
}
# 配置錯誤頁面轉(zhuǎn)向
location = /404.html {
root /home/www/myweb;
index 404.html;
}
}
④ 配置文件 /home/www/conf.d/server2.conf:
[root@192 conf.d]# cat server2.conf
server{
# 配置監(jiān)聽端口和主機名稱
listen 8082;
server_name localhost;
# 配置請求處理日志存放路徑
access_log /home/www/myweb/server2/logs/access.log server2;
# 配置錯誤頁面,對404.html做了定向配置
error_page 404 /404.html;
# 配置處理/server1/location1請求的location
location /server2/location1{
root /home/www/myweb;
index index_sr2_location1.html;
}
# 配置處理/server2/location2請求的location
location /server2/location2{
root /home/www/myweb;
index index_sr2_location2.html;
}
# 配置錯誤頁面轉(zhuǎn)向
location = /404.html {
root /home/www/myweb;
index 404.html;
}
}
⑤ 訪問測試:可以看到報錯403 Forbidden
查看 nginx 的錯誤日志,可以看到用戶沒有讀取目錄 /home/www/myweb/server1/location1的權(quán)限
[root@192 sbin]# tail -f /usr/local/nginx/logs/error.log
2023/09/01 22:05:36 [error] 113845#0: *6 open() "/home/www/myweb/server1/location1" failed (13: Permission denied), client: 192.168.38.33, server: localhost, request: "GET /server1/location1 HTTP/1.1", host: "192.168.38.33:8081"
⑥ 查看運行Nginx進程的用戶和訪問目錄的權(quán)限,確認Nginx進程用戶是否具有訪問該目錄的權(quán)限:
[root@192 sbin]# ps aux | grep nginx
root 113496 0.0 0.0 20684 1472 ? Ss 21:55 0:00 nginx: master process ./nginx
www 113844 0.0 0.0 21076 1400 ? S 22:04 0:00 nginx: worker process
www 113845 0.0 0.0 21076 1644 ? S 22:04 0:00 nginx: worker process
root 114050 0.0 0.0 112824 980 pts/0 R+ 22:08 0:00 grep --color=auto nginx
[root@192 sbin]# ll /home/www/myweb/server1/location1
-rwxr-x--- 1 root root 46 9月 1 21:04 index_sr1_location1.html
# 給home/www/目錄下的目錄和文件添加用戶名和用戶組www
[root@192 home]# sudo chgrp -R www /home/www/
[root@192 home]# sudo chown -R www /home/www/
[root@192 home]# cd /home/www/myweb/server2/location1
[root@192 location1]# ll
-rwxrwxrwx 1 www www 46 9月 1 21:05 index_sr2_location1.html
⑦ 運行測試:
[root@192 sbin]# curl http://192.168.38.33:8081/server1/location2/
<h1>This is index_server1_location2.html</h1>
[root@192 sbin]# curl http://192.168.38.33:8081/server1/location1/
<h1>This is index_server1_location1.html</h1>
[root@192 sbin]# curl http://192.168.38.33:8081/server2/location1/
<h1>This is 404.html in myweb</h1>
[root@192 sbin]# curl http://192.168.38.33:8082/server2/location1/
<h1>This is index_server2_location1.html</h1>
[root@192 sbin]# curl http://192.168.38.33:8082/server2/location2/
<h1> This is index_server2_location2.html</h1>
6. Nginx 配置成系統(tǒng)服務(wù)
把Nginx應(yīng)用服務(wù)設(shè)置成為系統(tǒng)服務(wù),方便對Nginx服務(wù)的啟動和停止等相關(guān)操作,具體實現(xiàn)步驟:
① 在/usr/lib/systemd/system
目錄下添加 nginx.service,內(nèi)容如下:
[root@192 conf]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=default.target
② 添加完成后如果權(quán)限有問題需要進行權(quán)限設(shè)置:
[root@192 conf]# chmod 755 /usr/lib/systemd/system/nginx.service
③ 使用系統(tǒng)命令來操作Nginx服務(wù):
# 啟動nginx服務(wù)
[root@192 conf]# systemctl start nginx
Warning: nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
[root@192 conf]# systemctl daemon-reload
[root@192 conf]# systemctl start nginx
# 查看nginx服務(wù)狀態(tài)
[root@192 conf]# systemctl status nginx
● nginx.service - nginx web service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2023-09-01 23:08:54 CST; 2min 24s ago
Docs: http://nginx.org/en/docs/
Main PID: 116647 (nginx)
CGroup: /system.slice/nginx.service
├─116647 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─116648 nginx: worker process
9月 01 23:08:54 192.168.38.33 systemd[1]: Starting nginx - high performance web server...
9月 01 23:08:54 192.168.38.33 systemd[1]: New main PID 7638 does not exist or is a zombie.
9月 01 23:08:54 192.168.38.33 systemd[1]: Started nginx - high performance web server.
# 停止nginx服務(wù)
[root@192 conf]# systemctl stop nginx
[root@192 conf]# systemctl status nginx
● nginx.service - nginx web service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Docs: http://nginx.org/en/docs/
8月 28 22:00:10 192.168.38.33 systemd[1]: Failed to parse PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument
8月 28 22:00:10 192.168.38.33 systemd[1]: Started nginx - web server.
8月 28 22:23:13 192.168.38.33 systemd[1]: nginx.service: main process exited, code=killed, status=9/KILL
8月 28 22:23:13 192.168.38.33 systemd[1]: Unit nginx.service entered failed state.
8月 28 22:23:13 192.168.38.33 systemd[1]: nginx.service failed.
9月 01 23:08:54 192.168.38.33 systemd[1]: Starting nginx - high performance web server...
9月 01 23:08:54 192.168.38.33 systemd[1]: New main PID 7638 does not exist or is a zombie.
9月 01 23:08:54 192.168.38.33 systemd[1]: Started nginx - high performance web server.
9月 01 23:11:27 192.168.38.33 systemd[1]: Stopping nginx web service...
9月 01 23:11:27 192.168.38.33 systemd[1]: Stopped nginx web service.
# 重新啟動nginx服務(wù)
[root@192 conf]# systemctl restart nginx
# 重新加載nginx配置文件
[root@192 conf]# systemctl reload nginx
# 開機啟動nginx服務(wù)
[root@192 conf]# systemctl enable nginx
Created symlink from /etc/systemd/system/default.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
7. Nginx 命令配置到系統(tǒng)環(huán)境
經(jīng)過前面的操作,我們會發(fā)現(xiàn),如果想要啟動、關(guān)閉或重新加載nginx配置文件,都需要先進入到nginx的安裝目錄的sbin目錄,然后使用nginx的二級制可執(zhí)行文件來操作,相對來說操作比較繁瑣,這塊該如何優(yōu)化?
① 修改 /etc/profile 文件,在文件最后一行添加:文章來源:http://www.zghlxwxcb.cn/news/detail-687318.html
export PATH=$PATH:/usr/local/nginx/sbin
[root@192 conf]# vi /etc/profile
[root@192 conf]# source /etc/profile
② 測試在任何目錄下執(zhí)行nginx的相關(guān)命令:文章來源地址http://www.zghlxwxcb.cn/news/detail-687318.html
[root@192 conf]# systemctl status nginx
● nginx.service - nginx web service
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2023-09-01 23:11:41 CST; 7min ago
Docs: http://nginx.org/en/docs/
Main PID: 116813 (nginx)
CGroup: /system.slice/nginx.service
├─116813 nginx: master process /usr/local/nginx/sbin/nginx
├─116826 nginx: worker process
└─116827 nginx: worker process
9月 01 23:11:41 192.168.38.33 systemd[1]: Starting nginx web service...
9月 01 23:11:41 192.168.38.33 nginx[116810]: nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
9月 01 23:11:41 192.168.38.33 nginx[116810]: nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
9月 01 23:11:41 192.168.38.33 systemd[1]: Failed to parse PID from file /usr/local/nginx/logs/nginx.pid: Invalid argument
9月 01 23:11:41 192.168.38.33 systemd[1]: Started nginx web service.
9月 01 23:11:48 192.168.38.33 systemd[1]: Reloading nginx web service.
9月 01 23:11:48 192.168.38.33 systemd[1]: Reloaded nginx web service.
到了這里,關(guān)于分布式 - 服務(wù)器Nginx:基礎(chǔ)系列之Nginx配置文件結(jié)構(gòu)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!