Nginx常用功能配置

参数include配置

说明:如果日常工作中server标签存在太多,可以采用include配置模式,Nginx的主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,如果过虚拟主机的数量不多,也可以把多个虚拟主机配置成一个单独的配置文件,仅仅和Nginx的主配置文件nginx.conf分离开即可。

注意:include里包含每个server,所以每个server的conf配置文件需要在include的配置文件下,例如:www.conf 的配置文件在/application/nginx/conf/extra下,而nginx.conf在/application/nginx/conf/下

具体操作步骤

###创建文件放置目录###
mkdir /application/nginx/conf/extra -p

###编辑nginx.conf,把server模块全部删除,然后添加include模块###
cd /application/nginx/conf/
vim nginx.conf
worker_processes 1;
events {
    worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

include extra/www.conf;
    include extra/bbs.conf;
    include extra/blog.conf;  #(这三个可缩减为include extra/*.conf,此操作执行后,以后新添加的虚拟主机配置,则不必每次进行修改添加)
}

###分别生成bbs.conf,www.conf,blog.conf,三个虚拟主机文件###
cd /application/nginx/conf/extra

vim www.conf

server {
    listen 80;
    server_name www.etiantian.org;
    location / {
    root html/www;
    index index.html index.htm;
  }
}

vim bbs.conf

server {
    listen 80;
    server_name bbs.etiantian.org;
    location / {
    root html/bbs;
    index index.html index.htm;
  }
}

vim blog.conf

server {
    listen 80;
    server_name blog.etiantian.org;
    location / {
    root html/blog;
    index index.html index.htm;
  }
}

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

###在其他客户端上先进行hosts解析###
vim /etc/hosts
172.16.1.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org

###hosts解析完后,然后在客户端上验证是否生效###
curl www.etiantian.org
www
curl bbs.etiantian.org
bbs
curl blog.etiantian.org
blog

配置Nginx status

这个模块主要是记录Nginx的基本访问状态信息,让使用者了解nginx的工作状态

具体操作步骤

###检查编译安装时设置的参数,是否存在--with-http_stub_status_module,这个模块需要在编译Nginx时进行安装###
../sbin/nginx -V

###配置status.conf###
cat >>/application/nginx/conf/extra/status.conf<<EOF
##status
server {
    listen 80;
    server_name status.etiantian.org;
    location / {
    stub_status on;
    access_log off;
    }
}
EOF

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

###需要在window端加一行域名解析###
在C:\windows\System32\drivers\etc下找到hosts文件进行添加域名解析
10.0.0.8 status.etiantian.org

禁止和允许的IP设置

可以用location的方式实现状态信息配置,例如在任意一个虚拟主机里,为server标签增加如下配置:

例如:
location /nginx_status {
    stub_status on;
    access_log off;
    allow 10.0.0.0/24; #(设置允许的IP端访问)
    deny all; #(设置禁止的IP端访问)
}

nginx配置错误日志(error_log)

简介:nginx会把自身运行的故障信息及用户访问的日志信息记录到指定的日志文件里。

关键字error_log不能改变,日志文件可以指定任意存放日志的目录,错误日志基本常见的又【debug|inof|notice|warn|error|crit|alert|emerg】,级别越高记录的信息越少,生产场景一般是warn|error|crit这三个级别之一,注意不要配置info等较低级别,会带来巨大的磁盘I/O消耗。

注意:编辑该配置文件:error_log logs/error.log;#(一般配置这一行就可以)配置在主的nginx.conf

具体操作步骤

cd /application/nginx/conf/
vim nginx.conf
worker_processes 1;
error_log logs/error.log;#(一般配置这一行就可以)
events {
worker_connections 1024;
}
http {
...
}

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

nginx配置访问日志(access_log)

简介:Nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户浏览行为;此功能由ngx_http_log_module模块负责,log_format 表示用什么格式记录访问日志,("$http_referer"可以查看从上一级目录从哪里来的地址),对应官方地址为:http://nginx.org/en/docs/http/ngx_http_log_module.html

具体配置说明:

第一步:在nginx的的主配置文件中加入log_format main格式

编辑该配置文件:/application/nginx/conf/nginx.conf放入:
log_format main '$remote_addr - $remote_user [$time_local] "$requset" '
                           '$status $body_bytes_sent "$http_referer" '
                           '"$http_user_agent" "$http_x_forwarded_for" ';

配合:access_log logs/access.log main;进行使用

第二步:配置对应虚拟机的配置文件

cd /application/nginx/conf/extra/
vim www.conf
server{
      listen 80;
      server_name www.etiantian.org etiantian.org;
      location / {
          root html/www;
          index index.html index.htm;
      }
      access_log logs/www_access.log main;(logs/www_access.log表示记录名,mian表示使用上述log_format的记录格式)
}

具体操作步骤

###配置主配置nginx.conf文件###
cd /application/nginx/conf/
vim nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
     worker_connections 1024;
}
http {
    include mime.types;
    default_type application/octet-stream;
    sendfile on;
    keepalive_timeout 65;

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

include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
include extra/status.conf;
}

###然后配置每个虚拟主机使用上述格式记录,www.conf,blog.conf,bbs.conf###
cd /application/nginx/conf/extra/
vim www.conf
server{
    listen 80;
    server_name www.etiantian.org ;
    location / {
        root html/www;
        ndex index.html index.htm;
    }
    access_log logs/www_access.log main; #(logs/www_access.log表示记录名,mian表示使用上述log_format的记录格式)
    }

