当前位置:   article > 正文

nginx正向代理_windows的正向nginx代理

windows的正向nginx代理

感谢原作者,原地址:https://cloud.tencent.com/developer/article/1521322

 

一、nginx正向代理介绍及配置

1、环境介绍

代理服务器系统环境为:centos

nginx代理服务器为:192.168.10.10

测试客户端为局域网内任意windows电脑或Linux电脑

2、正向代理简介

nginx不仅可以做反向代理,还能用作正向代理来进行上网等功能。如果把局域网外的Internet想象成一个巨大的资源库,则局域网中的客户端要访问Internet,则需要通过代理服务器来访问,这种代理服务就称为正向代理(也就是大家常说的,通过正向代理进行上网功能)

3、nginx正向代理的配置

现在的网站基本上都是https,要解决既能访问http80端口也能访问https443端口的网站,需要配置两个SERVER节点,一个处理HTTP转发,另一个处理HTTPS转发,而客户端都通过HTTP来访问代理,通过访问代理不同的端口,来区分HTTP和HTTPS请求。

  1. [root@localhost ~]# vim /usr/local/nginx-1.12.1/conf/nginx.conf
  2. server {
  3. resolver 114.114.114.114; #指定DNS服务器IP地址
  4. listen 80;
  5. location / {
  6. proxy_pass http://$host$request_uri; #设定代理服务器的协议和地址
  7. proxy_set_header HOST $host;
  8. proxy_buffers 256 4k;
  9. proxy_max_temp_file_size 0k;
  10. proxy_connect_timeout 30;
  11. proxy_send_timeout 60;
  12. proxy_read_timeout 60;
  13. proxy_next_upstream error timeout invalid_header http_502;
  14. }
  15. }
  16. server {
  17. resolver 114.114.114.114; #指定DNS服务器IP地址
  18. listen 443;
  19. location / {
  20. proxy_pass https://$host$request_uri; #设定代理服务器的协议和地址
  21. proxy_buffers 256 4k;
  22. proxy_max_temp_file_size 0k;
  23. proxy_connect_timeout 30;
  24. proxy_send_timeout 60;
  25. proxy_read_timeout 60;
  26. proxy_next_upstream error timeout invalid_header http_502;
  27. }
  28. }
  29. [root@localhost ~]# /usr/local/nginx-1.12.1/sbin/nginx -s reload

4、Linux客户端访问测试

http的访问测试

  1. [root@localhost ~]# curl -I --proxy 192.168.10.10:80 www.baidu.com
  2. HTTP/1.1 200 OK
  3. Server: nginx/1.12.1
  4. Date: Mon, 11 Jun 2018 15:37:47 GMT
  5. Content-Type: text/html
  6. Content-Length: 612
  7. Last-Modified: Thu, 31 May 2018 09:28:16 GMT
  8. Connection: keep-alive
  9. ETag: "5b0fc030-264"
  10. Accept-Ranges: bytes
  11. https的访问测试
  12. [root@localhost ~]# curl -I --proxy 192.168.10.10:443 www.baidu.com
  13. HTTP/1.1 200 OK
  14. Server: nginx/1.12.1
  15. Date: Mon, 11 Jun 2018 15:38:07 GMT
  16. Content-Type: text/html
  17. Content-Length: 277
  18. Connection: keep-alive
  19. Accept-Ranges: bytes
  20. Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
  21. Etag: "575e1f5c-115"
  22. Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
  23. Pragma: no-cache
  24. 5、设置Linux客户端全局代理
  25. [root@localhost ~]# vim /etc/profile
  26. export http_proxy='192.168.10.10:80'
  27. export http_proxy='192.168.10.10:443'
  28. export ftp_proxy='192.168.10.10:80'
  29. [root@localhost ~]# source /etc/profile
  30. [root@localhost ~]# curl -I www.baidu.com:80
  31. HTTP/1.1 200 OK
  32. Server: nginx/1.12.1
  33. Date: Mon, 11 Jun 2018 16:10:18 GMT
  34. Content-Type: text/html
  35. Content-Length: 277
  36. Connection: keep-alive
  37. Accept-Ranges: bytes
  38. Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
  39. Etag: "575e1f5c-115"
  40. Last-Modified: Mon, 13 Jun 2016 02:50:04 GMT
  41. Pragma: no-cache
  42. [root@localhost ~]# curl -I www.baidu.com:443
  43. HTTP/1.1 200 OK
  44. Server: nginx/1.12.1
  45. Date: Mon, 11 Jun 2018 16:10:27 GMT
  46. Content-Type: text/html
  47. Content-Length: 277
  48. Connection: keep-alive
  49. Accept-Ranges: bytes
  50. Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
  51. Etag: "575e1f59-115"
  52. Last-Modified: Mon, 13 Jun 2016 02:50:01 GMT
  53. Pragma: no-cache

上面结果就说明我们的服务端nginx正向代理和客户端使用nginx做为全局代理设置成功。

 

如果实际生产用的,在hosts配置下  代理服务ip  www.baidu.com

然后 curl http://www.baidu.com:443 就可以访问了

参考:

https://blog.csdn.net/chikuai9995/article/details/100723079

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/706055
推荐阅读
相关标签
  

闽ICP备14008679号