• Nginx目录结构与配置文件详解


    目录

    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的目录结构:

    1. [root@van ~]# tree /usr/local/nginx/
    2. /usr/local/nginx/
    3. ├── client_body_temp
    4. ├── conf
    5. │   ├── fastcgi.conf
    6. │   ├── fastcgi.conf.default
    7. │   ├── fastcgi_params
    8. │   ├── fastcgi_params.default
    9. │   ├── koi-utf
    10. │   ├── koi-win
    11. │   ├── mime.types
    12. │   ├── mime.types.default
    13. │   ├── nginx.conf
    14. │   ├── nginx.conf.default
    15. │   ├── scgi_params
    16. │   ├── scgi_params.default
    17. │   ├── uwsgi_params
    18. │   ├── uwsgi_params.default
    19. │   └── win-utf
    20. ├── fastcgi_temp
    21. ├── html
    22. │   ├── 50x.html
    23. │   ├── index.html
    24. │   └── index.php
    25. ├── logs
    26. │   ├── access.log
    27. │   ├── error.log
    28. │   └── nginx.pid
    29. ├── proxy_temp
    30. ├── sbin
    31. │   └── nginx
    32. ├── scgi_temp
    33. └── uwsgi_temp

    这里方便直观讲解,所以直接在Xftp中进行演示。

    目录如下,可能不同 以自己的安装路径为准:

    /usr/local/nginx
    

    这里可以看到有5个以 _temp 结尾的目录,这些都是临时形成的,所以就不介绍了。


    conf

    顾名思义,这里是用来存放配置文件的目录,这里最为主要的是 nginx.conf 这个文件,这个是Nginx的主配置文件。若以后需要手动添加其它的配置文件,还请一并放在此目录下。

    nginx.conf 

    主要来看一下这个 nginx.conf 文件(这里直接在Xftp里右键查看了,方便):

    内容大致如下(因为我的有做过一定修改,可能会与你们的有出入,不碍事):

    1. #user nobody;
    2. worker_processes 1;
    3. #error_log logs/error.log;
    4. #error_log logs/error.log notice;
    5. #error_log logs/error.log info;
    6. #pid logs/nginx.pid;
    7. events {
    8. worker_connections 1024;
    9. }
    10. http {
    11. include mime.types;
    12. default_type application/octet-stream;
    13. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
    14. # '$status $body_bytes_sent "$http_referer" '
    15. # '"$http_user_agent" "$http_x_forwarded_for"';
    16. #access_log logs/access.log main;
    17. sendfile on;
    18. #tcp_nopush on;
    19. #keepalive_timeout 0;
    20. keepalive_timeout 65;
    21. #gzip on;
    22. server {
    23. listen 80;
    24. server_name localhost;
    25. #charset koi8-r;
    26. #access_log logs/host.access.log main;
    27. location / {
    28. root html;
    29. index index.html index.htm index.php index.java;
    30. }
    31. #error_page 404 /404.html;
    32. # redirect server error pages to the static page /50x.html
    33. #
    34. error_page 500 502 503 504 /50x.html;
    35. location = /50x.html {
    36. root html;
    37. }
    38. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    39. #
    40. #location ~ \.php$ {
    41. # proxy_pass http://127.0.0.1;
    42. #}
    43. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    44. #
    45. #location ~ \.php$ {
    46. # root html;
    47. # fastcgi_pass 127.0.0.1:9000;
    48. # fastcgi_index index.php;
    49. # fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
    50. # include fastcgi_params;
    51. #}
    52. # deny access to .htaccess files, if Apache's document root
    53. # concurs with nginx's one
    54. #
    55. #location ~ /\.ht {
    56. # deny all;
    57. #}
    58. }
    59. # another virtual host using mix of IP-, name-, and port-based configuration
    60. #
    61. #server {
    62. # listen 8000;
    63. # listen somename:8080;
    64. # server_name somename alias another.alias;
    65. # location / {
    66. # root html;
    67. # index index.html index.htm;
    68. # }
    69. #}
    70. # HTTPS server
    71. #
    72. #server {
    73. # listen 443 ssl;
    74. # server_name localhost;
    75. # ssl_certificate cert.pem;
    76. # ssl_certificate_key cert.key;
    77. # ssl_session_cache shared:SSL:1m;
    78. # ssl_session_timeout 5m;
    79. # ssl_ciphers HIGH:!aNULL:!MD5;
    80. # ssl_prefer_server_ciphers on;
    81. # location / {
    82. # root html;
    83. # index index.html index.htm;
    84. # }
    85. #}
    86. }

    能看出来格式很想JSON串。 

    worker_processes

    默认为1,表示开启一个业务进程。当然你也可以把他改成10,但需要注意的是这个东西跟cpu的内核数量是有挂钩的。如果说咱们的cpu只有2核,那在这里写个3那可能就没啥意义了。如果把一个cpu内核绑定到多个进程上的话,按照时间片轮转机制来说,这样只会降低cpu的利用率。打个比方:我让A帮我去买份饭回来,但是紧接着B和C也想让A帮他们带饭,这样就延长的A帮我带饭回来的时间,那对于 我 来说A的工作效率其实是降低了的。

    worker_connections

    单个进程可接受连接数,默认为1024。

    include mime.types;

    引入http mime类型

    这里的 include 命令会把另外一个配置文件引用到我们的当前的这个配置文件当中。 这里的 mime.types 配置文件是用来申明所返回或发送文件的类型。咱们的浏览器其实不能根据文件的后缀名来判断文件的格式/类型,它必须由服务器发送的http头里所标注的信息来展示文件的类型。咱们可以去这个 mime.types 里面看一下:

    1. types {
    2. text/html html htm shtml;
    3. text/css css;
    4. text/xml xml;
    5. application/javascript js;
    6. text/plain txt;
    7. image/png png;
    8. application/java-archive jar war ear;
    9. application/json json;
    10. application/msword doc;
    11. application/pdf pdf;
    12. application/zip zip;
    13. application/octet-stream bin exe dll;
    14. application/octet-stream deb;
    15. application/octet-stream dmg;
    16. application/octet-stream iso img;
    17. application/octet-stream msi msp msm;
    18. audio/midi mid midi kar;
    19. audio/mpeg mp3;
    20. audio/ogg ogg;
    21. audio/x-m4a m4a;
    22. audio/x-realaudio ra;
    23. video/mp2t ts;
    24. video/mp4 mp4;
    25. video/x-flv flv;
    26. video/x-m4v m4v;
    27. video/x-mng mng;
    28. video/x-ms-asf asx asf;
    29. video/x-ms-wmv wmv;
    30. video/x-msvideo avi;
    31. }

    复制了一部分过来,原文件的内容还是不少的。可以看到这里的格式有点像kv键值对。比如说这里18行的 exe 文件,他所得到的是一个叫做 application/octet-stream 的类型,意思是以数据流的方式让浏览器去加载,并询问用户是否要进行下载操作。那比如说咱们现在有个一个非常奇怪的格式,这个格式是平常根本见不到的格式,那我们就可以将其写在此文件中,让他按照你想要的格式去让浏览器解读。

    default_type application/octet-stream;

    如果mime类型没匹配上,默认使用二进制流的方式传输。

    sendfile

    使用linux的 sendfile(socket, file, len) 高效网络传输,也就是数据0拷贝。 未开启sendfile。再发个比方:咱们现在有两台电脑,我们需要从PC1拿点东西去PC2,那我们就得先拿U盘去PC1里面拷贝,然后再用U盘拷贝到PC2中,而如果开启了这个sendfile,那就相当于直接隔空传过去,省去了拷贝的过程。

    sendfile off; 

    直接上图来讲:

    咱们的Nginx是假设在CentOS操作系统上的,当服务过来的时候是通过CentOS的网络接口然后转发给Nginx。Nginx拿到信息后会根据其内容去硬盘(SSD)中找到那个目标文件,然后Nginx会把这个文件完整的读一遍,读到自己的内存中再交给应用程序,应用程序也把拿到的文件完整的读进自己的内存,再转发到CentOS的网络接口(当然这里面还有DMA [ Direct Memory Access直接访问内存 ] 的调度,网络接口的缓存和内核的缓存)网络接口再把其发送出去。这里的层层缓存就相当于上面所说的层层复制。

    sendfile on;

    这里通过Nginx会给内核发送一个信号,此时的应用程序不会再去把文件读取进自己的内存了,而是直接发送这个 sendfile() 信号(当然这其实是一个方法,里面会写明需要传出的文件和socket)给CentOS的网络接口,网络接口在收到信号后回去找到这个目标文件,然后发送出去。

    keepalive_timeout  65;

    连接的超时时间限制。

    server {}

    这里可以把一个server看成是一个主机,所以一个Nginx可以拥有多台这样的主机,它们通过不同的端口号 listen 来区分。当然端口号也可以相同,那样的话就需要通过 server_name 来分区了。开启多个主机的方式也叫作虚拟主机(vhost)。

    location / {}

    咱们这回先举例子。 http://abc.com/xxx/index.html 这个叫做url,没啥问题吧。而这其中的 /xxx/index.html 叫做uri。当这个uri被匹配上的时候,那么就会进入到这个 location / {} 中,去找里面的root目录。这里的 root   html; 中的html其实是个相对路径,就是咱们下一小节提到的那个html目录。

    error_page   500 502 503 504  /50x.html;

    简单的来说,如果出现了这四个报错,那么就会进入到这个 50x.html 中,这个文件也会在下一小节中提到。


    html

    这里的 index.php 直接无视,这是我自己加进去的:

    index.html

    能看到这里放着两个html文件,其中的 index.html 为Nginx的默认页,可以看看里面是啥:

    1. html>
    2. <html>
    3. <head>
    4. <title>Welcome to nginx!title>
    5. <style>
    6. body {
    7. width: 35em;
    8. margin: 0 auto;
    9. font-family: Tahoma, Verdana, Arial, sans-serif;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <h1>Welcome to Tomcat!h1>
    15. <p>If you see this page, the nginx web server is successfully installed and
    16. working. Further configuration is required.p>
    17. <p>For online documentation and support please refer to
    18. <a href="http://nginx.org/">nginx.orga>.<br/>
    19. Commercial support is available at
    20. <a href="http://nginx.com/">nginx.coma>.p>
    21. <p><em>Thank you for using nginx.em>p>
    22. body>
    23. html>

    可以看到我把这里第13行的Nginx修改成了Tomcat,咱们可以去测试看一下。 

     很简单的html语言,咱们继续看第二个文件 50x.html 


    50x.html

    1. html>
    2. <html>
    3. <head>
    4. <title>Errortitle>
    5. <style>
    6. body {
    7. width: 35em;
    8. margin: 0 auto;
    9. font-family: Tahoma, Verdana, Arial, sans-serif;
    10. }
    11. style>
    12. head>
    13. <body>
    14. <h1>An error occurred.h1>
    15. <p>Sorry, the page you are looking for is currently unavailable.<br/>
    16. Please try again later.p>
    17. <p>If you are the system administrator of this resource then you should check
    18. the error log for details.p>
    19. <p><em>Faithfully yours, nginx.em>p>
    20. body>
    21. html>

    这里就是Nginx的一个默认的报错页面,也没啥好讲的。


    logs

    这里有两个log日志和一个pid文件。可以看到这个 access.log 真的是相当的大!足足有48KB(我这里才用了不到两天)

    access.log

    访问日志

    1. 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"
    2. 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"

    这里的内容实在是太多啦,所以只复制了两行过来。 

    其实也就是在用户访问咱们的时候,会把时间、地点、人物、人物所访问的文件、人物所带的参数等信息记录在此。所以这个文件通常都是非常的大(但是它并不会影响到Nginx的性能),为了防止数据写不进来而导致服务出现问题,所以在配置文件中会严格的把控 access.log 文件的大小,一旦它达到了那个指定的大小,那么系统就是重新新建一个文件用来继续写入访问信息日志。


    error.log

    随便复制几行过来看看: 

    1. 2022/08/24 11:05:47 [notice] 53322#0: signal process started
    2. 2022/08/24 11:28:44 [notice] 54846#0: signal process started
    3. 2022/08/24 18:23:11 [notice] 14640#0: signal process started
    4. 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/"

    这个日志就比上面的 access.log 要小很多了。说白了就是用户在访问服务时所遇到的错误,都会被记录在这个 error.log 里面。


    nginx.pid 

    能看出来这个文件跟上面的不一样啊,后缀为pid而不是log。这个文件是用来记录咱们Nginx主进程的ID号的。

    854
    

    内容也很简单,只有这一个数字。当然你们的ID跟我的不太可能会一致,咱们可以去系统里看看:

    ps -ef | grep nginx

    可以看到master主进程的ID与文件中内容一致。 


    sbin

    Nginx 

    下面只有一个Nginx的主程序,也没啥好说的。 

  • 相关阅读:
    PC摄像头设置 默认摄像头设置 win11 默认摄像头设置
    Linux之Shell变量和引用
    记一次 .NET 某传感器采集系统 线程爆高分析
    springboot上传文件到后台指定文件夹
    postgresql如何关闭自动提交设置
    mybatis-plus进行数据字段加密解密入库
    香港的Web3从业者们 出走新加坡还是选择留下?
    Android 多桌面图标启动, 爬坑点击打开不同页面
    保研笔记八——YOLOV5项目复习
    开源纯粹主义:每一颗螺丝钉都是自由的
  • 原文地址:https://blog.csdn.net/Trollz/article/details/126525238