Nginx配置文件nginx.conf详解

2016年11月18日15:13:26 评论 6,775

本文整理了一些常用的nginx配置参数说明,结合自己的经验和网络内容。更详细的模块参数请参考官方文档:

http://wiki.nginx.org/Main

http://tengine.taobao.org/documentation_cn.html

0x01 公共模块

#定义nginx运行的用户和用户组

user www www;

#nginx进程数,不知道怎么设置就设为auto,由系统自动分配

worker_processes auto;

#一个nginx进程打开的最多文件描述符数目,建议与ulimit -n的值一致

worker_rlimit_nofile 65535;

#根据worker的数量自动配置cpu绑定位图,这个参数是tengine配置

worker_cpu_affinity auto;

#全局错误日志定义类型(debug、info、notice、warn、error、crit)

erroe_log logs/error.log  info;

#进程文件

pid   logs/nginx.pid;

0x02 events模块

events

{

#参考事件模型(kqueue、rtsig、epoll、/dev/poll、select、poll)

use epoll;

#单个进程最大连接数(最大连接数=连接数*进程数)

worker_connections 65535;

#性能优化,tengine配置参数

reuse_port on;

}

0x03 http模块

http

{

#文件扩展名与文件类型映射表

include   mime.types;

#默认文件类型

default_type  application/octet-stream;

#来自客户端请求头的headebuffer大小

client_header_buffer_size 32k;

#客户端请求中较大的消息头的缓存最大数量和大小

large_client_header_buffers 4 64k;

#上传文件大小限制

client_max_body_size 8m;

#服务器名字的hash表大小

server_names_hash_bucket_size 128;

#开启目录列表访问,合适下载服务器,默认关闭

autoindex on;

#日志格式设定

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"'

'"$upstream_cache_status"'

#开启高效文件传输模式,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载

sendfile   on;

#防止网络阻塞

tcp_nopush on;

#长连接超时时间,单位是秒

keepalive_timeout  65;

0x04 FastCGI模块

FastCGI相关参数是为了改善网站的性能:减少资源占用,提高访问速度

fastcgi_connect_timeout 300s;

fastcgi_send_timeout 300s;

fastcgi_read_timeout 300s;

fastcgi_buffer_size 128k;

fastcgi_buffers 8 128k;

fastcgi_busy_buffers_size 256k;

fastcgi_temp_file_write_size 256k;

#设置缓存目录,缓存时间1d和最大缓存大小1G

fastcgi_cache_path /cache1/wpcache levels=1:2 keys_zone=yangfannie:250m inactive=1d max_size=1G;

#缓存temp路径

fastcgi_temp_path /cache1/wpcache/temp;

#缓存cache_key,一定要加上$request_method

fastcgi_cache_key "$scheme$request_method$host$request_uri";

#定义哪些情况下用过期缓存

fastcgi_cache_use_stale error timeout invalid_header http_500;

#忽略一切nocache申明,避免不缓存伪静态等

fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

0x05 gzip模块

#开启gzip压缩输出

gzip on;

#最小压缩文件大小

gzip_min_length 1k;

#压缩缓冲区

gzip_buffers 4 16k;

#压缩版本(默认1.1,前端如果是squid2.5请使用1.0)

gzip_http_version 1.0;

#压缩等级

gzip_comp_level 2;

#压缩类型,默认就已经包含text/html

gzip_types text/plain application/x-javascript text/css application/xml;

0x06 upstream模块

upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。

具体详细配置可以参考之前写的一篇文章

nginx upstream的几种分配方式

0x07 server模块

server

{

#监听端口

listen 80;

#网站域名

server_name yangfannie.com;

index index.html index.htm index.php default.html default.htm default.php;

#网站存放路径

root  /xxx/yangfannie;

#nginx运行log

access_log /xxx/xxx/logs/yangfannie.log main;

#定义不缓存为0

set $no_cache 0;

#不缓存POST操作

if ($request_method = POST) {

set $no_cache 1;

}

#动态查询不缓存

if ($query_string != "") {

set $no_cache 1;

}

#后台特定页面不缓存

if ($request_uri ~* "/wp-admin/|/db-upload/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {

set $no_cache 1;

}

#已登录的不缓存(防止留言串号)

if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {

set $no_cache 1;

}

#自定义伪静态规则

location / {

try_files $uri $uri/ /index.php?$args;

#末尾/添加到 */wp-admin 请求

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

rewrite ^/sitemap.xml$ /sitemap.php last;

}

#

location ~ [^/]\.php(/|$)

{

try_files $uri =404;

fastcgi_pass unix:/tmp/php-cgi.sock;

fastcgi_index index.php;

include fastcgi.conf;

#新增的缓存规则

fastcgi_cache_bypass $no_cache;

fastcgi_no_cache $no_cache;

add_header X-Cache "$upstream_cache_status From $host";

fastcgi_cache yangfannie;

fastcgi_cache_valid 200 301 302 1d;

}

#文件格式缓存时间设置

location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {

access_log off; log_not_found off; expires max;

}

}

}
weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: