引言
总感觉前两篇好像记下了些什么,好像又什么都没记录。
此文主要写Nginx的主要配置,但还是以梳理为先。
梳理基础
1.HTTP请求
Nginx作为Web Server或是HTTP代理,处理的都是HTTP的请求。
request
请求报文包括请求行、请求头部、请求数据response
相应报文包括状态行、消息报头、相应正文
我们来模拟一个请求
我们使用curl命令,curl可以理解成一个浏览器
curl www.baidu.com
如果想看到请求报文信息,使用如下
curl -v www.baidu.com
信息如下:
* About to connect() to www.baidu.com port 80 (#0)
* Trying 220.181.112.244... connected
* Connected to www.baidu.com (220.181.112.244) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: www.baidu.com
> Accept: */*
>
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
< Connection: Keep-Alive
< Content-Length: 2381
< Content-Type: text/html
< Date: Sat, 05 May 2018 03:50:17 GMT
< Etag: "588604c8-94d"
< Last-Modified: Mon, 23 Jan 2017 13:27:36 GMT
< Pragma: no-cache
< Server: bfe/1.0.8.18
< Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/
<
<!DOCTYPE html>
<!--STATUS OK--><html> <head><meta http-equiv=content-type content=text/html;charset=utf-8><meta http-equiv=X-UA-Compatible content=IE=Edge><meta content=always name=referrer><link rel=stylesheet type=text/css href=http://s1.bdstatic.com/r/www/cache/bdorz/baidu.min.css><title>百度一下,你就知道</title></head> <body link=#0000cc> <div id=wrapper> <div id=head> <div class=head_wrapper> <div class=s_form> <div class=s_form_wrapper> <div id=lg> <img hidefocus=true src=//www.baidu.com/img/bd_logo1.png width=270 height=129> </div> <form id=form name=f action=//www.baidu.com/s class=fm> <input type=hidden name=bdorz_come value=1> <input type=hidden name=ie value=utf-8> <input type=hidden name=f value=8> <input type=hidden name=rsv_bp value=1> <input type=hidden name=rsv_idx value=1> <input type=hidden name=tn value=baidu><span class="bg s_ipt_wr"><p id=cp>©2017 Baidu <a href=http://www.baidu.com/duty/>使用百度前必读</a> <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a> 京ICP证030173号 <img src=//www.baidu.com/img/gs.gif> </p> </div> </div> </div> </body> </html>
* Connection #0 to host www.baidu.com left intact
* Closing connection #0
如果要把返回的信息重定向到linux上的一个空设备上面。
curl -v www.baidu.com >/dev/null
2.Nginx日志类型
包括:
- error.log
主要记录了nginx处理http请求的错误状态 - access.log
主要记录了nginx的每个http请求访问的状态,为了分析每次访问的请求和客户端的交互,以及对行为的分析
log_format
Syntax:log_format name [escape=default|json] string...
Default:log_format combined "...";
Context:http
示例:
vim etc/nginx/nginx.conf
具体配置信息:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
3.Nginx变量
http请求变量 -arg_PARAMETER、http_Header、sent_http_HEADER
内置变量 -Nginx内置的
参考地址自定义变量 -自己定义
示例:
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"';
#检查nginx.conf是否有语法错误
nginx -t -c /etc/nginx/nginx.conf
#重启nginx
nginx -s reload -c /etc/nginx/nginx.conf
#注意nginx -s 命令只能用于停止和重启,不能用于start
nginx -s stop
4.Nginx模块
- Nginx官方模块
- 第三方模块