Nginx配置文件详解(配置文件结构)

Nginx配置文件结构

在nginx的安装目录下的conf目录下有一个nginx.conf,这个就是nginx默认的配置文件,默认的内容如下:


#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;
        }

        #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  /scripts$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;
    #    }
    #}
}

Nginx配置文件结构:

....   #全局块

events{        #events块     

}

http{          #http块
    .....      #http全局块
   server{     #server块
        .....  #server全局块
       location [PATTERN]{     #location块
     
       }
 
       location [PATTERN]{
     
       }
   
   }

   server{
      ....
   }
}
  • 1、main配置影响nginx全局的指令。一般有运行nginx服务器的用户组nginx进程pid存放路径日志存放路径配置文件引入允许生成worker process数等。可配置的参数如下:

(1)user:来指定Nginx Worker进程运行用户以及用户组,默认由nobody账号运行 (2)worker_processes:指定了Nginx要开启的子进程数。每个Nginx进程平均耗费10M~12M内存。根据经验,一般指定1个进程就足够了。如果是多核CPU,建议指定和CPU的数量一样的进程数即可。 (3)error_log:用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit可供选择,其中,debug输出日志最为最详细,而crit输出日志最少。 (4)pid:用来指定进程id的存储文件位置。 (5)worker_rlimit_nofile:用于指定一个nginx进程可以打开的最多文件描述符数目,这里是65535,需要使用命令“ulimit -n 65535”来设置。 配置示例

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;

  • 2、events块配置影响nginx服务器或与用户的网络连接。有每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网路连接,开启多个网络连接序列化等。

(1)use用来指定Nginx的工作模式。Nginx支持的工作模式有selectpollkqueueepollrtsig/dev/poll。其中select和poll都是标准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD系统中,对于Linux系统,epoll工作模式是首选。 (2)worker_connections用于定义Nginx每个进程的最大连接数,即接收前端的最大请求数,默认是1024。最大客户端连接数由worker_processes和worker_connections决定,即Max_clients=worker_processes*worker_connections,在作为反向代理时,Max_clients变为:Max_clients = worker_processes * worker_connections/4。进程的最大连接数受Linux系统进程的最大打开文件数限制,在执行操作系统命令“ulimit -n 65536”后worker_connections的设置才能生效。 配置示例

events{
     use  poll;
     worker_connections    1024;    
}
  • 3、http块:可以嵌套多个server,http模块负责HTTP服务器相关属性的配置,有server和upstream两个子模块的配置。如文件引入,mime-type定义,日志自定义,是否使用sendfile传输文件,连接超时时间,单连接请求数等。

(1)include设定文件的mime类型,类型在配置文件目录下的mime.type文件定义,来告诉nginx来识别文件类型。 (2)default_type:设定了默认的类型为二进制流,也就是当文件类型未定义时使用这种方式,例如在没有配置asp的locate环境时,Nginx是不予解析的,此时,用浏览器访问asp文件就会出现下载了。 (3)log_format:用于设置日志的格式,和记录哪些参数,这里设置为main,刚好用于access_log来纪录这种类型。 配置示例

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 65; gzip on;

  • 4、server块配置虚拟主机的相关参数,一个http中可以有多个server。

(1)listen:用于指定虚拟主机监听的服务端口。 (2)server_name:用来指定IP地址或者域名,多个域名之间用空格分开。 (3)root :表示在这整个server虚拟主机内,全部的root web根目录。注意要和locate {}下面定义的区分开来。 (4)index :全局定义访问的默认首页地址。注意要和locate {}下面定义的区分开来。 (5)charset:用于设置网页的默认编码格式。 (6)access_log:用来指定此虚拟主机的访问日志存放路径,最后的main用于指定访问日志的输出格式。 配置示例

 server {
    listen       80;
    server_name  localhost;
    root   /Users/hk/www;
    index  index.php index.html index.htm; 
    charset utf-8;
    access_log  logs/host.access.log  main;
    aerror_log  logs/host.error.log   main;

}

  • 5、location块:location模块 负载均衡,反向代理,虚拟域名等配置。是来定位的,定位URL,解析URL,它也提供了强大的正则匹配功能,也支持条件判断匹配,可以通过location指令实现Nginx对动,静态网页进行过滤处理。

(1)location / 表示匹配访问根目录。 (2)root:用于指定访问根目录时,虚拟主机的web目录,这个目录可以是相对路径(相对路径是相对于nginx的安装目录)。也可以是绝对路径。 (3)proxy_pass:代理转发,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。 (4)proxy_set_header:允许重新定义或者添加发往后端服务器的请求头。 (5)include:加载配置文件,后面介绍nginx多个配置文件时候会提到。 (6)index:定义页面显示html,一般和alias配合使用。 (7)root:定位localtion匹配的url资源路径。 配置示例

 location / {       
        root   html;    
        index  index.html index.htm;

}

  • 6、upstream:模块负债负载均衡模块,通过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。

Nginx的负载均衡模块目前支持4种调度算法: (1)weight 轮询(默认)每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。weight指定轮询权值,weight值越大,分配到的访问机率越高,主要用于后端每个服务器性能不均的情况下。 (2)ip_hash每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。 (3) fair。比上面两个更加智能的负载均衡算法。此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块。 (4)url_hash按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率。Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx 的hash软件包。

在HTTP Upstream模块中,可以通过server指令指定后端服务器的IP地址和端口,同时还可以设定每个后端服务器在负载均衡调度中的状态。常用的状态有: down,表示当前的server暂时不参与负载均衡。 backup,预留的备份机器。当其他所有的非backup机器出现故障或者忙的时候,才会请求backup机器,因此这台机器的压力最轻。 max_fails,允许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。 fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails可以和fail_timeout一起使用。

留言区

还能输入500个字符