【Nginx】Nginx 配置文件

妖狐艹你老母 2023-02-17 11:03 130阅读 0赞

Nginx 配置文件

Nginx 配置文件一般存放在 usr/local/nginx/conf 目录下,nginx.conf 就是配置文件

  1. ########### 每个指令必须有分号结束。#################
  2. #user administrator administrators; #配置用户或者组,默认为nobody nobody。
  3. #worker_processes 2; #允许生成的进程数,默认为1
  4. #pid /nginx/pid/nginx.pid; #指定nginx进程运行文件存放地址
  5. error_log log/error.log debug; #制定日志路径,级别。这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
  6. events {
  7. accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
  8. multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
  9. #use epoll; #事件驱动模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport
  10. worker_connections 1024; #最大连接数,默认为512
  11. }
  12. http {
  13. include mime.types; #文件扩展名与文件类型映射表
  14. default_type application/octet-stream; #默认文件类型,默认为text/plain
  15. #access_log off; #取消服务日志
  16. log_format myFormat '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定义格式
  17. access_log log/access.log myFormat; #combined为日志格式的默认值
  18. sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,location块。
  19. sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上限。
  20. keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
  21. upstream mysvr {
  22. server 127.0.0.1:7878;
  23. server 192.168.10.121:3333 backup; #热备
  24. }
  25. error_page 404 https://www.baidu.com; #错误页
  26. server {
  27. keepalive_requests 120; #单连接请求上限次数。
  28. listen 4545; #监听端口
  29. server_name 127.0.0.1; #监听地址
  30. location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小写。
  31. #root path; #根目录
  32. #index vv.txt; #设置默认页
  33. proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
  34. deny 127.0.0.1; #拒绝的ip
  35. allow 172.18.5.54; #允许的ip
  36. }
  37. }
  38. }

Nginx 配置文件组成

Nginx 配置文件有三部分:

全局块

从配置文件到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径,日志存放路径和类型以及配置文件的引入等。

比如第一行配置的:

  1. worker_processes 1;

这是 Nginx 服务器并发处理服务的关键配置,worker_processes 值越大,可以支持的并发数量也越多,但是会受到硬件、软件等设备的制约。

events 块

  1. events {
  2. worker_connections 1024; #work process 支持的最大连接数为 1024
  3. }

events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process 下的网络连接进行序列化,是否允许同时接受多个网络连接,选取哪种时间驱动模型来处理连接请求,每个 work process 可以同时支持的最大连接数。

http 块

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

这是 Nginx 服务器配置最频繁的部分,代理,缓存和日志定义等绝大多数功能和第三方模块的配置都在这里。需要注意的是:http 块包括 http 全局块、server 块

http 全局块

http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。

server 块

这块和虚拟主机有密切关系,虚拟主机从用户角度看,和一台独立的硬件主机是完全一样的,该技术的产生是为了节省互联网服务器硬件成本。

每个 http 块可以包括多个 server 块,而每个 server 块就相当于一个虚拟主机。

而每个 server 块也分为全局 server 块,以及可以同时包含多个 location 块。

  • 全局 server 块

    最常见的配置是本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置。

  • location 块

    一个 server 块可以配置多个 location 块。

    这块的主要作用是基于 Nginx 服务器接收到的请求字符串(例如 server_name/uri-string),对虚拟主机名称(也可以是 IP 别名)之外的字符串(例如 前面的 /uri-string)进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许

发表评论

表情:
评论列表 (有 0 条评论,130人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx 配置文件配置

    nginx 分为三个模块 核心模块 : nginx最基本最核心的服务,例如 进程管理,权限控制,日志记录 http模块 第三方模块   nginx配置文件实例

    相关 配置文件

    / 程序头部注释开始 程序的版权和版本声明部分 Copyright (c) 2011, 烟台大学计算机学院学生