Nginx—nginx.conf 设置结构详解

打印 上一主题 下一主题

主题 1850|帖子 1850|积分 5550

一、nginx.conf 设置结构



函数
说明
main
全局设置
event
设置工作模式以及连接数
http
http模块相干设置
server
捏造主机设置,可以有多个
location
路由规则,表达式
upstream
集群、内网服务器(负载均衡也在这里边配)


二、Nginx设置语法



基本的语法:
指令集组成:每个指令单独写一行,每个指令分号 ";" 分开,每个指令块用大括号 "{ ... }" 分开,大括号的后方没有分号。注释用#号分开。


$符号:$符号为nginx内部提供的一些参数变量。




三、nginx.conf 核心设置文件详解

 
函数
说明
main
全局设置
event
设置工作模式以及连接数
http
http模块相干设置
server
捏造主机设置,可以有多个
location
路由规则,表达式
upstream
集群、内网服务器(负载均衡也在这里边配)
 
主设置文件详解

  1. #user  nobody;                   #表示当系统在执行worker进程的时候由哪个用户去执行,(默认为nobody)
  2. worker_processes  10;            #逻辑CPU的个数设置的值为:(n-1)
  3. # nginx的日志级别:debug info notice warn error crit 等级逐渐升高。
  4. #error_log  logs/error.log;      #错误的日志,在编译的时候已经设置相关的路径。
  5. #error_log  logs/error.log  notice;
  6. #error_log  logs/error.log  info;
  7. #pid        logs/nginx.pid;
  8. events {
  9.     #默认使用epoll
  10.     use epoll;
  11.     #每个worker允许的客端最大连接数
  12.     worker_connections  1024;
  13. }
  14. http {
  15.     include       mime.types;
  16.     default_type  application/octet-stream;
  17.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
  18.     #                  '$status $body_bytes_sent "$http_referer" '
  19.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  20.     #access_log  logs/access.log  main;
  21.     sendfile        on;
  22.     #tcp_nopush     on;
  23.     #keepalive_timeout  0;
  24.     keepalive_timeout  65;
  25.     #gzip  on;
  26.     server {
  27.         listen       8080;
  28.         server_name  localhost;
  29.         #charset koi8-r;
  30.         #access_log  logs/host.access.log  main;
  31.         location / {
  32.             root   html;
  33.             index  index.html index.htm;
  34.         }
  35.         #error_page  404              /404.html;
  36.         # redirect server error pages to the static page /50x.html
  37.         #
  38.         error_page   500 502 503 504  /50x.html;
  39.         location = /50x.html {
  40.             root   html;
  41.         }
  42.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  43.         #
  44.         #location ~ \.php$ {
  45.         #    proxy_pass   http://127.0.0.1;
  46.         #}
  47.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  48.         #
  49.         #location ~ \.php$ {
  50.         #    root           html;
  51.         #    fastcgi_pass   127.0.0.1:9000;
  52.         #    fastcgi_index  index.php;
  53.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  54.         #    include        fastcgi_params;
  55.         #}
  56.         # deny access to .htaccess files, if Apache's document root
  57.         # concurs with nginx's one
  58.         #
  59.         #location ~ /\.ht {
  60.         #    deny  all;
  61.         #}
  62.     }
  63.     # another virtual host using mix of IP-, name-, and port-based configuration
  64.     #
  65.     #server {
  66.     #    listen       8000;
  67.     #    listen       somename:8080;
  68.     #    server_name  somename  alias  another.alias;
  69.     #    location / {
  70.     #        root   html;
  71.     #        index  index.html index.htm;
  72.     #    }
  73.     #}
  74.     # HTTPS server
  75.     #
  76.     #server {
  77.     #    listen       443 ssl;
  78.     #    server_name  localhost;
  79.     #    ssl_certificate      cert.pem;
  80.     #    ssl_certificate_key  cert.key;
  81.     #    ssl_session_cache    shared:SSL:1m;
  82.     #    ssl_session_timeout  5m;
  83.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  84.     #    ssl_prefer_server_ciphers  on;
  85.     #    location / {
  86.     #        root   html;
  87.     #        index  index.html index.htm;
  88.     #    }
  89.     #}
  90. }
