一 ngx_http_proxy_module模块

1.1 proxy_pass配置

proxy_pass URL;
Context: location, if in location, limit_except
注意:proxy_pass后面的路径不带uri时,其会将location的uri传递给后端主机。
  1 server {
2 …
3 server_name HOSTNAME;
4 location /uri/ {
5 proxy http://hos[:port];
6 }
7 …
8 }

http://HOSTNAME/uri –> http://host/uri
proxy_pass后面的路径是一个uri时,其会将location的uri替换为proxy_pass的uri
  1 server {
2 …
3 server_name HOSTNAME;
4
5 location /uri/ {
6 proxy http://host/new_uri/;
7 }
8 …
9 }

http://HOSTNAME/uri/ –> http://host/new_uri/
如果location定义其uri时使用了正则表达式的模式,则proxy_pass之后必须不能使用uri; 用户请求时传递的uri将直接附加代理到的服务的之后
  1 server {
2 …
3 server_name HOSTNAME;
4 location ~|~* /uri/ {
5 proxy http://host;
6 }
7 …
8 }

1.2 proxy_set_header配置

proxy_set_header field value; #设定发往后端主机的请求报文的请求首部的值;
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
$remote_addr:记录的是上一台主机的IP,而上一台主机有可能也是代理服务器
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$proxy_add_x_forwarded_for:记录的是源IP地址

在http客户端还有修改/etc/httpd/conf/httpd.conf文件
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
通过上述方法则可以在后端主机上记录真实的httpd资源请求者,而不再是只记录前端代理服务器的IP地址

1.3 proxy_cache_path

