1、Nginx采用的是编译安装,安装目录在/apps/nginx/,Nginx的配置文件nginx.conf位于其安装目录的conf目录下,/apps/nginx/conf/nginx.conf
Nginx的配置⽂件的组成部分: 主配置⽂件:nginx.conf,⼦配置⽂件 include conf.d/*.conf。Nginx.conf由多个块组成,依次是全局配置,Events和HTTP,HTTP包含upstream和多个Server,Server又包含多个location。过滤掉注释,空行,安装之后的默认配置如下所示:
2、全局配置端
全局配置端,对全局⽣效,主要设置nginx的启动⽤⼾/组,启动的⼯作进程数量,⼯作模式,Nginx的PID路径,⽇志路径等。
2.1 user nginx nginx; #启动Nginx⼯作进程的⽤⼾和组
2.2 worker_processes [number | auto]; #启动Nginx⼯作进程的数量,数量由cpu决定,四核就会开启4个工作进程。可以写数字1~4,也可以auto自动检测,例如虚拟机四核,我选的是auto自动模式,查看进程如下:一个主进程,四个工作进程
worker_cpu_affinity #将Nginx⼯作进程绑定到指定的CPU核⼼,默认Nginx是不进⾏进程绑定的,绑定并不是意味着当前nginx进程独占以⼀核⼼CPU,但是可以保证此进程不会运⾏在其他核⼼上,这就极⼤减少了nginx的⼯作进程在不同的cpu核⼼上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。四核可以进行如下绑定,worker_cpu_affinity 0001 0010 0100 1000。当然nginx的官方文档也支持auto自动绑定。
绑定之前:
选择 worker_cpu_affinity 0001 0010 0100 1000 绑定之后:
2.3 错误日志
#错误⽇志记录配置,语法:error_log file [debug | info | notice | warn | error | crit |alert | emerg]
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log /apps/nginx/logs/error.log error;
2.4 pid⽂件保存路径 pid /apps/nginx/logs/nginx.pid;
2.5 worker_rlimit_nofile
worker_rlimit_nofile 65536; #这个数字包括Nginx的所有连接(例如与代理服务器的连接等),⽽不仅仅是与客⼾端的连接,另⼀个考虑因素是实际的并发连接数不能超过系统级别的最⼤打开⽂件数的限制.这个值一般调大点
3、events事件模型配置
events设置快,主要影响nginx服务器与⽤⼾的⽹络连接,⽐如是否允许同时接受多个⽹络连接,使⽤哪种事件驱动模型处理请求,每个⼯作进程可以同时⽀持的最⼤连接数,是否开启对多⼯作进程下的⽹络连接进⾏序列化等
3.1 worker_connections 65536
设置单个nginx⼯作进程可以接受的最⼤并发,作为web服务器的时候最⼤并发数为worker_connections * worker_processes,作为反向代理的时候为(worker_connections *worker_processes)/2
3.2 use epoll;
使⽤epoll事件驱动(默认就是),Nginx⽀持众多的事件驱动,⽐如select、poll、epoll,只能设置在events模块中设置。比如使用select模块,就可以写成use selec,如果出现错误可能是编译安装的时候没有安装select模块,需要重新编译安装
3.3 accept_mutex on; #优化同⼀时刻只有⼀个请求⽽避免多个睡眠进程被唤醒的设置,on为防⽌被同时唤醒默认为off,全部唤醒的过程也成为"惊群",因此nginx刚安装完以后要进⾏适当的优化。
3.4 multi_accept on; Nginx服务器的每个⼯作进程可以同时接受多个新的⽹络连接,但是需要在配置⽂件中配置,此指令默认为关闭,即默认为⼀个⼯作进程只能⼀次接受⼀个新的⽹络连接,打开后⼏个同时接受多个。
4、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; #在开启了sendfile的情况下,合并请求后统⼀发送给客⼾端。
#tcp_nodelay off; #在开启了keepalived模式下的连接是否启⽤TCP_NODELAY选项,当为off时,延迟0.2s发送,默认On时,不延迟发送,⽴即发送⽤⼾相应报⽂。
#keepalive_timeout 0;
keepalive_timeout 65 65; #设置会话保持时间
#gzip on; #开启⽂件压缩
server {
listen 80; #设置监听地址和端⼝
server_name localhost; #设置server name,可以以空格隔开写多个并⽀持正则表达式,如*.xxxx.com www.xxxxx.* www.(site\d+)\.xxxxx\.com$ default_server
#charset koi8-r; #设置编码格式,默认是俄语格式,可以改为utf-8
charset utf-8;
#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;
} #可以自定义404错误界面,如下:
#error_page 404 /404.html; #定义错误⻚⾯
#location = /404.html {
#root html; } 然后自己再做一个404的html文件就好了
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ { #以http的⽅式转发php请求到指定web服务器
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ { #以fastcgi的⽅式转发php请求到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 { #拒绝web形式访问指定⽂件,如很多的⽹站都是通过.htaccess⽂件来改变⾃⼰
的重定向等功能。
# deny all;
#}
location ~ /passwd.html {
deny all;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server { #⾃定义虚拟server
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm; #指定默认⽹⻚⽂件,此指令由ngx_http_index_module模
块提供
# }
#}
# HTTPS server
#
#server { #https服务器配置
# 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;
# }
location /linux38/passwd.ht {
deny all;
}
#}