nginx 配置详解

2025-11-07 15:16:16

1、【nginx.conf基本组成】:

xxxxx

events

{

    xxxxx

}

http

{

    xxxxx

    server

    {

        xxxxx

    }

    server

    {

        xxxxx

    }

    xxxxx

}

nginx 配置详解

nginx 配置详解

nginx 配置详解

2、【头部】:user-是nginx进程用户名;worker_processes-是nginx工作进程数量,建议设置为cpu核心数量的2倍;pid-是nginx进程pid的存放文件。

nginx 配置详解

3、【events部分】:worker_connections是每个工作进程允许最大的同时连接数 量。

nginx 配置详解

4、【http部分】:主要是server虚拟主机部分的配置。以下详细讲解。

nginx 配置详解

5、【nginx虚拟主机】:

server

{

    listen 端口;

    server_name 192.168.1.166;

    ......

}

server_name可以是ip地址也可以是域名,可以添加多个用空格分开。

nginx 配置详解

6、【日志格式设置和路径】:

og_format设置日志格式

log_format name format [format ...]

name:定义格式名称

format:定义格式样式

默认格式:combined '$remote_addr - $remote_user [$time_local]' '"$request" $status $body_bytes_sent  ' '"$http_referer" "$http_user_agent"';

access_log 指定日志文件存放路径

access_log path [format [buffer=size | off]]

path:文件存放路径

format:log_format定义格式的名称

buffer:内存缓冲区大小

使用默认格式:access_log path;

使用缓冲加速日志文件读写:

open_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time] | off(默认)

max:缓存最大文件描述符数量,超过使用LRU算法淘汰

inactive:在inactive指定时间内文件描述符没有被使用则被自动删除,默认10秒

min_uses:在inactive指定时间内,文件描述符超过min_uses使用次数,则加入缓存,默认1

valid:查看变量指定的日志文件路径与文件是否存在的时间间隔,默认60秒

nginx 配置详解

7、【压缩输出】:

gzip压缩后变为原来的<=30%

配置:

gzip on;

gzip_min_length 1k;

gzip_buffers 4 16k;

gzip_http_version 1.1;

gzip_comp_level 2;

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

gzip_vary on;

nginx 配置详解

8、【自动列出目录和location】:

location / {

autoindex on;打开自动列出目录

autoindex_exact_size [on|off] 索引文件大少单位(B KB MB GB)

autoindex_localtime [on|off] 开启本地时间显示文件时间

}

nginx 配置详解

9、【浏览器本地缓存】:

expires [time|epoch|max|off]

time 时间值 负数不缓存

epoch 设定为1 january,1970, 00:00:01 GMT

max 设定为 31 December 2037 23:59:59 GMT

10、【php fastcgi】:

location ~ \.php$ {

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME $document_root/$fastcgi_script_name;

            include        fastcgi_params;

        }

nginx 配置详解

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