您的位置:首页 > 运维架构 > Nginx

Nginx学习笔记(二):配置文件

2014-01-04 12:48 405 查看

前言

学习笔记第二篇,之前想直接跳过去的,因为感觉比较枯燥。但是后面发现配置非常需要了解,所以重头看。

Nginx配置

Nginx在服务启动时会读入配置文件,后续的动作行为会按照配置文件中的指令进行。

Nginx的配置文件是纯文本文件,默认安装Nginx后,其配置文件均在/usr/local/nginx/conf/目录下。其中,nginx.conf为主配置文件。

我的主配置文件nginx.conf示例如下:

### 正常运行配置项
#		Nginx worker进程运行的用户及用户组
###
#user  nobody;

### 优化性能配置项
#		Nginx worker进程运行的个数
###
worker_processes  1;

### 调试进程和定位问题配置项
#   语法:error_log /path/file level;
#		level级别:debug、info、notice、warn、error、crit、alert、emerg,从左至右级别增大
#		设定一个级别,大于或等于其级别的日志将输出到指定/path/file文件中
###
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

### 正常运行配置项
#		保存master进程ID的pid文件存放路径
###
#pid        logs/nginx.pid;

### 事件类配置项
#		每个worker的最大连接数
###
events {
    worker_connections  1024;
}

http {
	# 嵌入其他配置文件
    include       mime.types;
    
    # 默认MIME type???
    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;

	# Linux中sendfile系统调用发送文件,减少内核态与用户态的两次内存复制
    sendfile        on;
    
    # 发送响应时把整个响应包头放到一个TCP包中发送
    #tcp_nopush     on;

	# keepalive超时时间
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

	### server块
	#		每个server块就是一个虚拟主机,按照server_name区分
	###
    server {
    		# 监听端口
        listen       80;
        # 主机名称
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

	# 与用户请求中的URI进行匹配,如果匹配成功,就选择{}中的配置处理用户请求
        location / {
            # 以root方式设置资源路径
            root   html;
            # 访问网站首页
            index  index.html index.htm;
        }

	# 根据HTTP返回码重定向页面
        #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;
    #    }
    #}

}




配置解析

1,以#打头,注释,供编辑配置文件的用户查看;

2,主配置文件任何时候都需要使用,其他配置文件视具体情况而定;

3,在配置文件中,包含若干配置项,按照用户使用时的预期功能,可以分成4类:

用于调试、定位问题的配置项
正常运行的必备配置项
优化性能的配置项
事件类配置项

有一些配置项,即使没有显示进行配置,它们也会有默认值,比如daemon,在nginx.conf中并没有进行配置,但也默认打开此功能。

4,配置项由配置指令和指令参数两部分构成。

e.g. “ 配置指令 指令参数1 指令参数2 ... ;”

参数之间可用一个或多个空格或者TAB分隔符隔开,以“;”号结束;

配置指令可以是数字也可以是字符串(包括正则),如果指令中包含空格,需要用单引号括住;

配置项可分为简单配置项和复杂配置项;

简单配置项:

access_log  logs/host.access.log  main;


复杂配置项:

location / {
            root   html;
            index  index.html index.htm;
        }
5,Nginx配置文件以block形式组织。一个block通常以{}形式表示。

当前Nginx支持的几种block形式,通常的结构组织如图示(可对照上面的主配置文件):



以上的block注释:

main:Nginx在运行时与具体业务功能(比如http服务或者email服务)无关的一些参数,比如工作进程数,运行的身份等;

http:与提供http服务相关的配置参数。比如,是否使用keep-live,是否使用gzip进行压缩;

server:http服务上支持若干虚拟主机。每个虚拟主机对应一个server配置项,其中包含虚拟主机相关配置。提供mail服务时,同样可以建立若干server。每个server通过监听的地址来区分;

location:http服务中,某些特定的URL对应的一些列的配置项;

mail:实现email相关的SMTP/IMAP/POP3代理时,一些共享的配置项;

(这块现在仍以记录为主,现在表示还是没有多大概念,以后回头再来理会)

从结构组织图以及主配置文件可以看出,块配置项可以嵌套。内层块直接继承外层快。比如,server块里的配置都基于http块里的已有配置。当内外层块中的配置发生冲突时,以哪个块的配置为准,取决于解析配置项的模块。

另外,Nginx还能配置为反向代理服务器。而作为代理服务器,一般需要向上游服务器转发请求,因此配置在upstream块。这一块上文也没有提及,暂时也没法学习,在此略过。

总结

在笔记(一)中,我们已经知道,在执行configure命令时,我们已经将许多模块编译进Nginx中,即ngx_modules.c中的数组,但是否启用这些模块,即取决于配置文件中相应配置。也就是说,每个Nginx模块都有自己感兴趣的配置项,大部分模块都必须在nginx.conf主配置文件中读取某个配置项后才会在运行时启动。比如,只有当配置http{...}这个配置项后,ngx_http_module模块才会启动,其他依赖此模块的其他模块也才能正常使用。

参考内容:

《深入理解Nginx》

Nginx开发从入门到精通

Nginx模块开发入门
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: