Nginx常用功能配置一
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常用功能配置一的更多相关文章
- Nginx常用功能配置二
Nginx常用功能配置二 Nginx location匹配设置 location作用:可以根据用户请求的URI来执行不同的应用,根据用户请求的网站的地址URL匹配. location语法: locat ...
- nginx常用功能配置
一.规范优化nginx配置文件 nginx的主配置文件为nginx.conf,主配置文件包含的所有虚拟主机的子配置文件会统一放入extra目录中,虚拟主机的配置文件按照网站的域名或功能取名,例如www ...
- 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 ...
- 3.Nginx常用功能介绍
Nginx常用功能介绍 Nginx反向代理应用实例 反向代理(Reverse Proxy)方式是指通过代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器,并且从内部网络服 ...
- Nginx常用功能
3.Nginx常用功能 3.1 反向代理服务器 3.1.1.demo2 a.我在tomcat下部署了一个javaweb项目,tomcat安装的服务器IP为:192.168.37.136,部署的项目在t ...
- 2.了解nginx常用的配置
作者 微信:tangy8080 电子邮箱:914661180@qq.com 更新时间:2019-07-10 20:56:10 星期三 欢迎您订阅和分享我的订阅号,订阅号内会不定期分享一些我自己学习过程 ...
- 前端开发掌握nginx常用功能之rewrite
上一篇博文对nginx最常用功能的server及location的匹配规则进行了讲解,这也是nginx实现控制访问和反向代理的基础.掌握请求的匹配规则算是对nginx有了入门,但是这些往往还是不能满足 ...
- nginx常用服务配置
一.nginx.conf的配置方式,创建新vhost user nginx; worker_processes ; worker_cpu_affinity ; worker_rlimit_nofile ...
- Apache运维中常用功能配置笔记梳理
Apache 是一款使用量排名第一的 web 服务器,LAMP 中的 A 指的就是它.由于其开源.稳定.安全等特性而被广泛使用.下边记录了使用 Apache 以来经常用到的功能,做此梳理,作为日常运维 ...
随机推荐
- 2.4 webpack + gulp 构建完整前端工作流
在前面的两个小节中已经完整的讲了 webpack 和 gulp 相关的内容,本小节中将会结合二者构建一个完整的前端工作流,内容目录为: 前端工程结构和目标 前端工程目录结构 gulp clean gu ...
- ollydbg调试PE文件
ollydbg项目地址:http://www.ollydbg.de/ 将exe文件打开到ollydbg项目中,就会直接停到"入口点"地址处,通过View->Memory Ma ...
- linux学习的任督二脉-进程调度和内存管理
转自 宋宝华老师的博客原文:https://blog.csdn.net/21cnbao/article/details/77505330 内功心法 学习或遇到问题时,反过来主动思考如果我是设计者,我会 ...
- java中Date日期类型的大小比较
方法一:java.util.Date类实现了Comparable接口,可以直接调用Date的compareTo()方法来比较大小 String beginTime = "2018-07-28 ...
- Linux 进程间通信 有名管道(fifo)
有名管道特点: 1)无名管道只能用于具有亲缘关系的进程之间,这就限制了无名管道的使用范围 2)有名管道可以使互不相关的两个进程互相通信. 3)有名管道可以通过路径名来指出,并且在文件系统中可见,但内容 ...
- JS数组 选定元素slice() slice() 方法可从已有的数组中返回选定的元素。 语法 arrayObject.slice(start,end)
选定元素slice() slice() 方法可从已有的数组中返回选定的元素. 语法 arrayObject.slice(start,end) 参数说明: 1.返回一个新的数组,包含从 start 到 ...
- 【学术篇】规律选手再次证明自己(舒老师的胡策题 T2 LX还在迷路)
只要你不强制在线, 我就能分块. --Reflash 就算你强制在线, 我还是要分块. --Enzymii 今天做了一波舒老师的毒瘤题, T1据说很水但是没思路所以直接放掉了.. 去看了看T2好像可以 ...
- (PASS)什么是原子性和原子性操作?
什么是原子性操作呢? 下面我举一个例子来说明一下: A想要从自己的帐户中转1000块钱到B的帐户里.那么从A开始转帐,到转帐结束的这一个过程,称之为一个事务.在这个事务里,要做如下操作: 1. 从A的 ...
- Python语法学习记录之tuple该如何使用?
一.介绍 dict 的用法比较简单,它可以存储任意值,并允许是不同类型的值,下面实例来说明: 下面例子中 a 是整数, b 是字符串, c 是数组,这个例子充分说明哈希数组的适用性. 每一个元素是pa ...
- grep命令 一 文本搜索工具
使用正则表达式搜索文本,并把匹配的行打印出来.使用权限是所有用户. 基本使用 grep [option] pattern filename: pattern如果是表达式或者超过两个单词的, 需要用引号 ...