nginx作为正向代理,反向代理的一些应用

正向代理代理的对象是客户端

反向代理代理的对象是服务端

举例说下nginx作为正向代理作访问控制

server{
    listen   80;
    server_name localhost jeson.gaosf.com;
    access_log /var/log/nginx/log/host.access.log main;

    location /{
        if($http_x_forwarded_for !~* "^116\.62\.103\.228"){
            return 403;
        }
        root /opt/app/code;
        index index.html index.htm;
    }
}

利用http_x_forwarded_for 来识别是不是116.62.103.228这个ip,不是的话就返回403

 

反向代理的例子:

在/etc/nginx/conf.d下的realserver.conf里面

server{
    listen   8080;
    server_name localhost jeson.gaosf.com;
    access_log /var/log/nginx/log/serve.access.log main;

    location /{
        root /opt/app/code2;
        index index.html index.htm;
    }
}

在/opt/app/code2下有个test_proxy.html文件

在/etc/nginx/conf.d下的fx_proxy.conf里面

server{
    listen   80;
    server_name localhost jeson.gaosf.com;
    access_log /var/log/nginx/log/host.access.log main;

    location /{
        root /usr/share/nginx/html;
        index index.html index.htm;
    }
    
    //当匹配test_proxy.html,会代理8080端口
    location ~/test_proxy.html${
        proxy_pass http://127.0.0.1:8080;
    }
}

然后查看下

netstat -luntp|grep nginx

查看下状态

 

location /{
        proxy_pass http://127.0.0.1:8080;
        proxy_redirect default;
        
        proxy_set_header Host $http_host;
        proxy_set_header  X-Real-IP  $remote_addr;

        proxy_connect_timeout 30;
        proxy_send_timeout  60;
        proxy_read_timeout   60;

        proxy_buffer_size 32k;
        proxy_buffering on;
        proxy_buffers 4 128k;
        proxy_busy_buffers_size 256k;
        proxy_max_temp_file_size  256k;

proxy_buffer是为了减少I/O   ,开启内存缓存区

 对于上面这种可读性相对较差

可以这样把下面的提取出来,用一个文件名来代替,然后把内容当道一个文件里

location /{

  proxy_pass  http://127.0.0.1:8080;

     include proxy_params;

}

 

posted @ 2019-01-05 11:33  代码堆里的看客  阅读(477)  评论(0编辑  收藏  举报