Nginx 学习总结(2) —— 基本配置
文章目录
这是 Nginx 学习总结的第二篇,上一篇介绍到了 Nginx 学习总结(1) —— 概述,这一篇会对 Nginx 的基本配置做一些总结。
Nginx 配置信息主要分为六大部分: main(全局设置)、events(事件设置)、http(HTTP服务器设置)、 sever(虚拟主机设置)、location(URL匹配设置)、upstream(反向代理设置)。
main 模块
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 模块
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 模块
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 模块
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 模块
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 模块》。
upstram 模块
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) —— 负载均衡》。
参考文章:
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论