码迷,mamicode.com
首页 > 其他好文 > 详细

2、Nginx配置文件nginx.conf的配置详解

时间:2016-05-03 16:22:41      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:nginx配置文件详解

前面Nginx安装配置文件中简单的解释了nginx.conf配置文件中几个指令的含义,这篇文章内容将对这些指令的用法作出详细的解释。

先看看配置文件的内容:

user  nginx;
worker_processes  4;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

Nginx的配置分为全局块、events块、http块和server块。在nginx.conf文件中只包含了全局块、events块和http块的内容,server块的配置需要自己定义。每一个server块都可以当做一个虚拟主机,一个http块可以包含多个server块,每一个server块的配置都是独立的,不会影响到其他server块。http全局块的配置对server块有效,但是如果server块中和http全局块的配置冲突,则采用就近原则,以server块的配置为准。下面来看看配置文件中这些指令的含义和其用法。


全局配置段:

user nginx;

user :指定可以运行Nginx服务的用户,只有被设置的用户或者用户组成员才有运行nginx进程的权限,如果希望所有用户都能运行Nginx进程,则可以注释掉该指令,或者设置为user nobody nonobody;

user指令用法:

user  username [groupname];


work_processes 4;

work_processes:设置Nginx服务器运行时启动的进程数,理论上设置的值越大,则Nginx服务器能响应的请求数越多,但是由于受到服务器软硬件(CPU和磁盘驱动器)的限制,所以必须合理设置才行。

一般设置为与CPU核心数相等,或者为CPU核心数减去1,我的CPU为4核,所以我设置为4。

work_processes执行用法:

work_processes number | auto;

number为work_processes启动的最大processes数,设置为auto时,则Nginx服务器自动检测并设置。



error_log  /var/log/nginx/error.log warn;

error_log:设置Nginx服务器错误日志路径。

error_log用法:

error_log file | stderr 【debug | info | notice | warn | error | crit | alert | emerg】

从语法结构看,Nginx服务器的日志支持输出到某一固定的文件file,或者输出到标准错误输出stderr,日志级别是可选项,由低到高为debug(需要编译时使用--with-debug开启debug开关)info、notice、warn、error、crit 、alert、emerg等。


pid        /var/run/nginx.pid;

pid:指定Nginx服务器PID文件的存放路径。

pid用法:

pid file;


其他配置指令:

1、worker_cpu_affinity CPUMASK CPUMASK ...;

该指令用来为每个进程分配CPU的工作内核,其值为几组二进制的值表示,例如:

worker_cpu_affinity 0001 0010 0100 1000;

如果CPU为8核,则可以这样设置:

worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 0010000 001000000 10000000;

2、worker_priority number;

指定Nginx进程的CPUnice值,范围为-20到19,值越小,优先级越高。默认所有进程nice值为0

3、worker_rlimit_nofile number;

指定每个worker进程能打开的最大文件描述符的数量

4、worker_rlimit_sigpending number;

指定每个用户能够发给worker进程的信号的最大数量




events配置段:

worker_connections  1024;

worker_connections:设置每一个work_process的最大并发连接数,默认为1024。其语法为:

worker_connections number;



其他配置指令:

1、accept_mutex on | off;

当某一时刻只有一个网络请求进来,多个睡眠的Nginx进程会被叫醒来响应请求,但是只有一个进程能获得连接,如果每次唤醒的进程数目太多,会影响一部分系统性能。accept_mutex就是为解决这一问题。当设置为on的时候,表示让多个worker轮流的序列化的响应请求。默认为开启状态,其只能在events段中配置。

2、multi_accpet on |off;

设置每个Nginx进程都能同时接收多个请求,默认为关闭状态。同样只能在events段中配置

3、use 【select | poll | kqueus | epoll | rtsig | /dev/poll | eventport】

该指令用来选择事件驱动的模型,建议让Nginx自动选择


http配置段:

include       /etc/nginx/mime.types;

include:引入其他的Nginx配置或者第三方模块的配置到当前主配置文件中,语法为:

include file;

file为要引入的配置文件,支持相对路径。


default_type  application/octet-stream;

default_type:用于指定处理前端请求的MIME类型。此指令还可以在http段、server段、或者location中配置


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  /var/log/nginx/access.log  main;

access_log:用于设置访问日志的路径

log_format:用于设置访问日志的格式,配置文件中的$remote_addr都是nginx的内置变量


sendfile        on;

sendfile:用于设置是否启用sendfile功能,语法为:sendfile on | off;


sendfile_max_chunk_size;

设置sendfile传输的数据量最大值,如果设置为0,则不限制。


keepalive_timeout  65;

keepalive_timeout:设置长连接的会话保持时间,该指令还可以在server段和location中设置。其语法为:keepalive timeout [header_timeout]; 


keepalive_requests 100;

Nginx服务器和用户端建立连接后,用户端通过此连接发送请求,keepalive_requests指令用于限制用户通过某一连接向Nginx服务器发送请求的次数。默认为100


include /etc/nginx/conf.d/*.conf;

设置http端配置包含/etc/nginx/conf.d目录中所有以".conf"文件结尾的配置文件,一般将server段配置文件放在该目录下。














本文出自 “zengestudy” 博客,谢绝转载!

2、Nginx配置文件nginx.conf的配置详解

标签:nginx配置文件详解

原文地址:http://zengestudy.blog.51cto.com/1702365/1769705

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!