nginx代理

正向代理

  正向代理主要是代理服务器接收客户端的请求,转发给外网中真正的目标服务器,正向代理接收内网中客户端的请求,主要由Nginx ngx_http_proxy_module 模块提供。

例:

server {
    listen 80 default_server;
    resolver 8.8.8.8
    location / {
        proxy_pass http://$host$request_uri;
    }
}  

参数说明:

1) resolver
    该指令用于指定 DNS 服务器的IP地址。

2) resolver_timeount
    该指令用于设置 DNS 服务器域名解析的超时时间。

3) proxy_pass
    该指令用于设置代理服务器的协议和地址。

反向代理

    反向代理 作用是用来让外网的客户端接入局域王忠的站点以访问站点中的资源。

例:

server {
    listen 80;
    server_name www.test.com
    location / {
        proxy_pass http://127.0.0.1:8080/;
        proxy_set_header Host $host;
        proxy_ser_header X-real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }  
    
}

ngx_http_proxy_module

  该模块允许将请求传递给另一台服务器。

proxy_pass

    设置代理服务器的协议和地址及相应映射位置。

例:

location /admin/ {
    proxy_pass http://172.16.0.12;
}

注意:
    1) 如果  proxy_pass 后面的路径没有 URI 时候,其会将 location 定义的 URI 传递给后端主机。
    2) 如果  proxy_pass 后面的路径有 URI 的时候,其会直接放为 proxy_pass 中的url。
    3)  如果没有 URI 后面尽量别以 / 号结尾。
    4) 如果 location 定义其 URI 时使用了正则表达式的模式,或者 if 语句或者 limt_execept 中使用 proxy_pass 指令,则 proxy_pass 之后必须不能使用 URI ,用户请求时传递的 URI 将直接附加到代理服务器之后。
location ~|~* /admin/{
	proxy http://host;
}

proxy_set_header

    设定发往后端的主机请求报文的请求首部的值。

Context: http,server,location

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

前者获取后端真实IP适用于一层代理,后者可获取多层代理的客户端的真实IP。

log_ormat: "$http_x_real_ip" "$http_x_forwarded_for"

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 /tmp/nginx/cache levels=1:2 keys_zone=my_cache:10m max_size=10g

proxy_cache

    指明要使用的缓存. keys_zone 指定的名字.

proxy_cache_path my_cache;

proxy_cache_min_uses

     指定时间内如果几次未命中则删除缓存项.

proxy_cache_min_uses 3;

proxy_cache_key

    缓存使用的"键" 的内容.

默认值: proxy_cache_key $scheme $proxy_host $request_url

proxy_cache_valid

    定义对特定的响应码的相应内容的缓存时长.

proxy_cache_path /var/cache/nginx/proxy_cache levels=1:2:2 keys_zone=pxycache:20m max_siz=20g;

proxy_cache pxycache;
proxy_cache_key $request_url;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;

proxy_cache_use_stale

    当作为cache的NGINX收到源站返回error、timeout或者其他指定的5XX错误,并且在其缓存中有请求文件的陈旧版本,则会将这些陈旧版本的文件而不是错误信息发送给客户端。

proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;

proxy_cache_methods

  该指令用于设置缓存哪些HTTP方法,默认缓存HTTP GET/HEAD方法,不缓存HTTP POST 方法.。

Default: proxy_cache_methods GET HEAD;

proxy_hide_header

    隐藏主机头部信息.

proxy_connect_timeout

    该指令设置与upstream server的连接超时时间

proxy_connect_timeout 30;

proxy_read_timeout

    该指令设置与代理服务器的读超时时间。

proxy_read_timeout 30;

proxy_sed_timeout

这个指定设置了发送请求给upstream服务器的超时时间

proxy_send_timeout 30;

ngx_http_headers_module

    定义由代理服务器相应给客户端的报文添加自定义首部,或者修改首部的值.

add_header

add_header X-Via $server_addr;

ngx_http_upstream_module

    此模块主要实现 nginx 的七层负载, 用于多个服务器定义成一个组,由 proxy_pass,fastcgi_pass等参数进行引用.

upstream

    定义一个后端服务器组,引入一个新的上下文,只能在 http 上下文中配置.

例:

