再回首之Nginx(三)


引言

总感觉前两篇好像记下了些什么,好像又什么都没记录。

此文主要写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>&copy;2017&nbsp;Baidu&nbsp;<a href=http://www.baidu.com/duty/>使用百度前必读</a>&nbsp; <a href=http://jianyi.baidu.com/ class=cp-feedback>意见反馈</a>&nbsp;京ICP证030173号&nbsp; <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官方模块
  • 第三方模块
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个location配置块使用了多个 Nginx 模块...
    SkTj阅读 7,797评论 0 12
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 2,037评论 0 9
  • Nginx 的配置文件使用的就是一门微型的编程语言,许多真实世界里的 Nginx 配置文件其实就是一个一个的小程序...
    SkTj阅读 4,243评论 0 7
  • I/O模型: 阻塞型、非阻塞型、复用型、信号驱动型、异步 同步/异步:关注消息通知机制 消息通知:同步:等待对方返...
    Net夜风阅读 2,027评论 0 1
  • 1.简介:  Nginx:engine X ,2002年,开源,商业版 http协议:web服务器(类似于ht...
    尛尛大尹阅读 1,894评论 0 3