目錄
conf
nginx.conf?
worker_processes
worker_connections
include mime.types;
default_type application/octet-stream;
sendfile
sendfile off;?
sendfile on;
keepalive_timeout ?65;
server {}
location / {}
error_page ? 500 502 503 504 ?/50x.html;
html
index.html
50x.html
logs
access.log
error.log
nginx.pid?
sbin
Nginx?
Nginx的目錄結(jié)構(gòu):
[root@van ~]# tree /usr/local/nginx/
/usr/local/nginx/
├── client_body_temp
├── conf
│?? ├── fastcgi.conf
│?? ├── fastcgi.conf.default
│?? ├── fastcgi_params
│?? ├── fastcgi_params.default
│?? ├── koi-utf
│?? ├── koi-win
│?? ├── mime.types
│?? ├── mime.types.default
│?? ├── nginx.conf
│?? ├── nginx.conf.default
│?? ├── scgi_params
│?? ├── scgi_params.default
│?? ├── uwsgi_params
│?? ├── uwsgi_params.default
│?? └── win-utf
├── fastcgi_temp
├── html
│?? ├── 50x.html
│?? ├── index.html
│?? └── index.php
├── logs
│?? ├── access.log
│?? ├── error.log
│?? └── nginx.pid
├── proxy_temp
├── sbin
│?? └── nginx
├── scgi_temp
└── uwsgi_temp
這里方便直觀講解,所以直接在Xftp中進(jìn)行演示。
目錄如下,可能不同 以自己的安裝路徑為準(zhǔn):
/usr/local/nginx
這里可以看到有5個(gè)以 _temp 結(jié)尾的目錄,這些都是臨時(shí)形成的,所以就不介紹了。
conf
顧名思義,這里是用來存放配置文件的目錄,這里最為主要的是 nginx.conf 這個(gè)文件,這個(gè)是Nginx的主配置文件。若以后需要手動(dòng)添加其它的配置文件,還請(qǐng)一并放在此目錄下。
nginx.conf?
主要來看一下這個(gè) nginx.conf 文件(這里直接在Xftp里右鍵查看了,方便):
內(nèi)容大致如下(因?yàn)槲业挠凶鲞^一定修改,可能會(huì)與你們的有出入,不礙事):
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm index.php index.java;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root 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 /usr/local/nginx/html$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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
能看出來格式很想JSON串。?
worker_processes
默認(rèn)為1,表示開啟一個(gè)業(yè)務(wù)進(jìn)程。當(dāng)然你也可以把他改成10,但需要注意的是這個(gè)東西跟cpu的內(nèi)核數(shù)量是有掛鉤的。如果說咱們的cpu只有2核,那在這里寫個(gè)3那可能就沒啥意義了。如果把一個(gè)cpu內(nèi)核綁定到多個(gè)進(jìn)程上的話,按照時(shí)間片輪轉(zhuǎn)機(jī)制來說,這樣只會(huì)降低cpu的利用率。打個(gè)比方:我讓A幫我去買份飯回來,但是緊接著B和C也想讓A幫他們帶飯,這樣就延長的A幫我?guī)э埢貋淼臅r(shí)間,那對(duì)于 我 來說A的工作效率其實(shí)是降低了的。
worker_connections
單個(gè)進(jìn)程可接受連接數(shù),默認(rèn)為1024。
include mime.types;
引入http mime類型
這里的 include 命令會(huì)把另外一個(gè)配置文件引用到我們的當(dāng)前的這個(gè)配置文件當(dāng)中。?這里的?mime.types 配置文件是用來申明所返回或發(fā)送文件的類型。咱們的瀏覽器其實(shí)不能根據(jù)文件的后綴名來判斷文件的格式/類型,它必須由服務(wù)器發(fā)送的http頭里所標(biāo)注的信息來展示文件的類型。咱們可以去這個(gè) mime.types 里面看一下:
types {
text/html html htm shtml;
text/css css;
text/xml xml;
application/javascript js;
text/plain txt;
image/png png;
application/java-archive jar war ear;
application/json json;
application/msword doc;
application/pdf pdf;
application/zip zip;
application/octet-stream bin exe dll;
application/octet-stream deb;
application/octet-stream dmg;
application/octet-stream iso img;
application/octet-stream msi msp msm;
audio/midi mid midi kar;
audio/mpeg mp3;
audio/ogg ogg;
audio/x-m4a m4a;
audio/x-realaudio ra;
video/mp2t ts;
video/mp4 mp4;
video/x-flv flv;
video/x-m4v m4v;
video/x-mng mng;
video/x-ms-asf asx asf;
video/x-ms-wmv wmv;
video/x-msvideo avi;
}
復(fù)制了一部分過來,原文件的內(nèi)容還是不少的??梢钥吹竭@里的格式有點(diǎn)像kv鍵值對(duì)。比如說這里18行的 exe 文件,他所得到的是一個(gè)叫做 application/octet-stream 的類型,意思是以數(shù)據(jù)流的方式讓瀏覽器去加載,并詢問用戶是否要進(jìn)行下載操作。那比如說咱們現(xiàn)在有個(gè)一個(gè)非常奇怪的格式,這個(gè)格式是平常根本見不到的格式,那我們就可以將其寫在此文件中,讓他按照你想要的格式去讓瀏覽器解讀。
default_type application/octet-stream;
如果mime類型沒匹配上,默認(rèn)使用二進(jìn)制流的方式傳輸。
sendfile
使用linux的 sendfile(socket, file, len) 高效網(wǎng)絡(luò)傳輸,也就是數(shù)據(jù)0拷貝。 未開啟sendfile。再發(fā)個(gè)比方:咱們現(xiàn)在有兩臺(tái)電腦,我們需要從PC1拿點(diǎn)東西去PC2,那我們就得先拿U盤去PC1里面拷貝,然后再用U盤拷貝到PC2中,而如果開啟了這個(gè)sendfile,那就相當(dāng)于直接隔空傳過去,省去了拷貝的過程。
sendfile off;?
直接上圖來講:
咱們的Nginx是假設(shè)在CentOS操作系統(tǒng)上的,當(dāng)服務(wù)過來的時(shí)候是通過CentOS的網(wǎng)絡(luò)接口然后轉(zhuǎn)發(fā)給Nginx。Nginx拿到信息后會(huì)根據(jù)其內(nèi)容去硬盤(SSD)中找到那個(gè)目標(biāo)文件,然后Nginx會(huì)把這個(gè)文件完整的讀一遍,讀到自己的內(nèi)存中再交給應(yīng)用程序,應(yīng)用程序也把拿到的文件完整的讀進(jìn)自己的內(nèi)存,再轉(zhuǎn)發(fā)到CentOS的網(wǎng)絡(luò)接口(當(dāng)然這里面還有DMA [ Direct Memory Access直接訪問內(nèi)存 ] 的調(diào)度,網(wǎng)絡(luò)接口的緩存和內(nèi)核的緩存)網(wǎng)絡(luò)接口再把其發(fā)送出去。這里的層層緩存就相當(dāng)于上面所說的層層復(fù)制。
sendfile on;
這里通過Nginx會(huì)給內(nèi)核發(fā)送一個(gè)信號(hào),此時(shí)的應(yīng)用程序不會(huì)再去把文件讀取進(jìn)自己的內(nèi)存了,而是直接發(fā)送這個(gè) sendfile() 信號(hào)(當(dāng)然這其實(shí)是一個(gè)方法,里面會(huì)寫明需要傳出的文件和socket)給CentOS的網(wǎng)絡(luò)接口,網(wǎng)絡(luò)接口在收到信號(hào)后回去找到這個(gè)目標(biāo)文件,然后發(fā)送出去。
keepalive_timeout ?65;
連接的超時(shí)時(shí)間限制。
server {}
這里可以把一個(gè)server看成是一個(gè)主機(jī),所以一個(gè)Nginx可以擁有多臺(tái)這樣的主機(jī),它們通過不同的端口號(hào)?listen 來區(qū)分。當(dāng)然端口號(hào)也可以相同,那樣的話就需要通過 server_name 來分區(qū)了。開啟多個(gè)主機(jī)的方式也叫作虛擬主機(jī)(vhost)。
location / {}
咱們這回先舉例子。 http://abc.com/xxx/index.html 這個(gè)叫做url,沒啥問題吧。而這其中的 /xxx/index.html 叫做uri。當(dāng)這個(gè)uri被匹配上的時(shí)候,那么就會(huì)進(jìn)入到這個(gè) location / {} 中,去找里面的root目錄。這里的?root ? html; 中的html其實(shí)是個(gè)相對(duì)路徑,就是咱們下一小節(jié)提到的那個(gè)html目錄。
error_page ? 500 502 503 504 ?/50x.html;
簡單的來說,如果出現(xiàn)了這四個(gè)報(bào)錯(cuò),那么就會(huì)進(jìn)入到這個(gè) 50x.html 中,這個(gè)文件也會(huì)在下一小節(jié)中提到。
html
這里的 index.php 直接無視,這是我自己加進(jìn)去的:
index.html
能看到這里放著兩個(gè)html文件,其中的 index.html 為Nginx的默認(rèn)頁,可以看看里面是啥:
<!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 Tomcat!</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 >nginx.org</a>.<br/>
Commercial support is available at
<a >nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
可以看到我把這里第13行的Nginx修改成了Tomcat,咱們可以去測試看一下。?
?很簡單的html語言,咱們繼續(xù)看第二個(gè)文件?50x.html?
50x.html
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
這里就是Nginx的一個(gè)默認(rèn)的報(bào)錯(cuò)頁面,也沒啥好講的。
logs
這里有兩個(gè)log日志和一個(gè)pid文件。可以看到這個(gè)?access.log?真的是相當(dāng)?shù)拇?!足足?8KB(我這里才用了不到兩天)
access.log
訪問日志
192.168.150.11 - - [24/Aug/2022:11:06:06 +0800] "GET /img/%E8%A1%A5%E8%B4%B4.png HTTP/1.1" 304 0 "http://192.168.150.50/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
192.168.150.11 - - [24/Aug/2022:11:06:06 +0800] "GET /img/%E5%A4%87%E6%B3%A8.png HTTP/1.1" 304 0 "http://192.168.150.50/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36"
這里的內(nèi)容實(shí)在是太多啦,所以只復(fù)制了兩行過來。?
其實(shí)也就是在用戶訪問咱們的時(shí)候,會(huì)把時(shí)間、地點(diǎn)、人物、人物所訪問的文件、人物所帶的參數(shù)等信息記錄在此。所以這個(gè)文件通常都是非常的大(但是它并不會(huì)影響到Nginx的性能),為了防止數(shù)據(jù)寫不進(jìn)來而導(dǎo)致服務(wù)出現(xiàn)問題,所以在配置文件中會(huì)嚴(yán)格的把控 access.log 文件的大小,一旦它達(dá)到了那個(gè)指定的大小,那么系統(tǒng)就是重新新建一個(gè)文件用來繼續(xù)寫入訪問信息日志。
error.log
隨便復(fù)制幾行過來看看:?
2022/08/24 11:05:47 [notice] 53322#0: signal process started
2022/08/24 11:28:44 [notice] 54846#0: signal process started
2022/08/24 18:23:11 [notice] 14640#0: signal process started
2022/08/25 15:24:22 [error] 855#0: *1 open() "/usr/local/nginx/html/favicon.ico" failed (2: No such file or directory), client: 192.168.150.11, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.150.50", referrer: "http://192.168.150.50/"
這個(gè)日志就比上面的 access.log 要小很多了。說白了就是用戶在訪問服務(wù)時(shí)所遇到的錯(cuò)誤,都會(huì)被記錄在這個(gè) error.log 里面。
nginx.pid?
能看出來這個(gè)文件跟上面的不一樣啊,后綴為pid而不是log。這個(gè)文件是用來記錄咱們Nginx主進(jìn)程的ID號(hào)的。
854
內(nèi)容也很簡單,只有這一個(gè)數(shù)字。當(dāng)然你們的ID跟我的不太可能會(huì)一致,咱們可以去系統(tǒng)里看看:
ps -ef | grep nginx
可以看到master主進(jìn)程的ID與文件中內(nèi)容一致。?
sbin
文章來源:http://www.zghlxwxcb.cn/news/detail-467003.html
Nginx?
下面只有一個(gè)Nginx的主程序,也沒啥好說的。?文章來源地址http://www.zghlxwxcb.cn/news/detail-467003.html
到了這里,關(guān)于Nginx - 目錄結(jié)構(gòu)與配置文件詳解的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!