nginx 学习之反向代理(1)

1,327 阅读2分钟

查看nginx支持哪些模块

./configure --help | more
  --prefix=PATH                      set installation prefix
  --sbin-path=PATH                 set nginx binary pathname
  --modules-path=PATH                set modules path
  --conf-path=PATH                   set nginx.conf pathname
  --error-log-path=PATH              set error log pathname
  --pid-path=PATH                    set nginx.pid pathname
  --lock-path=PATH                   set nginx.lock pathname

  --with-http_geoip_module           enable ngx_http_geoip_module
  --with-http_geoip_module=dynamic   enable dynamic ngx_http_geoip_module
  --with-http_sub_module             enable ngx_http_sub_module
  --with-http_dav_module             enable ngx_http_dav_module

  --without-http_charset_module      disable ngx_http_charset_module
  --without-http_gzip_module         disable ngx_http_gzip_module
  --without-http_ssi_module          disable ngx_http_ssi_module
  --without-http_userid_module       disable ngx_http_userid_module
  --without-http_access_module       disable ngx_http_access_module

--prefix 表示安装在哪个目录 ,其他如下 ./configure 后参数 with表示增加的模块, without表示去掉默认将要安装的模块

配置不同的日志格式:

参数                      说明                                         示例
$remote_addr             客户端地址                                    211.28.65.253
$remote_user             客户端用户名称                                --
$time_local              访问时间和时区                                18/Jul/2012:17:00:01 +0800
$request                 请求的URI和HTTP协议                           "GET /article-10000.html HTTP/1.1"
$http_host               请求地址,即浏览器中你输入的地址(IP或域名)     www.wang.com 192.168.100.100
$status                  HTTP请求状态                                  200
$upstream_status         upstream状态                                  200
$body_bytes_sent         发送给客户端文件内容大小                        1547
$http_referer            url跳转来源                                   https://www.baidu.com/
$http_user_agent         用户终端浏览器等信息                           "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; SV1; GTB7.0; .NET4.0C;
$ssl_protocol            SSL协议版本                                   TLSv1
$ssl_cipher              交换数据中的算法                               RC4-SHA
$upstream_addr           后台upstream的地址,即真正提供服务的主机地址     10.10.10.100:80
$request_time            整个请求的总时间                               0.205
$upstream_response_time  请求过程中,upstream响应时间                    0.002

默认的日志规则

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

log_format后面的第一个参数main, 表示为日志格式的别名

你可以在http模块内部、server或location里面配置日志(日志格式指定为main或者定义其他日志格式)

access_log  logs/host.access.log  main;
    include       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"';

    log_format  test '$http_user_agent --test';

    access_log  logs/access.log  main;
    ...
    server {
        listen       80;
        server_name  localhost;
        access_log  logs/host.access.log  test;
        ...
    }

    ...

上面我们又新增了一个别名为test的日志格式

如果http和server都配置了access_log日志, 最里面会覆盖外面的access_log配置。

此时访问将会获得到test的配置日志格式模版

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.96 Safari/537.36 --test

关于proxy的几个指令用

location / {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass  http://10.10.11.11:8088;
        }

proxy_pass指令将转发到 http://10.10.11.11:8088

作为反向代理服务器,可能后面对应服务集群,这时我们使用upstream指令

http {
   upstream local {
      server 10.10.11.11:8088;
      server 10.10.11.12:80;
    }
    location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass  http://local;
    } 
}

proxy_pass指令配置转发为local, 将会寻找upstream local的配置server, 里面的server ip列表默认权重都为1, 采用轮循方式

proxy_set_header指令作用:

因为代理服务器,实际上游服务是拿不到客户端的header数据的,可以使用proxy_set_header来返回

使用proxy_cache指令加速性能(回源-nginx作为缓存服务器)

我们nginx作为反向代理,每次请求都会去转发到后端服务。但对于数据要求并不是严格一致、流量大的场景,我们可以在nginx代理层使用proxy_cache指令增加缓存加速我们的响应时间.

声明cache配置

proxy_cache_path /usr/local/nginx/nginxcache/test keys_zone=cache_test:10m levels=1:2 inactive=12d max_size=200m;

proxy_cache_path 缓存文件路径

levels 设置缓存文件目录层次;levels=1:2 表示两级目录

keys_zone 设置缓存名字和共享内存大小

inactive 在指定时间内没人访问则被删除

max_size 最大缓存空间,如果缓存空间满,默认覆盖掉缓存时间最长的资源。

缓存proxy_cache指令配置

location / {

          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_pass http:/xx.xx.xx.xx;

         #缓存配置
         proxy_cache cache_test; 
         #缓存key
         proxy_cache_key $host$uri;  
         #缓存状态码 缓存时长,如果要匹配所有状态码,改成any
         proxy_cache_valid 200 304 302 1d;
   }

但是这里有个问题,就是proxy_cache指令是受浏览器缓存头影响的,所以我们如果要强制缓存接口数据, 还需要配置如下(proxy_ignore_headers)

 proxy_ignore_headers Expires Cache-Control;
 proxy_cache cache_test;
 proxy_cache_key $host$uri;
 proxy_cache_valid 200 304 302 1d;

更多精彩内容关注 公众号: 呆呆熊一点通