先看看配置文件的内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<code>user nginx;</code>
<code>worker_processes 4;</code>
<code>error_log </code><code>/var/log/nginx/error</code><code>.log warn;</code>
<code>pid </code><code>/var/run/nginx</code><code>.pid;</code>
<code>events {</code>
<code> </code><code>worker_connections 1024;</code>
<code>}</code>
<code>http {</code>
<code> </code><code>include </code><code>/etc/nginx/mime</code><code>.types;</code>
<code> </code><code>default_type application</code><code>/octet-stream</code><code>;</code>
<code> </code><code>log_format main </code><code>'$remote_addr - $remote_user [$time_local] "$request" '</code>
<code> </code><code>'$status $body_bytes_sent "$http_referer" '</code>
<code> </code><code>'"$http_user_agent" "$http_x_forwarded_for"'</code><code>;</code>
<code> </code><code>access_log </code><code>/var/log/nginx/access</code><code>.log main;</code>
<code> </code><code>sendfile on;</code>
<code> </code><code>#tcp_nopush on;</code>
<code> </code><code>keepalive_timeout 65;</code>
<code> </code><code>#gzip on;</code>
<code> </code><code>include </code><code>/etc/nginx/conf</code><code>.d/*.conf;</code>
Nginx的配置分为全局块、events块、http块和server块。在nginx.conf文件中只包含了全局块、events块和http块的内容,server块的配置需要自己定义。每一个server块都可以当做一个虚拟主机,一个http块可以包含多个server块,每一个server块的配置都是独立的,不会影响到其他server块。http全局块的配置对server块有效,但是如果server块中和http全局块的配置冲突,则采用就近原则,以server块的配置为准。下面来看看配置文件中这些指令的含义和其用法。
全局配置段:
<code>user nginx;</code>
user :指定可以运行Nginx服务的用户,只有被设置的用户或者用户组成员才有运行nginx进程的权限,如果希望所有用户都能运行Nginx进程,则可以注释掉该指令,或者设置为user nobody nonobody;
user指令用法:
user username [groupname];
<code>work_processes 4;</code>
work_processes:设置Nginx服务器运行时启动的进程数,理论上设置的值越大,则Nginx服务器能响应的请求数越多,但是由于受到服务器软硬件(CPU和磁盘驱动器)的限制,所以必须合理设置才行。
一般设置为与CPU核心数相等,或者为CPU核心数减去1,我的CPU为4核,所以我设置为4。
work_processes执行用法:
<code>work_processes number | auto;</code>
number为work_processes启动的最大processes数,设置为auto时,则Nginx服务器自动检测并设置。
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:指定Nginx服务器PID文件的存放路径。
pid用法:
<code>pid </code><code>file</code><code>;</code>
其他配置指令:
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配置段:
<code>worker_connections 1024;</code>
worker_connections:设置每一个work_process的最大并发连接数,默认为1024。其语法为:
<code>worker_connections number;</code>
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配置段:
<code>include </code><code>/etc/nginx/mime</code><code>.types;</code>
include:引入其他的Nginx配置或者第三方模块的配置到当前主配置文件中,语法为:
<code>include </code><code>file</code><code>;</code>
file为要引入的配置文件,支持相对路径。
<code>default_type application</code><code>/octet-stream</code><code>;</code>
default_type:用于指定处理前端请求的MIME类型。此指令还可以在http段、server段、或者location中配置
<code>log_format main </code><code>'$remote_addr - $remote_user [$time_local] "$request" '</code>
<code>access_log </code><code>/var/log/nginx/access</code><code>.log main;</code>
access_log:用于设置访问日志的路径
log_format:用于设置访问日志的格式,配置文件中的$remote_addr都是nginx的内置变量
<code>sendfile on;</code>
sendfile:用于设置是否启用sendfile功能,语法为:sendfile on | off;
sendfile_max_chunk_size;
设置sendfile传输的数据量最大值,如果设置为0,则不限制。
<code>keepalive_timeout 65;</code>
keepalive_timeout:设置长连接的会话保持时间,该指令还可以在server段和location中设置。其语法为:keepalive timeout [header_timeout];
keepalive_requests 100;
Nginx服务器和用户端建立连接后,用户端通过此连接发送请求,keepalive_requests指令用于限制用户通过某一连接向Nginx服务器发送请求的次数。默认为100
<code>include </code><code>/etc/nginx/conf</code><code>.d/*.conf;</code>
设置http端配置包含/etc/nginx/conf.d目录中所有以".conf"文件结尾的配置文件,一般将server段配置文件放在该目录下。
本文转自 曾哥最爱 51CTO博客,原文链接:http://blog.51cto.com/zengestudy/1769705,如需转载请自行联系原作者