爱迪生
由于工作需要搭建一个前端代理服务器(外网)一台,后端web服务器(内网)两台。说话要配个图才能更好的理解:
看第一张图就行了,我的代理是用nginx做反向代理,其实nginx也可以做正向代理,本来打算只用nginx的,但是因为nginx不支持https的正向代理,在网上查了好多资料,虽然有办法解决,但是看nginx官网说作者不打算在后续的版本增加nginx的https正向代理功能,又从网上查了好多都说nginx是为反向代理而生的,正向代理并不是它的特长,看有人推荐用squid做正向代理,并且支持多种连接协议。多提一句,做正向代理squid的效率比nginx要差很多,这个是从网上查的,没有具体实验。所以最终就采用nginx做反向代理,采用squid做正向代理,同时部署在有外网的服务器上监听不同的端口就可以了。下面是具体的配置文件信息:
先看下外网服务器的网卡配置信息:
eth0网卡配置:
eth1网卡配置:
网关配置:
下面是nginx配置文件信息:
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } 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"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream mysvr { server 192.168.10.129:8080; # server 192.168.10.121:3333 backup; #?.? } server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://mysvr; #璇锋?杞..mysvr 瀹.??..?″.?.〃 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
接着是squid配置文件信息:
acl manager proto cache_object acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl localnet src 172.16.0.0/12 # RFC1918 possible internal network acl localnet src 192.168.10.1/16 # RFC1918 possible internal network acl localnet src fc00::/7 # RFC 4193 local private network range acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 21 # ftp acl Safe_ports port 443 # https acl Safe_ports port 70 # gopher acl Safe_ports port 210 # wais acl Safe_ports port 1025-65535 # unregistered ports acl Safe_ports port 280 # http-mgmt acl Safe_ports port 488 # gss-http acl Safe_ports port 591 # filemaker acl Safe_ports port 777 # multiling http acl CONNECT method CONNECT # # Recommended minimum Access Permission configuration: # # Deny requests to certain unsafe ports http_access deny !Safe_ports # Deny CONNECT to other than secure SSL ports http_access deny CONNECT !SSL_ports # Only allow cachemgr access from localhost http_access allow localhost manager http_access deny manager # We strongly recommend the following be uncommented to protect innocent # web applications running on the proxy server who think the only # one who can access services on "localhost" is a local user #http_access deny to_localhost # # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS # # Example rule allowing access from your local networks. # Adapt localnet in the ACL section to list your (internal) IP networks # from where browsing should be allowed http_access allow localnet http_access allow localhost http_access deny all # Squid normally listens to port 3128 http_port 3128 # Uncomment and adjust the following to add a disk cache directory. #cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 # 缓存目录 cache_dir ufs /usr/local/squid/var/spool/squid 100 16 256 # Leave coredumps in the first cache dir coredump_dir /usr/local/squid/var/cache/squid # # Add any of your own refresh_pattern entries above these. # refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern -i (/cgi-bin/|\?) 0 0% 0 refresh_pattern . 0 20% 4320
其中要注意的就是记得把服务器的转发功能打开,具体配置修改上网查
1.修改/etc/sysctl.conf文件,让包转发功能在系统启动时自动生效:
# Controls IP packet forwarding
net.ipv4.ip_forward = 1
2.因为我们的数据库和redis都是1网段的,所以当时还遇到个问题,具体参考:
https://www.oschina.net/question/3335049_2240133
3.有正向代理的情况时,在java代码中也要开配置相关的代理请求:
/** * 代理服务器 */ public static final String PROXY_HOST = "192.168.10.128"; /** * 代理服务器端口 */ public static final Integer PROXY_PORT = 3128; static{ System.setProperty("proxySet", "true"); System.setProperty("http.proxyHost", Application.PROXY_HOST); System.setProperty("http.proxyPort", Application.PROXY_PORT+""); System.setProperty("https.proxyHost", Application.PROXY_HOST); System.setProperty("https.proxyPort", Application.PROXY_PORT+""); } 或者 // 依次是目标请求地址,端口号,协议类型 HttpHost proxy = new HttpHost(Application.PROXY_HOST, Application.PROXY_PORT); requestConfig = RequestConfig.custom().setProxy(proxy).setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
外网的服务器配好了,下面就看下内网的服务器配置信息,工程照常发布就行,主要就是服务器的网络配置正确就没有问题了:
需要注意的就是测试时可以开启临时的代理:
export http_proxy=http://192.168.10.128:3128
export https_proxy=http://192.168.10.128:3128
这样就可以了。今天就写到这里,如果有什么遗漏的地方请评论指出。
相关推荐
总结一下,"squid和nginx配置正向代理访问API接口"涉及的关键知识点包括正向代理的概念、Squid和Nginx的代理功能、配置这两者的步骤、以及安全性控制和优化策略。正确配置和使用这两个工具可以帮助企业构建高效、...
【Squid 代理项目-正向和反向详细笔记文档实战案例】 Squid 是一个广泛使用的开源 HTTP 代理服务器,它支持多种代理模式,包括正向代理、透明代理和反向代理。理解这些代理类型是管理和优化网络流量的关键。 1. **...
安装nginx.txt"),并配置为反向代理,设置负载均衡策略。 4. 安装MySQL("4.安装MySQL、memcache.txt"),并进行数据安全和性能优化。 5. 安装Memcached,与PHP集成("5.安装Apache、PHP、eAccelerator、...
14. 反向代理和正向代理: 反向代理是指在服务器端,代理服务器接收客户端请求,将请求转发给内部网络中的服务器,并将从服务器上得到的响应返回给客户端。正向代理是指代理客户端,向目标服务器发送请求,代理...
面试中可能会询问Squid缓存服务器的配置与管理,Nginx和Lighttpd的反向代理、负载均衡设置,Memcached的使用场景,以及邮件服务器的搭建和维护。同时,负载均衡软件如HAProxy和Nginx的负载分发策略也是考察重点。 ...
35. **Squid、Varnish、Nginx对比**:Squid主要用于缓存,Varnish专注静态内容加速,Nginx多功能,兼顾反向代理和负载均衡。 36. **nginx处理HTTP请求流程**:接收请求、解析请求、查找匹配的location、处理请求、...
12. **代理服务器**:如Squid和Nginx,可以作为代理缓存,提高访问速度,同时也可以实现访问控制和安全过滤。 13. **负载均衡**:例如HAProxy和Nginx的负载均衡功能,可以分发流量,提高系统可用性,但各有优缺点,...
两种缓存消除策略lru.go(nginx,redis,squid中都有lru的身影) 内存对齐在golang中的表现(证明cpu读取内存是按块读取的) golang shell工具(golang实现的交互式shell,实现一个简易版的交互终端) golang实现...