upstream web1 {
    server 192.168.0.1;
    server 192.168.0.2;
}

 

server

    定义后端主机.及相关参数.

语法:  server address [parameters]; 

参数:

参数   说明  
welght     权重,可根据服务器性能适当调整权重值大小,默认为1.    
max_fails  失败尝试的最大次数
fail_timeout 后端服务器的连接超时时长.
max_conns 连接后端服务器的最大并发活动连接数,1.11.5 以后版本支持.   
backup 将服务器标记为备用状态,所有服务器 都 down机才会启用.不能和ip hashi 一起使用.
down   标记为不可用,配合 ip_hash 使用,实现灰色发布.

 

调度算法

  默认调度算法是 wrr(轮循).

ip_hash

  源地址 hash 算法. 能够将来自同一个 源地址的请求发往同一个 upstream server.

列:

upstream backend {
    ip_hash;
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com down;
    server backend4.example.com;
}

least_conn

    最少连接数调度算法, 当 server 拥有不同的权重时为 wlc. 当所有后端主机的连接数相同时则使用 wrr 进行调度.

列:

upstream lb_demo {
        least_conn;
        server 172.16.255.194:9001;
        server 172.16.255.195:9001;
}

url_hash

    按照访问的 url 的 hash 结果分配请求, 实现可以做到针对url进行哈希算法式的负载均衡转发。

列:

upstream lb_demo {
        url_hash;
        server 172.16.255.194:9001;
        server 172.16.255.195:9001;
}

hash

   基于指定的 key 的hash 表实现请求调度. 此处的 key 可以使用文本,变量,或者二者结合.

列:

示例:
	hash $request_url conslstent 
	hash $remote_addr 
	hash $cookle_name 

conslstent: 参数,指定使用一致性hash算法.

keepalived

    使用长连接的连接数, 每个 worker 与 后端服务器保持最大空闲时长.

keepalive_timeout

    长连接超时时间.

health_check

    定义对后端主机的健康状态监测机制,只能用于 location 上下文中.

语法:  health_check [parameters] 

参数:

参数       说明          
lnterval=time 监测频率,默认为每隔5秒.
fails=num 判断服务器状态为失败需要监测的次数  ,默认为1   
passes=num 判断服务器状态为成功需要监测的次数. 默认为1 
url=url 判断其是否健康与否使用的url. 默认为/
math=name   基于指定的 match 来衡量监测结果的成败. 
port=numper 使用独立的端口进行监测.

列:

http {
upstream onmpw {
         zone onmpw 64k;
        server 192.168.144.128;
        server 192.168.144.132;
        server 192.168.144.131;
    }
    server {
        listen 80;
        location / {
       proxy_pass http://onmpw;
       health_check;
        }
    }
}

ngx_stream_core_module

    nginx1.9新模块ngx_stream_core_module实现stream tcp负载均衡,实现四层代理.

 主要实现 七层 代理

 1 stream {
 2     upstream text {
 3         server 192.168.1.111:22weight=5 max_fails=3 fail_timeout=30s;
 4         server 192.168.2.222:22weight=5 max_fails=3 fail_timeout=30s;
 5     }
 6 
 7     server {
 8         listen 22022;
 9         proxy_connect_timeout 1s;
10         proxy_timeout 3s;
11         proxy_pass text;
12     }
13 }
示例

参数说明:

1)  listen
    监听地址
listen address:port [ssl] [udp] [backlog=number] [bind] [ip]

2) poxy_pass address 

3) proxy_timeout timeout:

4) proxy_connect_timeout time;
	设置nginx 与代理服务器尝试建立连接的时长,超时时间,默认为60s;

nginx反代php-fpm

 示例说明:

fastcgi_cache_path /data/nginx/fcgicache levels=:1:2:2 keys_zone=fcache:10m max_size=10g;
location ~*\.php {
    fastcgi_pass 192.168.0.11:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME /data/apps$fastcgi_script_name;
    fastcgi_cache fcache;
    fastcgi_cache_key $request_uri;
    fastcgi_cache_valid 200 302 10m;
    fastcgi_cache_valid 301 1h;
    fastcgi_cache_valid any 1m;
    
}

  

 

posted @ 2020-04-13 09:59  闫世成  阅读(297)  评论(0编辑  收藏  举报