用nginx做代理服务器上网

11:48:00 PM 0 Comments

目前现状:只有1个机器能上网(web),其他机器不能
方法:能上网的做一个代理web服务器中转,其他机器连接它即可。采用nginx

Nginx配置如下:

server{
        resolver x.x.x.x;
        listen 82;
        location / {
                proxy_pass http://$http_host$request_uri;
        }
}

注意项:
1. 不能有hostname
2. 必须有resolver, 即dns,即上面的x.x.x.x,换成你们的DNS服务器ip即可
3 . $http_host和$request_uri是nginx系统变量,不要想着替换他们,保持原样就OK。

查看dns方法
cat /etc/resolv.conf

代理使用

在需要访问外网的机器上执行以下操作之一即可:
1. export http_proxy=http://yourproxyaddress:proxyport
2. gedit ~/.bashrc  
    export http_proxy=http://yourproxyaddress:proxyport
yourproxyaddress也就是你的Nginx服务器的ip了,proxyport就是上面配置中的82,可以根据自己的需要修改。


举例:

  1. worker_processes 1;
  2. master_process off;
  3. daemon off;
  4. #pid /var/run/nginx.pid;

  5. events {
  6. worker_connections 768;
  7. # multi_accept on;
  8. }

  9. http {
  10. include mime.types;
  11. default_type application/octet-stream;

  12. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  13. '$status $body_bytes_sent "$http_referer" '
  14. '"$http_user_agent" "$http_x_forwarded_for"';

  15. access_log /var/log/nginx/access.log;
  16. error_log /var/log/nginx/error.log;

  17. sendfile on;

  18. server {
  19. resolver 10.57.220.2;
  20. listen 82;
  21. access_log logs/host.access.log main;

  22. location / {
  23. proxy_pass http://$http_host$request_uri;
  24. }


  25. }
  26. }

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.