nginx实现代理上网,有三个关键点必须注意,其余的配置跟普通的nginx一样
1.增加dns解析resolver
2.增加无server_name名的server
3.proxy_pass指令
具体实现步骤:
1、在nginx.conf配置文件的http域下面配置如下server模块:
server {
resolver 8.8.8.8;
resolver_timeout 30s;
listen 8999;
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;
}
}
2、重启nginx使配置文件生效
nginx -s reload
3、设置代理,比如在IE浏览器中
4、测试上网功能是否正常(需要注意:暂不支持https,只能代理http的连接)可以访问 http://ip.quancha.cn 检测代理是否生效,如果能正常上网,则说明代理成功!
5、nginx.conf完整配置文件如下:
#user nobody;
worker_processes 1;
error_log E:\\nginx-1.13.1\\logs debug;
events {
#use epoll;
worker_connections 1024;
}
http {
# resolver dns
server {
resolver 8.8.8.8;
resolver_timeout 30s;
listen 8999;
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;
}
}
}