这是 Nginx
学习总结的第二篇,上一篇介绍到了 Nginx 学习总结(1) —— 概述,这一篇会对 Nginx
的基本配置作一些总结。php
Nginx
配置信息主要分为六大部分: main(全局设置)
、events(事件设置)
、http(HTTP服务器设置)
、 sever(虚拟主机设置)
、location(URL匹配设置)
、upstream(反向代理设置)
。html
user nobody nobody; worker_processes auto; error_log /usr/local/var/log/nginx/error.log error; pid /usr/local/var/run/nginx/nginx.pid; worker_rlimit_nofile 1024;
user
指定 Worker 进程的运行用户以及用户组,默认由 nobody 运行;worker_processes
设置 Nginx 要开启的 Worker 进程数。最优值取决于许多因素,包括(但不限于)CPU 内核的数量、存储数据的硬盘数量及负载模式。不能肯定的时候,将其设置为可用的 CPU 内核数将是一个好的开始(设置为auto
将尝试自动检测它);error_log
指定全局错误日志的输出文件位置和错误级别。日志级别有 debug
、info
、notice
、warn
、error
、crit
、alert
、emerg
可供选择,错误级别依次递增。设置某个日志级别将记录指定的以及错误级别更高的全部消息(即 debug
输出日志最为最详细,而 emerg
输出日志最少)。默认开启为 error
。想要开启 debug
错误级别,须要在编译 Nginx 时指定 --with-debug
参数;pid
指定记录 PID 进程的存储文件位置( Nginx 向 Worker 进程发送信号时,须要知道是在向哪一个进程发送信息,而不一样的进程有不一样的 PID),用户仅需建立该空白文件便可;worker_rlimit_nofile
设置每一个 Worker 进程能够打开的最大文件数目(若是不设置该选项,则该选项的值为操做系统的限制值,使用“ulimit -a”命令可查看)。events { use epoll; worker_connections 1024; }
use
设置事件处理模型。Nginx 支持的事件处理模型有: select
、poll
、kqueue
、epoll
、rtsig
和 /dev/poll
,其中 kqueue(BSD 特有) 和 epoll(Linux 特有) 都是高效的事件处理模型。若是不设置,Nginx 将会自动选择一个最适合当前操做系统的事件处理模型;worker_connections
设置每一个 Worker 进程能并发处理的最大链接数。最大客户端链接数由 worker_processes
和 worker_connections
决定:Nginx 做为 HTTP 服务器时,Max_clients = worker_processes * worker_connections
;Nginx 做为反向代理时,Max_clients = worker_processes * worker_connections / 4
。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 /usr/local/var/log/nginx/access.log main; #access_log off sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; # gzip压缩功能设置 gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 6; client_max_body_size 10M; client_body_buffer_size 128k; # http_proxy 设置 proxy_connect_timeout 75; proxy_send_timeout 75; proxy_read_timeout 75; proxy_buffer_size 4k; proxy_buffers 4 32k; proxy_busy_buffers_size 64k; proxy_temp_file_write_size 64k; proxy_temp_path /usr/local/nginx/proxy_temp 1 2; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; }
include
加载 mime.type 文件,用于帮助 Nginx 识别文件的 MIME 类型;default_type
指定文件默认的 MIME 类型。例如,在没有配置 asp 的环境时,Nginx 是不予解析的,此时,浏览器访问 asp 文件就会出现下载了;log_format
设置日志的格式;access_log
设置 access_log 日志文件地址;sendfile
是否开启高效文件传输模式。将 tcp_nopush
和 tcp_nodelay
两个指令设置为 on 用于防止网络阻塞;keepalive_timeout
设置客户端链接保持活动的超时时间,在超过这个时间以后,服务器会关闭该链接;gzip
是否开启 gzip 压缩,这将会减小咱们发送的数据量;client_max_body_size
容许客户端请求的最大单文件字节数。若是上传较大文件,请增长它的限制值;client_body_buffer_size
容许客户端请求的最大缓冲区字节数;proxy_connect_timeout
设置与后端服务器链接超时时间。server { listen 80; server_name localhost 192.168.12.10 jochen.com; root /usr/www; index index.php index.html index.htm; charset utf-8; error_page 404 /404.html; error_page 403 /403.html; error_page 500 502 503 504 /50x.html; access_log usr/local/var/log/host.access.log main; aerror_log usr/local/var/log/host.error.log error; }
listen
设置监听端口,默认 80,小于 1024 的要以 root
用户启动。能够为 listen *:80
、listen 127.0.0.1:80
等形式;server_name
设置域名,多个域名之间用空格分开;root
设置虚拟主机的默认网站根目录;index
定义默认访问的文件名;charset
设置网页的默认编码格式;error_page
设置错误页面,相对于上面的 root
目录。location / { root /usr/share/nginx/html; index index.php index.html index.htm; } location ~ \.php$ { root /usr/share/nginx/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi.conf; } location /nginx-status { stub_status on; allow 192.168.10.100; allow 172.29.73.0/24; deny all; }
location
模块用于针对某些特定的 URL 进行配置,有关 location 匹配规则详情请见《Nginx 学习总结(3) —— location 模块》。node
upstream jochen.com { ip_hash; server 192.168.12.1:80; server 192.168.12.2:80 down; server 192.168.12.3:8080 max_fails=3 fail_timeout=20s; server 192.168.12.4:8080; }
upstream 模块用于负载均衡,有关 upstream 模块详情请见《Nginx 学习总结(7) —— 负载均衡》。nginx
参考文章:segmentfault