Nginx相关配置
标签:Nginx

Nginx相关配置

1. 默认配置

vim /etc/nginx/nginx.conf

nginx.conf内容:

# 设置Nginx服务的系统使用用户
user  nginx;
# 工作进程数
worker_processes  1;

# Nginx的错误日志
error_log  /var/log/nginx/error.log warn;
# Nginx服务启动时候的pid
pid        /var/run/nginx.pid;


events {
    # 每个进程允许最大连接数,一般调节到10000个左右可以满足大部分需求
    worker_connections  1024;
}


http {
    # 包含Content-Type类型
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    # 服务端与客户端连接超时时间 65s
    keepalive_timeout  65;

    #gzip  on;
	# 包含相关默认配置文件
    include /etc/nginx/conf.d/*.conf;
}

2. 日志

2.1 Nginx日志类型

error.log: 记录Nginx处理http请求错误的状态,以及Nginx本身的错误状态,会按照不同的级别记录进去

access.log:记录每次http请求的访问状态,可以对Nginx访问进行分析

log_format

Syntax: log_format name [escape=default|json] string ...;

Default: log_format combined "..."

Context:http

2.2 Nginx变量

HTTP请求变量:arg_PARAMETER、http_HEADER、sent_http_HEADER

内置变量:Nginx内置的

自定义变量:自己定义的

修改nginx.conf 在main后面添加 '$http_user_agent'

 log_format  main  '$http_user_agent' '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

检查配置文件正确与否:

[root@localhost nginx]# nginx -t -c nginx.conf 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

nginx重新加载配置:

nginx -s reload -c nginx.conf 

查看日志:

[root@localhost nginx]# tail -n 200 /var/log/nginx/access.log
curl/7.29.0127.0.0.1 - - [15/Jun/2018:17:40:41 +0800] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"

可见每条日志前面出现了agent信息

  • 2 min read

CONTRIBUTORS


  • 2 min read