Nginx常用优化配置

Nginx | 配置 | 优化 | 代理

Posted by luoruiqing on January 1, 1999

配置项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 主配置 ===========================================================================================================================================
worker_processes 8; # 根据CPU的倍数设定

error_log /nginx_error.log crit;
# Nginx最大打开文件数
worker_rlimit_nofile 102400; #(ulimit -n)
# 事件模型 
events { 
    use epoll; # 选择
    worker_connections 102400; # worker * work_connections
    multi_accept on; # 高效模式,高并发开启
}
# 基本配置
http {
    keepalive_timeout 60; # 持续连接的超时时间
    client_header_buffer_size 2k; # 头部缓冲区大小,防止URL过长
    large_client_header_buffers 4 4k;
    open_file_cache max=102400 inactive=20s; # 文件缓存,max指定缓存数量,inactive缓存过期时间
    open_file_cache_min_uses 1; # inactive时间内不足使用次数的将被移除缓存
    open_file_cache_valid 30s; # 检查缓存有效性的间隔时间
    # 日志记录,增加时间格式化和请求耗时
    log_format detailed '$remote_addr - $remote_user [$time_iso8601] $request_time "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
}

# 子配置 ===========================================================================================================================================

server {
    # 基本信息 ----------------------------------------------------------------------------------------------------
    listen 14000;
    root /frontend/dist; # 静态文件目录
    # 日志
    access_log /frontend/dist/access.log detailed; # 成功日志
    error_log /frontend/dist/error.log warn; # 失败日志
    # 上传管理 ----------------------------------------------------------------------------------------------------
    client_max_body_size 100m; # 上传大小
    # 打开压缩 ----------------------------------------------------------------------------------------------------
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 9;
    gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    # 静态缓存 ----------------------------------------------------------------------------------------------------
    location ~* \.(ico|jpe?g|gif|png|bmp|swf|flv)$ {
        expires 30d;
        access_log off; # 关闭日志
    }
    # 反向代理 ----------------------------------------------------------------------------------------------------
    location ~* ^/(bridge|api)/(.*)$ {
        # 请求ID ----------------------------------------------------------------------------------------------------
        set $temp_request_id $http_x_request_id;
        if ($temp_request_id = "") { # 优先前端携带
            set $temp_request_id $request_id;
        }
        proxy_set_header x_request_id "";
        proxy_set_header X-Request-Id $temp_request_id;
        # 服务接口 ----------------------------------------------------------------------------------------------------
        rewrite /(bridge|api)/(.*) /$2 break; 
        proxy_pass http://localhost:9000; # 服务地址
    }
    # 前端主页 ----------------------------------------------------------------------------------------------------
    location / {
        try_files $uri $uri/ /index.html;
    }
}