vim blog.conf
server{
    listen 80;
    server_name blog.etiantian.org ;
    location / {
        root html/blog;
        index index.html index.htm;
    }
    access_log logs/blog_access.log main;
    }

vim bbs.conf
server{
    listen 80;
    server_name bbs.etiantian.org ;
    location / {
        root html/bbs;
        index index.html index.htm;
    }
    access_log logs/bbs_access.log main;
    }

###检查nginx语法并重启###
../../sbin/nginx -t
../../sbin/nginx -s reload

优化access.log

说明:默认情况Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于分默认情况Nginx会把所有的访问日志生成到一个指定的访问日志文件access.log里,但这样一来,时间长了就会导致日志个头很大,不利于分析,所有可以对access.log进行轮询切割保存。

具体操作步骤

####首先创建一个存放脚本的文件夹###
mkdir -p /server/scripts
cd /server/scripts

###定义一个Nginx轮询切割脚本###
vim /server/script/cut_nginx_log.sh

#!/bin/sh
Dateformat=`date +%Y%a%d`
Basedir="/application/nginx"
Nginxlogdir="$Basedir/logs"
Logname="access_www"
[ -d $Nginxlogdir ] && cd $Nginxlogdir||exit l
[ -f ${Logname}.log ]||exit l
/bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
$Basedir/sbin/nginx -s reload

###定时进行轮询操作nginx脚本###
cat >>/var/spool/cron/root <<EOF

#cut nginx access log by oldboy
00 00 * * * /bin/sh /server/script/cut_nginx_log.sh >/dev/null 2>&1
EOF

###查看crontab配置###

crontabl -l

Nginx常用功能配置一的更多相关文章

  1. Nginx常用功能配置二

    Nginx常用功能配置二 Nginx location匹配设置 location作用:可以根据用户请求的URI来执行不同的应用,根据用户请求的网站的地址URL匹配. location语法: locat ...

  2. nginx常用功能配置

    一.规范优化nginx配置文件 nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www ...

  3. nginx常用功能和配置

    nginx常用功能和配置 1.nginx常用功能和配置 1.1 限流 1.2 压力测试工具--Ab 1.2.1安装 1.2.2 测试 1.2.3 返回值 1.3 limit_conn_zone 1.4 ...

  4. 3.Nginx常用功能介绍

    Nginx常用功能介绍 Nginx反向代理应用实例 反向代理(Reverse Proxy)方式是指通过代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并且从内部网络服 ...

  5. Nginx常用功能

    3.Nginx常用功能 3.1 反向代理服务器 3.1.1.demo2 a.我在tomcat下部署了一个javaweb项目,tomcat安装的服务器IP为:192.168.37.136,部署的项目在t ...

  6. 2.了解nginx常用的配置

    作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...

  7. 前端开发掌握nginx常用功能之rewrite

    上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础.掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足 ...

  8. nginx常用服务配置

    一.nginx.conf的配置方式,创建新vhost user nginx; worker_processes ; worker_cpu_affinity ; worker_rlimit_nofile ...

  9. Apache运维中常用功能配置笔记梳理

    Apache 是一款使用量排名第一的 web 服务器,LAMP 中的 A 指的就是它.由于其开源.稳定.安全等特性而被广泛使用.下边记录了使用 Apache 以来经常用到的功能,做此梳理,作为日常运维 ...

随机推荐

  1. Centos7 安装 telnet 服务

    准备写一个 django-webtelnet(运维管理系统集成后管理网络设备),但是手边没有现成的网络设备资源可以测试,那就研究下 Centos7 下安装 telnet-server 吧. 安装 yu ...

  2. C++——代码风格

    google代码风格 1.使用安全的分配器(allocator),如scoped_ptr,scoped_array 2.测试用的,其他的不能用: 2.1 友元 2.2 C++异常 2.3 RTTI 3 ...

  3. 机器学习技法笔记:Homework #5 特征变换&Soft-Margin SVM相关习题

    原文地址:https://www.jianshu.com/p/6bf801bdc644 特征变换 问题描述 程序实现 # coding: utf-8 import numpy as np from c ...

  4. 拾遗:btrfs

    #扫描 btrfs 文件系统btrfs device scan btrfs device scan /dev/sda #创建子卷或快照 btrfs subvolume create /mnt/btrf ...

  5. hdu6319 Ascending Rating /// 单调队列

    题目大意: 给定n m k,p q r mod, 给出序列的前k项 之后的按公式 a[i]=(p×a[i-1]+q×i+r)%mod 递推 求序列中每个长度为m的连续区间的 该区间最大值与第一位的位置 ...

  6. 无法CREATE UNIQUE INDEX;找到重复的关键字

  7. java访问https绕过证书信任

    package com.xing.test; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  8. 使用sql对比Mysql中数据库2个表结构

    比较两个数据表的结构 SELECT column_name, max( CASE WHEN table_name = 'table1' AND table_schema = 'db1' THEN 'Y ...

  9. h5 app 设置全屏

    h5 app的全屏和沉浸式状态栏是不一样的 全屏模式 常见使用场景:如果页面是全屏游戏,一般会直接让状态栏消失,也就是页面全屏.webview高度全屏了,状态栏没有了.写法: 终端支持:没有终端类型限 ...

  10. vue tabNav 点击

    <template> <div class="content"> <header class="tab_nav"> < ...