Nginx|Nginx 学习(二).Nginx 基础配置文件详解

Nginx 学习(二).Nginx 基础配置文件详解 1.配置文件总览

##### ==============分界线==================== #usernobody; ##### ==============分界线==================== worker_processes1; ##### ==============分界线====================#error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; ##### ==============分界线==================== events { worker_connections1024; }##### ==============分界线==================== http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namelocalhost; #charset koi8-r; #access_loglogs/host.access.logmain; location / { roothtml; indexindex.html index.htm; }#error_page404/404.html; # redirect server error pages to the static page /50x.html # error_page500 502 503 504/50x.html; location = /50x.html { roothtml; }# proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { #proxy_passhttp://127.0.0.1; #}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { #roothtml; #fastcgi_pass127.0.0.1:9000; #fastcgi_indexindex.php; #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name; #includefastcgi_params; #}# deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { #denyall; #} }# another virtual host using mix of IP-, name-, and port-based configuration # #server { #listen8000; #listensomename:8080; #server_namesomenamealiasanother.alias; #location / { #roothtml; #indexindex.html index.htm; #} #}# HTTPS server # #server { #listen443 ssl; #server_namelocalhost; #ssl_certificatecert.pem; #ssl_certificate_keycert.key; #ssl_session_cacheshared:SSL:1m; #ssl_session_timeout5m; #ssl_ciphersHIGH:!aNULL:!MD5; #ssl_prefer_server_cipherson; #location / { #roothtml; #indexindex.html index.htm; #} #} } ##### ==============分界线====================

接下来是对配置文件中配置项一个个讲解
2.启动用户 默认情况下 worker进程是以nobody用户的身份运行的,如果要指定 ,就需要分开注释,使用user 指令去指定用户;
## 默认配置 ###usernobody; ### 自定义 usernginx;

2.工作进程 worker process (多进程模型) 配置文件
### 默认配置 worker process 一个线程 worker_processes1; ### 自定义 #worker_ cpu_ affinity #worker_cpu_affinity 1000 0100 0010 0001;

2.1 worker_processes
在我们启动nginx 后,会看到两个nginx 进程在运行
[root@localhost ~]# ps -ef |grep nginx root2411810 10:40 ?00:00:00 nginx: master process /package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf nobody24119241180 10:40 ?00:00:00 nginx: worker process root24123240770 10:40 pts/200:00:00 grep --color=auto nginx

一个是 master process 另一个 worker process
master process : 只有一个;主要是用来读取配置文件,校验配置文件;reload 时重新加载配置文件,开启 worker process
worker process : 处理请求响应; 在配置文件中可以配置多个 ;
worker processes的值一般设置为CPU核心数;避免worker 抢占CPU, 也可以设置为 worker_ processes auto,自动检测核心并启动相同数量的worker 进程;
以我本机为例
#### 配置 worker_ processes auto [root@localhost ~]# ps -ef |grep nginx root2411810 10:40 ?00:00:00 nginx: master process /package/nginx/sbin/nginx -c /package/nginx/conf/nginx.conf nginx25653241180 11:10 ?00:00:00 nginx: worker process nginx25654241180 11:10 ?00:00:00 nginx: worker process nginx25655241180 11:10 ?00:00:00 nginx: worker process nginx25656241180 11:10 ?00:00:00 nginx: worker process root25662240770 11:10 pts/200:00:00 grep --color=auto nginx

2.2 worker_ cpu_ affinity
CPU 和对应的worker process 绑定; 比如我CPU 有四个 ,每个核心对应的就是 1000,0100,0010,0001 ; 八个核心就是8 位的二进制表示
3.日志文件,PID 文件
##默认配置不修改就在安装目录 logs 下 #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid;

[root@localhost logs]# pwd /package/nginx/logs [root@localhost logs]# ls access.logerror.lognginx.pid

4. 连接数 每个worker process 最大处理的连接数量
### 默认配置 events { worker_connections1024; }

5. http协议转发 【Nginx|Nginx 学习(二).Nginx 基础配置文件详解】这里先简单的说一下 下一章节来说一下这个转发协议的具体配置; 一个server 表示一个虚拟主机
http { #### mime.types; http里面的头记录文件类型 对应文件后缀 includemime.types; ###mime.types; 中没有记录的数据 就流的形式传输 default_typeapplication/octet-stream; ### 零拷贝是否开启 sendfileon; ###保持连接 keepalive_timeout65; ### 表示一个主机, 可以配置多个主机 相互不影响; server { ### 监听端口 和 监听主机名/域名 listen80; server_namelocalhost; #URL http://localhost:xx/cccc/aaa/bbb #URI /cccc/aaa/bbb ### 匹配URI location / { ## 匹配上的主目录 roothtml; ### 目录下的文件 indexindex.html index.htm; }# 异常转发 错误页面 error_page500 502 503 504/50x.html; location = /50x.html { roothtml; } } }

    推荐阅读