tengine(nginx)正向代理和反向代理,tenginenginx


今天搞了一下tengine(nginx)正向代理和反向代理,简单总结一下。

说到正向代理和反向代理不得不提一下iptables的DNAT和SNAT,每次遇到内网去访问公网或者公网访问内网的时候就会第一时间想到iptables的NAT。

SNAT:


关于从外网回的包,iptables具有状态跟踪,就不需要我们自己专门为回包设置策略了。

DNAT:


关于DNAT经常用到端口转发,比如不想其他人直接访问你的22端口(ssh服务),就可以做个端口转发,从1322转到22上,这样其他人就能通过1322访问你的ssh服务了。

 在你的服务器执行
 iptables -t nat -A PREROUTING -p tcp  -d 192.168.4.177 --dport 1322 -j DNAT --to 192.168.4.177:22
 上面这条命令远程机器可以通过访问1322端口访问你的ssh的服务,但是本机不能通过1322访问ssh服务,
 如果有上面的需求,请添加
 iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 1322 -j DNAT --to 127.0.0.1:22

这里简单提一下,具体iptables,可以看一下大神的链接:

https://www.frozentux.net/iptables-tutorial/cn/iptables-tutorial-cn-1.1.19.html#TRAVERSINGGENERAL

http://www.cnblogs.com/metoy/p/4320813.html

http://www.360doc.com/content/15/0122/17/18578054_442885253.shtml

===============================================================================================

下面来说说tengine/nginx的代理,iptables做nat有个条件就是该主机必须是网关或者是内网机器可以默认路由到该主机,如果不做默认路由,一条条的加路由不太现实,因为外网域名对应的主机ip可能是动态的。可以用nginx做代理实现内外网访问。

正向代理:

这样内网的机器就可以代理访问外网了。

################nginx.conf文件
user   nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

dso {
    load ngx_http_rewrite_module.so;
    load ngx_http_access_module.so;
    load ngx_http_concat_module.so;
    load ngx_http_limit_conn_module.so;
    load ngx_http_limit_req_module.so;
    load ngx_http_sysguard_module.so;
    load ngx_http_upstream_session_sticky_module.so;
    load ngx_http_trim_filter_module.so;
}
http {
    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"';
    sendfile        on;
    keepalive_timeout  65;
    include /app/nginx/conf.d/*.conf;#注意这里的目录
}
创建上面的目录,在该目录下创建任意文件以.conf为名:

##conf.d下文件以文件.conf结尾
server {
    listen 端口;#server的端口
    server_name 主机IP;
    resolver 主机deDNS;#这个可以通过 /etc/resolv.conf文件查看
    resolver_timeout 5s;
    location / {
        proxy_pass $scheme://$host$request_uri;
        proxy_set_header Host $http_host;
 
        proxy_buffers 256 4k;
        proxy_max_temp_file_size 0;
 
        proxy_connect_timeout 30;
 
        proxy_cache_valid 200 302 10m;
        proxy_cache_valid 301 1h;
        proxy_cache_valid any 1m;
    }
}
重启nginx/tengine

#客户端添加
export http_proxy=10.10.10.10:80 #可以通过no_proxy排除不走代理的机器
curl www.baidu.com #应该就能通了
如果是tomcat程序的话,需要修改tomcat启动参数,这样tomcat程序才能通过代理访问外网
#nonProxyHosts是不走代理的IP,catalina.sh
-Dhttp.proxyHost=10.10.10.10 -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=localhost|127.0.0.*|10.14.*

http://www.cnblogs.com/inteliot/archive/2013/01/11/2855907.html(不错的例子)

反向代理:


这样外网机器就能访问内网了。

######nginx.conf
user   nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

dso {
    load ngx_http_rewrite_module.so;
    load ngx_http_access_module.so;
    load ngx_http_concat_module.so;
    load ngx_http_limit_conn_module.so;
    load ngx_http_limit_req_module.so;
    load ngx_http_sysguard_module.so;
    load ngx_http_upstream_session_sticky_module.so;
    load ngx_http_trim_filter_module.so;
}
http {
    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"';
    sendfile        on;
    keepalive_timeout  65;
    include /app/nginx/conf.d/*.conf;#注意这个目录,后面要添加的
}
创建上面提到目录,并在下面创建任意名字以.conf结尾的文件
upstream  tomcatproxy {
    consistent_hash $request_uri;
    server   IP:port weight=1;
    server   IP:port weight=1;
    server   IP:prt weight=1;
    session_sticky;
    check interval=3000 rise=2 fall=5 timeout=1000 type=http;
    check_http_send "GET   /OSN/ESBStatus  HTTP/1.0\r\nconnection: keep-alive\r\n\r\n";
    check_http_expect_alive http_2xx http_3xx;
}
server {
    listen       8001;
    server_name  主机IP;
    location / {
        root   /app/nginx/html;
        index  index.html index.htm;
    }


    location ~ status {
            check_status;
            access_log   off;
                      }
    location /API {
            proxy_pass http://tomcatproxy/OSN;#这里要和upstream对应
        }
    error_page   501 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}
重启tengine(nginx)

通过外网应该能访问了。

#具体tengine的参数可以上tengine官网查看,都是中文,。

相关内容

    暂无相关文章