复制代码

(一)main 全局设置模块

1、历程用户设置
  1. user root;
  2. worker_processes 10;
  3. worker_rlimit_nofile 65535;
复制代码
   
  

  • user root;

    • 这一设置项指定了 Nginx 工作历程所利用的用户身份。root 是体系中的超等用户,拥有最高权限。不过,从安全角度思量,不建议让 Nginx 以 root 用户身份运行,因为这会使 Nginx 拥有过高的权限,一旦出现安全毛病,攻击者大概会获取体系的最高控制权。通常,建议创建一个专门的低权限用户来运行 Nginx。

  • worker_processes 4;

    • 此设置项用于设置 Nginx 工作历程的数量。Nginx 采取多历程模子,一个主历程(master process)负责管理多个工作历程(worker processes),工作历程负责处理处罚实际的客户端请求。4 代表创建 4 个工作历程。一样平常而言,可以根据服务器的 CPU 核心数来设置该值,通常设置为 CPU 核心数或者核心数的两倍,如许能充分利用服务器的 CPU 资源。

  • worker_rlimit_nofile 65535;

    • 该设置项设定了每个 Nginx 工作历程能够打开的最大文件描述符数量。在 Linux 体系里,一切皆文件,包括网络连接、磁盘文件等,每个打开的文件或者连接都会占用一个文件描述符。65535 意味着每个工作历程最多可以同时打开 65535 个文件描述符。当服务器必要处理处罚大量并发连接时,就必要增大这个值,防止出现 “too many open files” 的错误。

  




2、 nginx日志路径设置
  1. #error_log  logs/error.log;      #错误的日志,在编译的时候已经设置相关的路径放:/var/log/nginx/
  2. #error_log  logs/error.log  notice;
  3. #error_log  logs/error.log  info;
复制代码


 3、存放pid的地方
  1. #pid        logs/nginx.pid;     #进程号存在的路径,在编译的时候已经设置相关的路径放:/var/run/nginx/
复制代码


 (二)、events设置工作模式以及连接数

  1. events {
  2.     #默认使用epoll
  3.     use epoll;
  4.     #每个worker允许客端连接的最大连接数,根据硬件的配置来选值的大小。
  5.     worker_connections  1024;
  6. }
复制代码

