博客
关于我
Nginx配置文件nginx.conf中文详解(总结)
阅读量:792 次
发布时间:2023-02-15

本文共 5564 字,大约阅读时间需要 18 分钟。

Nginx配置文件(nginx.conf)中文详解

Nginx作为一款高性能的 HTTP 和反向代理服务器,在Web应用中发挥着重要作用。本文将详细解析Nginx的核心配置参数,帮助开发者更好地理解和优化Nginx配置。

核心配置参数解析

1. 运行用户与进程数设置

user www www;worker_processes 8;
  • user www www;:设置Nginx运行的用户和用户组。建议根据实际权限设置合适的用户和组。
  • worker_processes 8;:设置Nginx进程数,建议设置为等于CPU核心数。例如,8核CPU设置为8。

2. 错误日志与进程文件

error_log ar/loginx/error.log info;pid ar/runinx.pid;
  • error_log ar/loginx/error.log info;:定义全局错误日志类型为info级别,日志文件路径为ar/loginx/error.log
  • pid ar/runinx.pid;:设置Nginx进程文件路径为ar/runinx.pid

3. 文件描述符与连接数管理

worker_rlimit_nofile 65535;worker_connections 65535;
  • worker_rlimit_nofile 65535;:设置每个Nginx进程可打开的最大文件描述符数目。建议与系统ulimit -n保持一致。
  • worker_connections 65535;:设置单个Nginx进程的最大连接数。最大连接数为连接数乘以进程数,建议设置为65535(根据实际负载情况调整)。

4. 工作模式与事件模型

events { use epoll; }
  • use epoll;:启用epoll事件模型,适用于Linux 2.6及以上版本,提供高性能的网络I/O处理。
  • worker_connections 65535;:单个进程的最大连接数,默认与worker_connections参数一致。

5. HTTP服务器配置

http {    include mime.types;    default_type application/octet-stream;    charset utf-8;    server_names_hash_bucket_size 128;    client_header_buffer_size 32k;    large_client_header_buffers 4 64k;    client_max_body_size 8m;    sendfile on;    autoindex on;    tcp_nopush on;    tcp_nodelay on;    keepalive_timeout 120;}
  • include mime.types;:加载文件扩展名与文件类型映射表。
  • default_type application/octet-stream;:默认文件类型为application/octet-stream
  • charset utf-8;:默认编码为UTF-8。
  • client_header_buffer_size 32k;:客户端请求缓冲区大小为32KB。
  • large_client_header_buffers 4 64k;:设置请求缓冲区,适用于处理大型请求头。
  • client_max_body_size 8m;:客户端请求最大单文件大小为8MB。
  • sendfile on;:启用高效文件传输模式。
  • autoindex on;:开启目录列表访问功能。
  • tcp_nopush on;:防止网络阻塞。
  • tcp_nodelay on;:防止网络延迟。
  • keepalive_timeout 120;:长连接超时时间为120秒。

6. FastCGI相关配置

fastcgi_connect_timeout 300;fastcgi_send_timeout 300;fastcgi_read_timeout 300;fastcgi_buffer_size 64k;fastcgi_buffers 4 64k;fastcgi_busy_buffers_size 128k;fastcgi_temp_file_write_size 128k;
  • fastcgi_connect_timeout 300;:FastCGI连接超时时间为300秒。
  • fastcgi_send_timeout 300;:FastCGI数据发送超时时间为300秒。
  • fastcgi_read_timeout 300;:FastCGI数据读取超时时间为300秒。
  • fastcgi_buffer_size 64k;:FastCGI缓冲区大小为64KB。
  • fastcgi_buffers 4 64k;:FastCGI缓冲区数量与大小。
  • fastcgi_busy_buffers_size 128k;:高负载下FastCGI缓冲区大小。
  • fastcgi_temp_file_write_size 128k;:临时文件写入大小。

7. Gzip压缩配置

gzip on;gzip_min_length 1k;gzip_buffers 4 16k;gzip_http_version 1.0;gzip_comp_level 2;gzip_types text/plain application/x-javascript text/css application/xml;gzip_vary on;
  • gzip on;:开启Gzip压缩。
  • gzip_min_length 1k;:最小压缩文件大小为1KB。
  • gzip_buffers 4 16k;:Gzip压缩缓冲区大小。
  • gzip_http_version 1.0;:Gzip压缩版本,建议设置为1.0(适用于Squid 2.5)。
  • gzip_comp_level 2;:压缩等级,2为最大压缩率。
  • gzip_types:定义压缩类型,包含常用文件类型。
  • gzip_vary on;:根据请求类型启用Gzip压缩。

8. 负载均衡配置

