Nginx(二)
利用include功能优化nginx的配置文件
- [root@lnmp conf]# cat nginx.conf
- worker_processes 1;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- #nginx vhosts config
- include extra/www.conf;
- include extra/bbs.conf;
- include extra/blog.conf;
- }
写配置文件
- [root@lnmp conf]# mkdir extra
- [root@lnmp conf]# sed -n '18,25p' nginx.conf.base-name >extra/bbs.conf
- [root@lnmp conf]# cat extra/bbs.conf
- server {
- listen 80;
- server_name bbs.etiantian.org;
- location / {
- root html/bbs;
- index index.html index.htm;
- }
- }
- [root@lnmp conf]# sed -n '26,33p' nginx.conf.base-name >extra/blog.conf
- [root@lnmp conf]# cat extra/blog.conf
- server {
- listen 80;
- server_name blog.etiantian.org;
- location / {
- root html/blog;
- index index.html index.htm;
- }
- }
- [root@lnmp conf]# cat extra/www.conf
- server {
- listen 80;
- server_name www.etiantian.org;
- location / {
- root html/www;
- index index.html index.htm;
- }
- }
重启服务
- [root@lnmp conf]# /application/nginx/sbin/nginx -t
- nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
- nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
- [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
测试
- [root@lnmp conf]# curl www.etiantian.org
- www.etiantian.org
- [root@lnmp conf]# curl bbs.etiantian.org
- bbs.etiantian.org
- [root@lnmp conf]# curl blog.etiantian.org
- blog.etiantian.org
nginx虚拟主机别名的配置
- [root@lnmp conf]# cat extra/www.conf
- server {
- listen 80;
- server_name www.etiantian.org etiantian.org;
- location / {
- root html/www;
- index index.html index.htm;
- }
- }
- [root@lnmp conf]# cat /etc/hosts
- 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
- ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
- 192.168.31.132 server
- 192.168.31.133 lamp
- 192.168.31.134 lnmp www.etiantian.org bbs.etiantian.org blog.etiantian.org etiantian.org
- 192.168.31.136 backup
- [root@lnmp conf]# /application/nginx/sbin/nginx -t
- nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
- nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
- [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
- [root@lnmp conf]# curl etiantian.org
- www.etiantian.org
- [root@lnmp conf]# curl www.etiantian.org
- www.etiantian.org
启动报错:
- [root@lnmp ~]# /application/nginx/sbin/nginx -s reload
- nginx: [error] invalid PID number "" in "/application/nginx-1.6.3/logs/nginx.pid"
解决办法:
- [root@lnmp ~]# /application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
- [root@lnmp ~]# /application/nginx/sbin/nginx -s reload
nginx状态信息配置
- [root@lnmp extra]# cat status.conf
- #status
- server{
- listen 80;
- server_name status.etiantian.org;
- location / {
- stub_status on;
- access_log off;
- }
- }
添加包含
- include extra/status.conf;
重启nginx,浏览器访问

第一个server表示nginx启动到现在共处理了多少个连接。
第二个accepts表示nginx启动到现在共创建了多少次握手,请求丢失次数=(握手次数-连接次数)。
第三个handled request表示总共处理了多少次请求。
Reading:nginx读取到客户端的header信息数。
writing:nginx返回给客户端的header信息素。
waiting:nginx已经处理完正在等候下一次请求指令的驻留连接,开启keep-alive的情况下,这个值等于active-(reading+writing)。
nginx错误日志
常见的错误日志级别有[debug|info|notice|warn|error|crit|alert|emerg],级别越高记录的信息越少,生产场景一般是warm|error|crit这三个级别之一,注意不要配置info等级较低的级别,会带来磁盘I/O消耗。
error_log的默认值为:error_log logs/error.log error
- [root@lnmp conf]# cat nginx.conf
- worker_processes 1;
- error_log logs/error.log error;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- #nginx vhosts config
- include extra/www.conf;
- include extra/bbs.conf;
- include extra/blog.conf;
- }
可以放置的标签段为:main,http,server,location。
访问日志
log_format:用来定义记录日志的格式(可以定义多种日志格式,取不同名字即可)。
access_log:用来指定日志文件的路径及使用的何种日志格式记录日志。
- [root@lnmp conf]# cat nginx.conf
- worker_processes 1;
- error_log logs/error.log error;
- 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"';
- #nginx vhosts config
- include extra/www.conf;
- include extra/bbs.conf;
- include extra/blog.conf;
- }
$remote_addr:记录访问网站的客户端地址。
$http_x_forwarded_for:当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器上也要进行相关的x_forwarded_for设置。
$remote_user:远程客户端用户名称。
$time_local:记录访问时间与时区。
$request:用户的http请求起始行信息。
$status:http状态码,记录请求返回的状态,例如:200、404、301等。
$body_bytes_sent:服务器发送给客户端的响应body字节数。
$http_referer:记录此次请求是从哪个链接访问过来的,可以根据referer进行防盗链设置。
$http_user_agent:记录客户端访问信息,例如:浏览器、手机客户端等。
access_log off;这里的off表示不记录访问日志。
默认配置:access_log logs/access.log combined;
放置位置:http,server,location,if in location,limit_except。
- [root@lnmp conf]# cat extra/www.conf
- server {
- listen 80;
- server_name www.etiantian.org etiantian.org;
- location / {
- root html/www;
- index index.html index.htm;
- }
- access_log logs/access_www.log main;
- }
重启nginx
- [root@lnmp conf]# /application/nginx/sbin/nginx -t
- nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
- nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful
- [root@lnmp conf]# /application/nginx/sbin/nginx -s reload
- [root@lnmp conf]# tail -f ../logs/access_www.log #访问日志
- 192.168.31.1 - - [16/Feb/2017:23:35:38 +0800] "GET / HTTP/1.1" 200 18 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"
- 192.168.31.1 - - [16/Feb/2017:23:35:38 +0800] "GET /favicon.ico HTTP/1.1" 404 570 "http://www.etiantian.org/" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36" "-"
日志轮询
- [root@lnmp scripts]# cat cut_nginx_log.sh
- #!/bin/sh
- Dateformat=`date +%Y%m%d`
- Basedir="/application/nginx"
- Nginxlogdir="$Basedir/logs"
- Logname="access_www"
- [ -d $Nginxlogdir ] && cd $Nginxlogdir||exit 1
- [ -f ${Logname}.log ]||exit 1
- /bin/mv ${Logname}.log ${Dateformat}_${Logname}.log
- $Basedir/sbin/nginx -s reload
执行
- [root@lnmp scripts]# sh cut_nginx_log.sh
- [root@lnmp scripts]# ll /application/nginx/logs/
- total 24
- -rw-r--r--. 1 root root 978 Feb 16 23:35 20170216_access_www.log
设置定时任务
- #nginx log cut
- 00 00 * * * /bin/sh /server/scripts/cut_nginx_log.sh >/dev/null 2>&1
nginx常用日志收集及分析工具有rsyslog、awstats、flume、ELK、storm等。
rewrite
rewrite指令语法
指令语法:rewrite regex replacement [flag];
默认值:none
应用位置:server,location,if。
- [root@lnmp conf]# cat extra/www.conf
- server {
- listen 80;
- server_name etiantian.org;
- rewrite ^/(.*) http://www.etiantian.org/$1 permanent;
- }
- server {
- listen 80;
- server_name www.etiantian.org;
- location / {
- root html/www;
- index index.html index.htm;
- }
- access_log logs/access_www.log main;
- }
创建oldboy.html然后进行访问
- [root@lnmp www]# curl etiantian.org/oldboy.html -I
- HTTP/1.1 301 Moved Permanently
- Server: nginx/1.6.3
- Date: Thu, 16 Feb 2017 16:30:47 GMT
- Content-Type: text/html
- Content-Length: 184
- Connection: keep-alive
- Location: http://www.etiantian.org/oldboy.html
rewrite指令最后一项参数flag标记说明
last:本条规则匹配完成后,继续向下匹配新的location URI规则。
break:本条规则匹配完成即终止。不再匹配后面的任何规则。
redirect:返回302临时重定向,浏览器地址栏会显示跳转后的URL地址。
permanent:返回301永久重定向,浏览器地址栏会显示后的URL地址。
Nginx(二)的更多相关文章
- nginx(二)----ubuntu14.04下启动或重启和关闭nginx
/** * lihaibo * 文章内容都是根据自己工作情况实践得出. *如有错误,请指正 *转载请注明出处 */ 一.启动 /usr/local/nginx/sbin/nginx或者cd /usr/ ...
- 网关中间件-Nginx(二)
网关中间件-Nginx(一) 第一部分我们主要介绍如下几点: 1.nginx的基本概念 2.nginx结合业务场景实现负载均衡 3.常见问题的举例 这一部分主要介绍Nginx中限流,缓存,动静分离,以 ...
- Nginx (二) Nginx的反向代理负载均衡以及日志切割
Nginx是一个高并发,高性能的服务器,可以进行反向代理以及网站的负载均衡.这些功能的运用都在配置文件中,也就是Nginx安装目录下的conf/nginx.conf. nginx.conf 1. 先来 ...
- 快速掌握Nginx(二) —— Nginx的Location和Rewrite
1 location详解 1.location匹配规则 Nginx中location的作用是根据Url来决定怎么处理用户请求(转发请求给其他服务器处理或者查找本地文件进行处理).location支持正 ...
- nginx(二)nginx的安装
下载 nginx官网下载地址 把源码解压缩之后,在终端里运行如下命令: ./configure make make install 默认情况下,Nginx 会被安装在 /usr/local/nginx ...
- Nginx(二) nginx 无法启动
有时候在客户端输入:nginx 但是终端会输出以下,显示启动失败 nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already i ...
- Nginx(二):虚拟主机配置
什么是虚拟主机? 虚拟主机使用的是特殊的软硬件技术,它把一台运行在因特网上的服务器主机分成一台台“虚拟”的主机,每台虚拟主机都可以是一个独立的网站,可以具有独立的域名,具有完整的Intemet服务器功 ...
- 《nginx 二》深入理解nginx的各项配置
Nginx应用场景 1.http服务器.Nginx是一个http服务可以独立提供http服务.可以做网页静态服务器. 2.虚拟主机.可以实现在一台服务器虚拟出多个网站,例如个人网站使用的虚拟机. 3. ...
- Nginx(二) 反向代理&负载均衡
1.反向代理 当我们请求一个网站时,nginx会决定由哪台服务器提供服务,就是反向代理. nginx只做请求的转发,后台有多个tomcat服务器提供服务,nginx的功能就是把请求转发给后面的服务器, ...
- nginx(二)
nginx rewrite Nginx提供的全局变量或自己设置的变量,结合正则表达式和标志位实现url重写以及重定向.rewrite只能放在server{},location{},if{}中,并且 ...
随机推荐
- 学习笔记TF033:实现ResNet
ResNet(Residual Neural Network),微软研究院 Kaiming He等4名华人提出.通过Residual Unit训练152层深神经网络,ILSVRC 2015比赛冠军,3 ...
- Oauth2.0客户端服务端示例
https://blog.csdn.net/qq_28165595/article/details/80459185 前言前面的理解OAuth2.0认证与客户端授权码模式详解,我们大致了解了Oauth ...
- VUE中如何优雅的动态绑定长按事件
答案没有: 图片是从后端传过来, 加到imgTarget属性,实现长按点击删除该图片 let img = document.createElement('img'); img.src = " ...
- Python NLP完整项目实战教程(1)
一.前言 打算写一个系列的关于自然语言处理技术的文章<Python NLP完整项目实战>,本文算是系列文章的起始篇,为了能够有效集合实际应用场景,避免为了学习而学习,考虑结合一个具体的项目 ...
- 关于SpringMVC
SpringMVC 原理:1.用户发送请求给服务器.url:user.do2.服务器收到请求.发现DispatchServlet可以处理.于是调用DispatchServlet.3.DispatchS ...
- LeetCode——727.Minimum Window Subsequence
一.题目链接:https://leetcode.com/problems/minimum-window-substring/ 二.题目大意: 给定两个字符串S和T,要求从S中找出包含T中所有字母的最短 ...
- rpm软件包、yum软件仓库、systemd初始化进程
rpm软件包.yum软件仓库.systemd初始化进程 作者:Eric 微信:loveoracle11g 红帽软件包管理器rpm (Redhat Package Manager) RPM会建立统一的数 ...
- Container 、Injection
Container: Linux容器作为一类操作系统层面的虚拟化技术成果,旨在立足于单一Linux主机交付多套隔离性Linux环境.与虚拟机不同,容器系统并不需要运行特定的访客操作系统.相反,容器共享 ...
- Java虚拟机------JVM内存区域
JVM内存区域运行时数据区域分为两种: JVM内存区域 运行时数据区域分为两种: 线程隔离的数据区: 程序计数器 Java虚拟机栈 本地方法栈 所有线程程共享的数据区: Java堆 方法区 JVM 内 ...
- day28元类与异常查找
元类与异常处理1. 什么是异常处理 异常是错误发生的信号,一旦程序出错就会产生一个异常,如果该异常 没有被应用程序处理,那么该异常就会抛出来,程序的执行也随之终止 异常包含三个部分: ...