nginx代理服务器如何使用?
发布时间:2019-12-11 15:11:53 来源:IP精灵
nginx是功能非常强大的代理服务器,我们能够用它来实现正向代理、反向代理。那么,它们要分别怎么来设置实现使用呢?下面我们来看看IP精灵带来的介绍。
nginx要如何配置正向代理和反向代理?
反向代理的配置教程:
http {
#省略了前面一般的配置,直接从负载均衡这里开始
#设置地址池,后端3台服务器
upstream servermap {
server 192.168.1.1:8080 weight=2 max_fails=2 fail_timeout=30s;
server 192.168.1.2:8080 weight=3 max_fails=2 fail_timeout=30s;
server 192.168.1.38080 weight=4 max_fails=2 fail_timeout=30s;
}
#一个虚拟主机,用来反向代理http_server_pool这组服务器
server {
listen 80;
#外网访问的域名
server_name www.test.com;
location / {
# 后端服务器返回500 503 404错误,自动请求转发到upstream池中另一台服务器
proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
proxy_pass http://servermap;
proxy_set_header Host www.test.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
access_log logs/www.test.com.access.log combined;
}
}
正向代理的配置教程:
server{
resolver 10.1.23.4;
resolver_timeout 30s;
listen 8888;
location / {
proxy_pass http://$http_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;
}
}
注意:
1、必须有resolver, 即dns,超时时间可选项
2、不能有hostname
3、配置代理服务器 Http 状态缓存时间
4、配置缓存大小,关闭磁盘缓存读写减少I/O、代理连接超时时间
配置好后,重启nginx,以浏览器为例,要使用这个代理服务器,则只需将浏览器代理设置为http://IP:8888,即可使用了。
以上就是IP精灵本次要给大家介绍的代理服务器正向、反向代理设置教程,希望对大家使用能有帮助。