upstream blog.ha97.com {    server 192.168.80.121:80 weight=3;    server 192.168.80.122:80 weight=2;    server 192.168.80.123:80 weight=3;}
  • server 192.168.80.121:80 weight=3;:定义后端服务器,权重为3。
  • server 192.168.80.122:80 weight=2;:定义后端服务器,权重为2。
  • server 192.168.80.123:80 weight=3;:定义后端服务器,权重为3。

9. 反向代理配置

server {    listen 80;    server_name www.ha97.com ha97.com;    index index.html index.htm index.php;    root /data/www/ha97;    location ~ .*.(php|php5)?$ {        fastcgi_pass 127.0.0.1:9000;        fastcgi_index index.php;        include fastcgi.conf;    }    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {        expires 10d;    }    location ~ .*.(js|css)?$ {        expires 1h;    }    location /NginxStatus {        stub_status on;        access_log on;        auth_basic "NginxStatus";        auth_basic_user_file confpasswd;    }    location ~ .(jsp|jspx|do)?$ {        proxy_set_header Host $host;        proxy_set_header X-Real-IP $remote_addr;        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;        proxy_pass http://127.0.0.1:8080;    }    location ~ .*.(htm| |gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {        expires 15d;    }    location ~ .*.(js|css)?$ {        expires 1h;    }}
  • listen 80;:监听端口80,处理HTTP流量。
  • server_name www.ha97.com ha97.com;:定义服务器名称,可支持多个域名。
  • index index.html index.htm index.php;:默认索引文件。
  • root /data/www/ha97;:设置物理路径。
  • location ~ .*.(php|php5)?$ { ... }:处理PHP请求,通过FastCGI代理。
  • location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 10d; }:图片缓存时间为10天。
  • location ~ .*.(js|css)?$ { expires 1h; }:JS和CSS缓存时间为1小时。
  • location /NginxStatus { ... }:设置Nginx状态监控页面。
  • location ~ .(jsp|jspx|do)?$ { ... }:反向代理Tomcat或Resin处理动态页面。
  • location ~ .*.(htm| |gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; }:静态文件缓存时间为15天。

10. 状态监控与反向代理

location / {    proxy_pass http://127.0.0.1:88;    proxy_redirect off;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_set_header Host $host;}
  • proxy_pass http://127.0.0.1:88;:反向代理请求至后端服务器。
  • proxy_redirect off;:禁止重定向。
  • proxy_set_header X-Real-IP $remote_addr;:设置真实IP地址。
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;:设置X-Forwarded-For头。
  • proxy_set_header Host $host;:设置请求的主机头。

11. 动态静态分离

location ~ .(jsp|jspx|do)?$ {    proxy_set_header Host $host;    proxy_set_header X-Real-IP $remote_addr;    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;    proxy_pass http://127.0.0.1:8080;}location ~ .*.(htm| |gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {    expires 15d;}location ~ .*.(js|css)?$ {    expires 1h;}
  • location ~ .(jsp|jspx|do)?$ { ... }:动态页面反向代理。
  • location ~ .*.(htm| |gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ { expires 15d; }:静态文件缓存时间为15天。
  • location ~ .*.(js|css)?$ { expires 1h; }:JS和CSS缓存时间为1小时。

总结

以上是Nginx配置文件的详细解析,涵盖了从核心配置到实际应用场景的各个方面。通过合理配置Nginx,可以显著提升Web服务器的性能和稳定性。如果需要更深入的模块参数说明,可以参考Nginx官方文档或相关技术博客。

转载地址:http://nujfk.baihongyu.com/

你可能感兴趣的文章
Netty源码—3.Reactor线程模型四
查看>>
Netty源码—4.客户端接入流程一
查看>>
Netty源码—4.客户端接入流程二
查看>>
Netty源码—5.Pipeline和Handler一
查看>>
Netty源码—5.Pipeline和Handler二
查看>>
Netty源码—6.ByteBuf原理一
查看>>
Netty源码—6.ByteBuf原理二
查看>>
Netty源码—7.ByteBuf原理三
查看>>
Netty源码—7.ByteBuf原理四
查看>>
Netty源码—8.编解码原理一
查看>>
Netty源码—8.编解码原理二
查看>>
Netty源码解读
查看>>
netty的HelloWorld演示
查看>>
Netty的Socket编程详解-搭建服务端与客户端并进行数据传输
查看>>
Netty的网络框架差点让我一夜秃头,哭了
查看>>
Netty相关
查看>>
Netty简介
查看>>
Netty线程模型理解
查看>>
netty解决tcp粘包和拆包问题
查看>>
Netty速成:基础+入门+中级+高级+源码架构+行业应用
查看>>