Nginx常用配置

重定向所有错误页面

1
2
3
4
5
6
error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 421 422 423 424 425 426 428 429 431 451 500 501 502 503 504 505 506 507 508 510 511 @error_page;

location @error_page {
    internal;
    return 404;
}

临时维护页面

1
2
3
4
5
rewrite ^(.*)$ /maintenance.html break;

location = /maintenance.html {
    root /var/www/html;
}

转发端口

1
2
3
4
5
6
stream {
    server {
        listen 2222;
        proxy_pass 192.168.1.1:22;
    }
}

隐藏版本号

1
server_tokens off;

日志输出格式

 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
log_format main escape=json
    '{'
        '"ngx": {'
            '"datetime": "$time_iso8601",'
            '"server": {'
                '"hostname": "$hostname",'
                '"name": "$server_name",'
                '"addr": "$server_addr",'
                '"port": "$server_port",'
                '"protocol": "$server_protocol"'
            '},'
            '"request": {'
                '"method": "$request_method",'
                '"scheme": "$scheme",'
                '"host": "$host",'
                '"request_uri": "$request_uri",'
                '"uri": "$uri",'
                '"args": "$args",'
                '"remote_addr": "$remote_addr",'
                '"remote_port": "$remote_port",'
                '"length": "$request_length",'
                '"time": "$request_time",'
                '"content_length": "$content_length",'
                '"content_type": "$content_type",'
                '"user_agent": "$http_user_agent",'
                '"referer": "$http_referer",'
                '"x_real_ip": "$http_x_real_ip",'
                '"x_forwarded_for": "$http_x_forwarded_for",'
                '"x_forwarded_proto": "$http_x_forwarded_proto",'
                '"x_scheme": "$http_x_scheme"'
            '},'
            '"response": {'
                '"bytes_sent": "$bytes_sent",'
                '"body_bytes_sent": "$body_bytes_sent",'
                '"status": "$status"'
            '},'
            '"upstream": {'
                '"addr": "$upstream_addr",'
                '"connect_time": "$upstream_connect_time",'
                '"header_time": "$upstream_header_time",'
                '"response_time": "$upstream_response_time",'
                '"cache_status": "$upstream_cache_status",'
                '"status": "$upstream_status"'
            '},'
            '"limit": {'
                '"conn_status": "$limit_conn_status",'
                '"req_status": "$limit_req_status"'
            '}'
        '}'
    '}';

access_log /var/log/nginx/default/access.log main;
error_log /var/log/nginx/default/error.log warn;

日志轮转

sudo vim /etc/logrotate.d/nginx

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/var/log/nginx/**/*.log {
        daily
        rotate 365
        missingok
        dateext
        compress
        delaycompress
        notifempty
        create 640 nginx adm
        sharedscripts
        postrotate
                if [ -f /var/run/nginx.pid ]; then
                        kill -USR1 `cat /var/run/nginx.pid`
                fi
        endscript
}
Licensed under CC BY-NC-SA 4.0
最后更新于 Feb 10, 2022 03:30 CST