(三)、http相干网络传输的模块(包罗了许多的设置内容)

  1. http {
  2.     include       mime.types;   #导入外部的文件,文件中为指令块,当前目录的mime.types文件。
  3.     default_type  application/octet-stream;    #默认的type类型。
  4.   *********************************************日志模块分析**********************************************************
  5.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  #access_log 日志的格式,可以自定义格式。
  6.     #                  '$status $body_bytes_sent "$http_referer" '
  7.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  8.     #access_log  logs/access.log  main;
  9.     ***参数注解区****
  10.     # $remote_addr               客户端的IP地址
  11.     # $remote_user               用户名称,可以是 "-"
  12.     # [$time_local]              访问时间
  13.     # $request                   请求的内容包括:URL 请求的方法GET、POST
  14.     # $status                    响应的状态码
  15.     # $body_bytes_sent           客户端发送的文件主体所包含内容的字节数
  16.     # $http_referer              记录着用户从哪个访问链接跳转过来的,我们在做日志分析的时候会用到。
  17.     # $http_user_agent           用户的代理
  18.     # $http_x_forwarded_for      可以记录客户端的IP
  19.     ****************
  20.   ******************************************************************************************************************
  21.     sendfile        on;
  22.     #tcp_nopush     on;
  23.     #keepalive_timeout  0;
  24.     keepalive_timeout  65;
  25.     #gzip  on;
  26.     server {
  27.         listen       8080;
  28.         server_name  localhost;
  29.         #charset koi8-r;
  30.         #access_log  logs/host.access.log  main;
  31.         location / {
  32.             root   html;
  33.             index  index.html index.htm;
  34.         }
  35.         #error_page  404              /404.html;
  36.         # redirect server error pages to the static page /50x.html
  37.         #
  38.         error_page   500 502 503 504  /50x.html;
  39.         location = /50x.html {
  40.             root   html;
  41.         }
  42.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  43.         #
  44.         #location ~ \.php$ {
  45.         #    proxy_pass   http://127.0.0.1;
  46.         #}
  47.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  48.         #
  49.         #location ~ \.php$ {
  50.         #    root           html;
  51.         #    fastcgi_pass   127.0.0.1:9000;
  52.         #    fastcgi_index  index.php;
  53.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  54.         #    include        fastcgi_params;
  55.         #}
  56.         # deny access to .htaccess files, if Apache's document root
  57.         # concurs with nginx's one
  58.         #
  59.         #location ~ /\.ht {
  60.         #    deny  all;
  61.         #}
  62.     }
  63.     # another virtual host using mix of IP-, name-, and port-based configuration
  64.     #
  65.     #server {
  66.     #    listen       8000;
  67.     #    listen       somename:8080;
  68.     #    server_name  somename  alias  another.alias;
  69.     #    location / {
  70.     #        root   html;
  71.     #        index  index.html index.htm;
  72.     #    }
  73.     #}
  74.     # HTTPS server
  75.     #
  76.     #server {
  77.     #    listen       443 ssl;
  78.     #    server_name  localhost;
  79.     #    ssl_certificate      cert.pem;
  80.     #    ssl_certificate_key  cert.key;
  81.     #    ssl_session_cache    shared:SSL:1m;
  82.     #    ssl_session_timeout  5m;
  83.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  84.     #    ssl_prefer_server_ciphers  on;
  85.     #    location / {
  86.     #        root   html;
  87.     #        index  index.html index.htm;
  88.     #    }
  89.     #}
  90. }
复制代码

mime.types文件


3.1、日志格式

  1.     #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '  #access_log 日志的格式,可以自定义格式。
  2.     #                  '$status $body_bytes_sent "$http_referer" '
  3.     #                  '"$http_user_agent" "$http_x_forwarded_for"';
  4.     #access_log  logs/access.log  main;
  5.     ***参数注解区****
  6.     # $remote_addr               客户端的IP地址
  7.     # $remote_user               用户名称,可以是 "-"
  8.     # [$time_local]              访问时间
  9.     # $request                   请求的内容包括:URL 请求的方法GET、POST
  10.     # $status                    响应的状态码
  11.     # $body_bytes_sent           客户端发送的文件主体所包含内容的字节数
  12.     # $http_referer              记录着用户从哪个访问链接跳转过来的,我们在做日志分析的时候会用到。
  13.     # $http_user_agent           用户的代理
  14.     # $http_x_forwarded_for      可以记录客户端的IP
复制代码



3.2、文件的高效传输

  1.     sendfile        on;          #打开,表示文件传输的性能会得到提升,nginx的性能也得到相应的提升。
  2.     #tcp_nopush     on;          #和sendfile一起使用,表示当我们的数据包累积到一定的大小之后再发送,可以提高传输的效率。先取数据在进行统一分发。
复制代码

3.3、客户端连接服务器的超时时间(传输完成后保持的时间)

  1.    keepalive_timeout  65;            #以秒为单位,http有keepalive机制,当数据传输完成之后会保持一定时间的连接处于打开状态,如果客户端有新的请求会用此连接去处理。不用创建新的连接,节省资源的开销。
复制代码

3.4、gzip压缩

  1. #gzip  on;      #内容的传输经过压缩之后体积变小,提升的传输速率,减少了带宽的产生,但是在压缩的过程中会消耗我们系统上CPU的性能。
复制代码

