nginx优化之nginx的配置文件详解

前言

默认的nginx服务器配置文件都存放在安装目录conf中,主配置文件名为nginx.conf,下面我们来看看nginx的配置文件内容和一些基本的配置方法。

一个完整nginx配置文件展示

worker_processes 1;                                 #全局生效

events {
  worker_connections 1024;                          #在events部分中生效
}

http {
  include mime.types       ;                               #以下指令在http部分中生效
  default_type application/octet-stream;
  sendfile on;
  keepalive_timeout 65;
  server {                                          #以下指令在http的server部分中生效
    listen 80;
    server_name localhost;
    location / {                                    #以下指令在http/server的location中生效
      root html;
      index index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location - /50x.html {
      root html;
    }

    location ~ .php$ {
        root html;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
  }
}

注意:在nginx配置中,还包括注释内容,注释标志位“#”。

nginx文件配置的结构

初始的nginx服务器主配置是比较长的,不过结构和内容还是比较清晰的,以下我们分为几个小节进行分析介绍。通过上面的配置文件内容分,我们可以总结出以下nginx.conf的基本结构:

...                            #全局快

events                         #events块
{
  ...
}

http                           #http块
{
  ...                          #http全局块

  server                       #server块
  {
    ...                        #server全局块

    location [PATTERN]         #location块
    {
      ...
    }

    location [PATTERN]         #location块
    {
      ...
    }
  }

  server                       #server块
  {
    ...
  }

  ...                          #http块
}

通过上图,我们可以看出,nginx主要有三块组成,分别是全局块,events块和http块;在http块中,又包含http全局块,多个server块;每个server块中,可以包含server全局块和多个location块.

在同一块配置中嵌套的配置块,各个之间不存在次序关系.

nginx详细配置信息

以下是一个nginx的完整配置文件,本节我们将以下面的配置文件为主解析nginx各个模块的参数已经配置,在完成解析后测试nginx服务。

####################  全局块 开始 ####################

#配置允许运行nginx服务器的用户和用户组
user root root;

#配置允许nginx进程生成的worker processes数
worker processes 3;

#配置允许nginx服务器运行对错误日志存放路径
error_log logs/error.log;

#配置nginx服务器运行时的pid文件存放路径和名称
pid nginx.pid;

####################  全局块 结束 ####################

####################  server块 开始 ####################

events
{
  #配置事件驱动模型
  use epoll;

  #配置最大连接数
  worker_connections 1024;
}

####################  server块 结束 ####################

####################  http块 开始 ####################
http
{
  # 定义MIME-Type
  include mime.types;
  default_type application/octet-stream;

  #配置允许使用sendfile方式传输
  sendfile on;

  #配置连接超时时间
  keepalive_timeout 65;

  #配置请求处理日志的格式
  log_format access.log;
  '$remote_addr - [$time_local]-"$request"-"$http_user_agent"';

  ####################  server块 开始 ####################
  #配置虚拟主机myServer1
  server {
    #配置监听端口和主机名称(基于名称)
    listen 8081;
    server_name myServer1;

    #配置请求处理日志存放路径
    access_log /myweb/server1/log/access.log;

    #配置错误页面
    error_page 404 /404.html;

    #配置处理/server1/location1请求的location
    location /server/location1 {
      root /myweb;
      index index.svrl-locl.htm;
    }

    #配置处理/server1/location2请求的location
    location /server1/location2 {
      root /myweb;
      index index.svrl-loc2.htm;
    }
  }

  #配置虚拟主机myServer2
  server {
    listen 8082;
    server_name 192.168.1.3;
    access_log /myweb/server1/log/access.log;

    #对错误页面404.html做了定向配置
    error_page 404 /404.html;

    location /server2/location1 {
      root /myweb;
      index index.svr2-locl.htm;W
    }

    location /svr2/loc2 {
      #对location的URL进行更改
      alias /myweb/server2/location2/;
      index index.svr2-loc2.htm;
    }

    #配置错误页面转向
    location = /404.html {
      root /myweb/;
      index 404.html;
    }
  }
  ####################  server块 结束 ####################
}
####################  http块 结束 ####################
原文地址:https://www.cnblogs.com/studyandstudy/p/14993507.html