Nginx 的基本配置与优化
修改主配置文件nginx.conf
#使用的用户和组
#user nobody;
#指定工作衍生进程数(一般等于CPU的总核数或者总核数的两倍,例如一个八核CPU)
worker_processes 8;
#指定错误日志存放的路径,错误日志级别可选项为:[debug|info|notice|warn|error|crit]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#指定 pid存放的路径
#pid logs/nginx.pid;
#指定文件描述符数量
worker_rlimit_nofile 51200;
events {
#允许的连接数(默认为1024,可自行根据实际情况配置)
worker_connections 1024;
}
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 logs/access.log main;
#设置客户端能够上传的文件大小(可根据实际情况配置)
client_max_body_size 8m;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#开启gzip压缩
gzip off;
# 启用gzip压缩的最小文件,小于设置值的文件将不会压缩
gzip_min_length 1k;
# gzip 压缩级别,1-9,数字越大压缩的越好,也越占用CPU时间,后面会有详细说明
gzip_comp_level 1;
# 进行压缩的文件类型。javascript有多种形式。其中的值可以在 mime.types 文件中找到。
gzip_types text/plain application/x-javascript text/css application/xml;
# 是否在http header中添加Vary: Accept-Encoding,建议开启
gzip_vary on;
# 禁用IE 6
gzip gzip_disable "MSIE [1-6]\.";
# 设置压缩所需要的缓冲区大小
gzip_buffers 32 4k;
# 设置gzip压缩针对的HTTP协议版本
gzip_http_version 1.0|1.1
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#配置浏览器本地缓存设置(可根据网站实际情况配置)
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 1h;
}
}
}
#使用