Nginx模块之proxy

1、proxy

命令
  proxy_pass url  

  (1)代理所有URI到后端服务器
    location / {
        proxy_pass http://192.168.49.142/;
    }
  (2)代理指定URL到后端服务器
    location /test1/ {
        proxy_pass http://192.168.49.142/test2/;
     将请求/test1下面的资源都转到后端/test2/下的资源
    location /test1 {
         proxy_pass http://192.168.49.142;
     将请求/test1/下面的资源都转到后端/test1/下的资源

  proxy_set_header field value   设定向后端主机发送的请求报文的首部和值

  (1)将客户端请求的真实IP转到后端apache服务器
    location / {
          proxy_pass http://192.168.49.142/;
          proxy_set_header X-Real-IP $remote_addr;
    ;

  (2)将客户端请求的真实IP转到后端nginx服务器
    location / {
        proxy_pass http://192.168.49.142;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
   然后在后端服务器的日志格式中添加 $http_x_forwarded_for 字段

  proxy_cache_path path [levels=levels] keys_zone=name:size [inactive=time] [max_size=size]  定义缓存,可用于http上下文

  proxy_cache zone | off   调用缓存,默认为off

  proxy_cache_key string   定义缓存键

    proxy_cache_key $request_uri
    proxy_cache_key $scheme$proxy_host$request_uri

  proxy_cache_vaild [code...] time;   为不同的响应码设定其缓存的时长

  proxy_cache_vaild 200 302 10m;
  proxy_cache_vaild 404 1m;
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。