proxy_cache_path #定义可用于proxy功能的缓存;Context: http
proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time]
[max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number]
[loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:1 keys_zone=gmtest:20M max_size=1G;

1.4 proxy_cache

proxy_cache zone | off; #指明要调用的缓存,或关闭缓存机制;
Context: http, server, location
proxy_cache gmtest;

1.5 proxy_cache_key

proxy_cache_key string; #缓存中用于“键”的内容;
默认值:proxy_cache_key $scheme$proxy_host$request_uri;
建议定义成方法和url

1.6 proxy_cache_valid

proxy_cache_valid [code …] time; #定义对特定响应码的响应内容的缓存时长;
定义在http{…}中;
proxy_cache_path /var/cache/nginx/proxy_cache levels=1:1:1 keys_zone=gmtest:20m max_size=1g;
定义在需要调用缓存功能的配置段,例如server{…},或者location中;
proxy_cache gmtest;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;

1.7 proxy_cache_use_stale

proxy_cache_use_stale
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403
| http_404 | off …;
Determines in which cases a stale cached response can be used when an error occurs during communication with the proxied server.
后端服务器的故障在那种情况下,就使用缓存的功能对客户的进行返回

1.8 proxy_cache_methods

proxy_cache_methods GET | HEAD | POST …;
If the client request method is listed in this directive then the response will be cached. “GET” and “HEAD” methods are always
added to the list, though it is recommended to specify them explicitly.
默认方法就是GET HEAD方法

1.9 proxy_hide_header

proxy_hide_header field;
By default, nginx does not pass the header fields “Date”, “Server”, “X-Pad”, and “X-Accel-…” from the response of a proxied server
to a client. The proxy_hide_header directive sets additional fields that will not be passed.

1.10 proxy_connect_timeout

proxy_connect_timeout time;
Defines a timeout for establishing a connection with a proxied server. It should be noted that this timeout cannot usually exceed 75
seconds.
默认为60s

1.11 buffer

buffer相关的配置
a:proxy_buffer_size size;
Sets the size of the buffer used for reading the first part of the response received from the proxied server. This part usually contains
a small response header. By default, the buffer size is equal to one memory page.
默认为4k|8k

b:proxy_buffering on | off;
Enables or disables buffering of responses from the proxied server.
默认为on

c:proxy_buffers number size;
Sets the number and size of the buffers used for reading a response from the proxied server, for a single connection. By default, the buffer size is equal to one memory page.
默认为8 4k|8k

d:proxy_busy_buffers_size size;
When buffering of responses from the proxied server is enabled, limits the total size of buffers that can be busy sending a response to the client while the response is not yet fully read.
默认为8k|16k

二 ngx_http_headers_module模块

The ngx_http_headers_module module allows adding the “Expires” and “Cache-Control” header fields, and arbitrary fields,
to a response header.
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值;

2.1 add_header

add_header name value [always]; #添加自定义首部;
add_header X-Via $server_addr; #经由的代理服务器地址
add_header X-Accel $server_name;

2.2 expires

expires [modified] time;
expires epoch | max | off;
用于定义Expire或Cache-Control首部的值;
可以把服务器定义的缓存时长修改了;

三 ngx_http_upstream_module模块

The ngx_http_upstream_module module is used to define groups of servers that can be referenced by the proxy_pass, fastcgi_pass,
uwsgi_pass, scgi_pass, and memcached_pass directives.

3.1 upstream

  1 upstream name { … }			#定义后端服务器组,会引入一个新的上下文;
2 Context: http
3 upstream httpdsrvs {
4 server …
5 server…
6 …
7 }

3.2 server address

server address [parameters];
在upstream上下文中server成员,以及相关的参数;Context: upstream
address的表示格式:
unix:/PATH/TO/SOME_SOCK_FILE
IP[:PORT]
HOSTNAME[:PORT]

parameters:
weight=number
权重,默认为1;默认算法是wrr

max_fails=number
失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用

fail_timeout=time
设置将服务器标记为不可用状态的超时时长

max_conns
当前的服务器的最大并发连接数

backup
将服务器标记为“备用”,即所有服务器均不可用时此服务器才启用

down
标记为“不可用”

先在nginx前端配置down,然后在下架后端服务器,上架新的web程序,然后上架,在修改配置文件立马的down

3.3 least_conn

least_conn;
最少连接调度算法,当server拥有不同的权重时其为wlc
要在后端服务器是长连接时,效果才好,比如mysql

3.4 ip_hash

ip_hash;
源地址hash调度方法

3.5 hash

hash key [consistent];
基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者的组合
作用:将请求分类,同一类请求将发往同一个upstream server
If the consistent parameter is specified the ketama consistent hashing method will be used instead.
示例:
hash $request_uri consistent;
hash $remote_addr;
hash $cookie_name; 对同一浏览器的请求,发往同一个upstream server

3.6 keepalive

keepalive connections;
为每个worker进程保留的空闲的长连接数量

四 ngx_stream_core_module模块

模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器

4.1 stream

stream { … }
定义stream相关的服务;Context: main
  1 stream {
2 upstream sshsrvs {
3 server 192.168.22.2:22;
4 server 192.168.22.3:22;
5 least_conn;
6 }
7 server {
8 listen 10.1.0.6:22022;
9 proxy_pass sshsrvs;
10 }
11 }
stream模块中管的upstream模块的用法同上

4.2 listen

listen
listen address:port [ssl] [udp] [proxy_protocol] [backlog=number] [bind] [ipv6only=on|off] [reuseport]
[so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];

附002.Nginx代理相关模块解析的更多相关文章

  1. 附002.Nginx全系列大总结

    Nginx全系列总结如下,后期不定期更新. 欢迎基于学习.交流目的的转载和分享,禁止任何商业盗用,同时希望能带上原文出处,尊重ITer的成果,也是尊重知识. 若发现任何错误或纰漏,留言反馈或右侧添加本 ...

  2. Nginx配置文件及模块解析

    一.Nginx是什么? Nginx是一个基于c语言开发的高性能http服务器及反向代理服务器.由俄罗斯的程序设计师Igor Sysoev所开发,官方测试nginx能够支支撑5万并发链接,并且cpu.内 ...

  3. 20、nginx之ngx_http_upstream_module模块

    nginx的ngx_http_upstream_module模块是用于nginx反向代理的,默认在安装nginx时已经被安装,ngx_http_upstream_module模块 的内容应放于 ngi ...

  4. Nginx防盗链、访问控制、解析PHP相关配置及Nginx代理

    6月11日任务 12.13 Nginx防盗链12.14 Nginx访问控制12.15 Nginx解析php相关配置12.16 Nginx代理 扩展502问题汇总 http://ask.apelearn ...

  5. Linux centos VMware Nginx防盗链、Nginx访问控制、Nginx解析php相关配置、Nginx代理

    一.Nginx防盗链 配置如下,可以和上面的配置结合起来 location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|x ...

  6. nginx防盗链、nginx访问控制、nginx解析php相关配制、nginx代理

    1.nginx防盗链编辑:vim /usr/local/nginx/conf/vhost/test.com.conf写入: location ~* ^.+\.(gif|jpg|png|swf|flv| ...

  7. Nginx相关模块学习使用实践指南

    转载自:https://www.bilibili.com/read/cv16150654?spm_id_from=333.999.0.0 0x01 Nginx 常用模块使用实践 官方模块使用手册:ht ...

  8. Nginx缓存了DNS解析造成后端不通--代理

    文章转载自:https://segmentfault.com/a/1190000022365954 1 问题现象 我们使用 Nginx 的时候,经常会用到 Proxy 功能,为了方便管理,后端站点或者 ...

  9. Nginx Rewrite相关功能-ngx_http_rewrite_module模块指令

    Nginx Rewrite相关功能-ngx_http_rewrite_module模块指令 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.

随机推荐

  1. java中“”==“” equals hashcode的关系

    ava中的数据类型,可分为两类: 1.基本数据类型,也称原始数据类型.byte,short,char,int,long,float,double,boolean 他们之间的比较,应用双等号(==),比 ...

  2. mock api测试demo

    前言 本测试demo基于Spring框架测试,这几个月也是刚刚接触Spring的项目.如果不对的地方请多谅解. 正文 1.创建测试类,添加注解 @RunWith(SpringRunner.class) ...

  3. caffe的python接口学习(6)用训练好的模型caffemodel分类新图片

    经过前面两篇博文的学习,我们已经训练好了一个caffemodel模型,并生成了一个deploy.prototxt文件,现在我们就利用这两个文件来对一个新的图片进行分类预测. 我们从mnist数据集的t ...

  4. NLP(一)

    “自然语言处理”(Natural Language Processing 简称 NLP)包含所有用计算机对自然语言进行的操作. 自然语言工具包(NLTK) 语言处理任务与相应 NLTK 模块以及功能描 ...

  5. CSS五种方式实现 Footer 置底

    页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部.当网页内容足够长以至超出浏览器可视高度时,页脚会随着内容被推到网页底部:但如果网页内容不够长,置底的页脚就会保 ...

  6. Python | 面试必问,线程与进程的区别,Python中如何创建多线程?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题第20篇文章,我们来聊聊Python当中的多线程. 其实关于元类还有很多种用法,比如说如何在元类当中设置参数啦,以及一 ...

  7. 使用Python编写的对拍程序

    简介 支持数据生成程序模式, 只要有RE或者WA的数据点, 就会停止 支持数据文件模式, 使用通配符指定输入文件, 将会对拍所有文件 结束后将会打印统计信息 第一次在某目录执行,将会通过交互方式获取配 ...

  8. 基于图嵌入的高斯混合变分自编码器的深度聚类(Deep Clustering by Gaussian Mixture Variational Autoencoders with Graph Embedding, DGG)

    基于图嵌入的高斯混合变分自编码器的深度聚类 Deep Clustering by Gaussian Mixture Variational Autoencoders with Graph Embedd ...

  9. webpack正式、测试环境接口地址本地运行及打包命令配置

    声明:本文由w3h5原创,转载请注明出处:<webpack正式.测试环境接口地址本地运行及打包命令配置> https://www.w3h5.com/post/521.html 为了方便开发 ...

  10. java动态代理基本原理及proxy源码分析一

    本系列文章主要是博主在学习spring aop的过程中了解到其使用了java动态代理,本着究根问底的态度,于是对java动态代理的本质原理做了一些研究,于是便有了这个系列的文章 为了尽快进入正题,这里 ...