3.5、server模块,捏造主机相干设置

  1.     server {
  2.         listen       8080;                #服务端口号
  3.         server_name  localhost;           #服务IP、域名
  4.         #charset koi8-r;
  5.         #access_log  logs/host.access.log  main;
  6.         location / {                      #配置页面显示的路由:location
  7.             root   html;
  8.             index  index.html index.htm;
  9.         }
  10.         #error_page  404              /404.html;
  11.         # redirect server error pages to the static page /50x.html
  12.         #
  13.         error_page   500 502 503 504  /50x.html;         #访问错误的时候会返回相应的状态值。
  14.         location = /50x.html {
  15.             root   html;
  16.         }
  17.         # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  18.         #
  19.         #location ~ \.php$ {
  20.         #    proxy_pass   http://127.0.0.1;
  21.         #}
  22.         # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  23.         #
  24.         #location ~ \.php$ {
  25.         #    root           html;
  26.         #    fastcgi_pass   127.0.0.1:9000;
  27.         #    fastcgi_index  index.php;
  28.         #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  29.         #    include        fastcgi_params;
  30.         #}
  31.         # deny access to .htaccess files, if Apache's document root
  32.         # concurs with nginx's one
  33.         #
  34.         #location ~ /\.ht {
  35.         #    deny  all;
  36.         #}
  37.     }
  38.     # another virtual host using mix of IP-, name-, and port-based configuration
  39.     #
  40.     #server {
  41.     #    listen       8000;
  42.     #    listen       somename:8080;
  43.     #    server_name  somename  alias  another.alias;
  44.     #    location / {
  45.     #        root   html;
  46.     #        index  index.html index.htm;
  47.     #    }
  48.     #}
  49.     # HTTPS server
  50.     #
  51.     #server {
  52.     #    listen       443 ssl;
  53.     #    server_name  localhost;
  54.     #    ssl_certificate      cert.pem;
  55.     #    ssl_certificate_key  cert.key;
  56.     #    ssl_session_cache    shared:SSL:1m;
  57.     #    ssl_session_timeout  5m;
  58.     #    ssl_ciphers  HIGH:!aNULL:!MD5;
  59.     #    ssl_prefer_server_ciphers  on;
  60.     #    location / {
  61.     #        root   html;
  62.     #        index  index.html index.htm;
  63.     #    }
  64.     #}
  65. }
复制代码

3.5.1 在nginx.conf文件中添加新的server模块。

  1.     server {
  2.         listen       8888;               #指定的服务端口为8888
  3.         server_name  127.0.0.1;          #指定的服务器的名称是127.0.0.1
  4.         location / {
  5.             root   html;
  6.             index  test.html index.htm;  #访问到的内容为test.html文件            
  7.         }  
复制代码


3.5.2 添加test.html文件:/usr/local/nginx/html/test.html 


 


 
3.6、通过include函数的调用server模块的设置,进步文件的可读性。

3.6.1 在nginx.conf文件中定义include调用server模块(支持正则匹配)

可用统一将设置文件放在/usr/local/nginx/conf/conf.d指定的路径下如许方便管理,如:


  • HTTP相干的设置放在:/usr/local/nginx/conf/conf.d/http 目录下
  • TCP相干的设置放在:/usr/local/nginx/conf/conf.d/tcp 目录下
  1. user root;
  2. worker_processes 4;
  3. worker_rlimit_nofile 65535;
  4. events {
  5.     ...
  6. }
  7. include conf.d/tcp/*.conf;  #TCP相关配置(不能放在下边的HTTP模块中不然会报错);这里的include是指包含/usr/local/nginx/conf/conf.d/tcp路径下所有的.conf。
  8. http {
  9.     ...
  10.     include conf.d/http/*.conf; #HTTP相关配置(需要放在HTTP模块中不然会报错);这里的include是指包含/usr/local/nginx/conf/conf.d/http 路径下所有的.conf
  11. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

郭卫东

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表