nginx配置与优化


如果你的nginx是通过yum安装的,那么nginx的配置文件默认在/etc/下的nginx下的二级子目录里,主配置文件名为nginx.conf。
如果你用编译安装nginx并且安装在了/usr/local/下,那么nginx的配置文件应该在/usr/local/nginx/conf/nginx.conf。下面我们可以看看nginx的主配置文件都写了些什么。
我会以–开头给出我的注释,请不要与#注释混淆。

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
--使用的用户和用户组
worker_processes auto;
--指定工作衍生进程数,默认配置文件这里是auto。一般来说应等于CPU的总核数或总核数的二倍,两个四核CPU,总核数就是8
error_log /var/log/nginx/error.log;
--指定错误日志存放路径
pid /run/nginx.pid;
--指定pid存放路径

# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
--这里省略了使用的网络I/O模型,Linux默认采用epoll模型,
    worker_connections 1024;
    --允许连接数
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

事实上nginx的配置文件主要包括两部分

############
events {
############
}
############

http {
############
    server {
############
        }
 ############
}

nginx的虚拟主机配置

虚拟主机就是把一台服务器分割成一台台虚拟的主机,每台主机都可以提供不同的web服务。虚拟主机可以基于IP或域名进行区分。
以前写过,这里就不再写了。

nginx日志文件配置

与日志相关的配置有两条,一条是log_format,用来设置日志格式;另一条是access_log,用来指定日志存放路径。我们来看在nginx.conf里的默认配置。

    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;

指定日志格式

首先介绍一下log_format中各项含义

$remote_addr记录IP地址
$remote_user记录远程客户端用户名称
$time_local记录访问时间与时区
$request记录请求URL与http协议
$status记录请求状态
$body_bytes_sent记录发送给客户端的文件主体内容大小
$http_referer记录是从哪个页面链接访问过来的
$http_user_agent记录客户端浏览器的相关信息
$http_x_forwarded_for记录IP地址

细心的童鞋可能发现了,有两个记录IP地址的,它们显然不是把同一条信息写两遍
假设本服务器作为web服务器,在生产环境中客户端想要访问到本主机很可能经过反向代理中间层,这时我们获取的第一个IP就不再是客户端的IP而是反向代理服务器的IP了,但是反向代理服务器可以在转发请求的HTTP头信息里记录原来的客户端IP地址和原来的客户端请求的服务器地址。

指定日志存放路径

默认的日志路径设置在这里

    access_log  /var/log/nginx/access.log  main;

如果想关闭日志功能,可以:

    access_log  off;

日志路径可以包含变量:

    access_log  /var/log/nginx/$server_name.log  main;

nginx日志文件切割

生产环境下的web服务器,日志的增长速度非常快,我们最好对日志按天切割。

mv access.log 20190913.log

事实上这里有一个有趣的现象,如果我执行了上述命令并且nginx继续提供服务,那么新的日志信息会被写到哪里呢?是重新生成一个access.log吗。日志信息会继续写到20190913.log里,这时我们需要通知应用程序重新加载配置文件,也就是:

ps aux|grep nginx
root       7381  0.0  0.1 122916  2112 ?        Ss   08:12   0:00 nginx: master process /usr/sbin/nginx
nginx      7382  0.0  0.1 123304  3600 ?        S    08:12   0:00 nginx: worker process
root       7387  0.0  0.0 112724   988 pts/0    R+   08:15   0:00 grep --color=auto nginx
kill -USR1 7381

要想在生产环境中实现日志分割,我们还需要cron的帮助。
首先写好日志分割的shell脚本(这里只给出最基础的脚本,请自行优化)

#!/bin/bash
pid=`ps aux|grep nginx|grep master|awk '{print $2}'`
date=`date '+%F'`
mv /var/log/nginx/access.log /var/log/nginx/"$date".log
kill -USR1 "$pid"

设置一个定时任务,在每天00:00自动执行此脚本

0 0 * * * sh /var/shell/nginx_log_cut.sh

nginx的目录列出配置

Apache默认显示当前页面下的目录,而nginx默认不显示目录,当然我们只需要修改主配置文件一个参数就可以实现nginx相同功能

        location / {
                autoindex       on;
                autoindex_exact_size    on;
                autoindex_localtime     on;
        }
autoindex显示目录nginx默认不显示目录
autoindex_exact_size设置索引时文件大小的单位默认为on,精确显示文件大小,单位为bytes;改为off时会显示文件的大概大小,单位是KB,MB,GB
autoindex_localtime以本地时间显示文件时间默认为off,显示GMT时间,修改为on后显示文件的服务器时间
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值