默认nginx服务器配置文件都存放在安装目录conf中,主配置文件名为nginx.conf.

默认nginx.conf在/usr/local/nginx目录下,上次编译安装nginx主配置文件存放在/etc/nginx下。

安装编译:http://magine356wj.blog.51cto.com/2664956/1677064

下面是关于nginx.conf的一些注释

#####################全局块##############################################
#user  nobody;                #使用的用户和组,默认是nobody,编译时设置成nginx
worker_processes  1;          #指定工作进程数,通常有几颗CPU用在nginx上就设置对应数字

#error_log  logs/error.log;       #可以使用[debug | info| notice| warn | error | crit ]参数
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;       #指定pid存放路径

################events事件模块###########################################
events {
    worker_connections  1024;     每个worker的最大连接数
}
######################http模块###########################################

http {#以下指令在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.conf一共由三部分组成,分别为core(全局),events块,http块。在http块中,又包含http 全局块,多个server块。每个server块又包含server全局和多个location1.在同一配置中嵌套的配置块,各个之间不存在次序关系。

一、core:

全局块是默认配置文件从开始到events块之间的一部分内容,主要是设置一些影响Nginx服务器整体运行的配置指令。这些指令的作用域是Nginx服务全局。
user
语法: user user[group]
缺省值:nobody nobody
指定nginx worker进程运行用户,默认是nobody账号

error_log
语法: error_log file [ debug | info | notice | warn | error | crit ]   
缺省值: ${prefix}/logs/error.log   
制定错误日志的存放位置和级别。

pid   
语法: pid file   
进程id存储文件。可以使用 kill -HUP cat /var/log/nginx.pid/ 对Nginx进行配置文件重新加载。   
  
worker_processes   
语法: worker_processes number   
缺省值: 1   
指定工作进程数。nginx可以使用多个worker进程。

 

image

将number改为3,再执行ps ax | grep nginx,就可以看到除了主进程master process,已经生成了3个worker process。

 

二、events块

  events块涉及的指令主要影响Nginx服务器与用户的网络连接。
use epoll; #说明使用哪种事件模型,默认为epoll,可以不写。
worker_connections number; #主要用来设置允许每一个worker process同时开启的最大连接数。

clients = worker_connections * work_process,根据实际情况设置worker_connections。

 

三、http模块:

http模块中指令包含文件引入,MIME-Type定义,日志定义,是否使用sendfile传输文件,连接超时时间,单链接请求数上限等。

image

include       mime.types;
default_type  application/octet-stream;

常用的浏览器可以显示HTML,XML,GIF及Flash等种类繁多的文件,媒体资源等。浏览器区分这些资源需要使用MIME Type。default_type配置了用于处理前端请求的MIME类型。
Nginx服务器支持对服务日志的格式,大小,输出等进行配置。
access_log 指定路径
log_format  指定日志格式
具体参数可以查阅:http://nginx.org/en/docs/http/ngx_http_log_module.html

 

sendfile on | off;   开启或关闭sendfile

keepalive_timeout timeout,默认是65s。

gzip on       是否开启gzip压缩,默认禁止

四、server,location模块

image

 

location [ = | ~ | ~* | ^~ ] uri { ... }

= ,要求字符串与uri严格匹配
~ ,用正则,区分大小写
~*,用正则,不区分大小写
^~,不做模式匹配




优先级:
第一是=;
第二是~;
最后是不带任何符号的/

注:有关Http模块,server,location只是初略标注。虚拟主机,rewrite等功能,会在后续博客写出。