基于CentOS 7配置Nginx正向代理

前端之家收集整理的这篇文章主要介绍了基于CentOS 7配置Nginx正向代理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Nginx是一款以轻量级、低内存开销、支持缓存、支持反向代理,负载均衡,电子邮件服务而著称。对于鲜为人知的是,它还可以作为一个简单易用的正向代理服务器。本文简要描述这个正向代理功能并给出演示,供大家参考。

有关Nginx的安装请参考
CentOS 7下yum方式安装Nginx
Nginx 概述及日常管理
Nginx基于IP,端口,域名配置虚拟主机

一、配置Nginx正向代理服务端配置

  1. 演示环境
  2. # more /etc/redhat-release
  3. CentOS Linux release 7.2.1511 (Core)
  4.  
  5. 当前主机名称ip
  6. # hostname
  7. centos7-router
  8.  
  9. # ip addr|grep -inet|grep global
  10. 9: inet 172.24.8.254/24 brd 172.24.8.255 scope global eno16777728
  11. 15: inet 192.168.1.175/24 brd 192.168.1.255 scope global dynamic eno33554960
  12.  
  13. 当前主机的dns配置
  14. # more /etc/resolv.conf
  15. # Generated by NetworkManager
  16. nameserver 192.168.1.1
  17.  
  18. Nginx版本
  19. # Nginx -v
  20. Nginx version: Nginx/1.12.2
  21.  
  22. Nginx正向代理配置
  23. # vim /etc/Nginx/conf.d/proxy.conf
  24. server {
  25. listen 8080; ##指定一个非缺省端口用于提供代理服务
  26. server_name localhost;
  27. resolver 192.168.1.1; ##指定DNS服务器IP
  28.  
  29. location / {
  30. proxy_pass $scheme://$host$request_uri;
  31. proxy_set_header Host $http_host;
  32.  
  33. ##proxy_pass:设置代理服务器的协议和地址以及位置应映射到的可选URI。协议可指定http或https
  34. ##proxy_set_header:与许字段重新定义或附加请求标头传递给代理服务器
  35.  
  36. proxy_buffers 256 4k; ## Author : Leshami
  37. proxy_max_temp_file_size 0; ## Blog : http://blog.csdn.net/leshami
  38.  
  39. ##proxy_buffers:为单个连接设置用于从代理服务器读取响应的缓冲区个数和缓冲区大小
  40. ##proxy_max_temp_file_size:禁用缓冲对临时文件的响应
  41.  
  42. proxy_connect_timeout 30; ##代理连接超时时间
  43.  
  44. proxy_cache_valid 200 302 10m; ##为不同的响应代码设置缓存时间
  45. proxy_cache_valid 301 1h;
  46. proxy_cache_valid any 1m;
  47. }
  48. }
  49.  
  50. # systemctl reload Nginx.service
  51. # ss -nltp|grep Nginx
  52. LISTEN 0 128 *:8080 *:* users:(("Nginx",pid=110780,fd=10),("Nginx",pid=19774,fd=10))
  53. LISTEN 0 128 *:80 *:* users:(("Nginx",fd=6),fd=6))
  54.  
  55. 防火墙配置
  56. # firewall-cmd --add-port=8080/tcp --permanent
  57. # firewall-cmd --reload

二、客户端配置

  1. 客户端主机名及IP
  2. # hostname
  3. centos7-web.example.com
  4. # ip addr|grep inet|grep global
  5. inet 172.24.8.128/24 brd 172.24.8.255 scope global eno16777728
  6.  
  7. 临时设置当前环境变量http_proxy
  8. # export http_proxy=http://172.24.8.254:8080
  9.  
  10. # curl -I http://www.baidu.com
  11. HTTP/1.1 200 OK
  12. Server: Nginx/1.12.2
  13. Date: Tue,24 Oct 2017 14:59:44 GMT
  14. Content-Type: text/html
  15. Content-Length: 277
  16. Connection: keep-alive
  17. Last-Modified: Mon,13 Jun 2016 02:50:26 GMT
  18. ETag: "575e1f72-115"
  19. Cache-Control: private,no-cache,no-store,proxy-revalidate,no-transform
  20. Pragma: no-cache
  21. Accept-Ranges: bytes
  22.  
  23. 清除http_proxy
  24. # unset http_proxy
  25.  
  26. 演示wget直接使用代理参数方式访问网络
  27. # wget -e "http_proxy=http://172.24.8.254:8080" www.baidu.com
  28. --2017-10-24 23:03:48-- http://www.baidu.com/
  29. Connecting to 172.24.8.254:8080... connected.
  30. Proxy request sent,awaiting response... 200 OK
  31. Length: 2381 (2.3K) [text/html]
  32. Saving to: index.html
  33.  
  34. 演示curl直接使用代理参数方式访问网络
  35. # curl -x http://172.24.8.254:8080 -I http://www.baidu.com
  36. HTTP/1.1 200 OK
  37. Server: Nginx/1.12.2
  38. Date: Tue,24 Oct 2017 15:07:39 GMT
  39. Content-Type: text/html
  40. Content-Length: 277
  41. Connection: keep-alive
  42. Last-Modified: Mon,no-transform
  43. Pragma: no-cache
  44. Accept-Ranges: bytes
  45.  
  46. 如果需要用户名密码,格式
  47. curl -x "http://user:pwd@host:port" www.baidu.com
  48.  
  49. 配置http_proxy以及ftp_proxy到应用程序,如yum代理配置
  50. /etc/yum.conf里面增加proxy=proxy_addr:port
  51.  
  52. # unset http_proxy
  53. # cp /etc/yum.conf /etc/yum.conf.bk
  54. # echo "proxy=http://172.24.8.254:8080">>/etc/yum.conf
  55.  
  56. # tail -1 /etc/yum.conf
  57. proxy=http://172.24.8.254:8080
  58.  
  59. # vim /etc/yum.repo.d/Nginx.repo
  60. [Nginx]
  61. name=Nginx repo
  62. baseurl=http://Nginx.org/packages/centos/7/$basearch/
  63. gpgcheck=0
  64. enabled=1
  65.  
  66. # yum clean all
  67. # yum repolist
  68. Loaded plugins: fastestmirror,langpacks
  69. Nginx | 2.9 kB 00:00:00
  70. Nginx/x86_64/primary_db | 31 kB 00:00:01
  71. Determining fastest mirrors
  72. repo id repo name status
  73. Nginx/x86_64 Nginx repo 90
  74. repolist: 90
  75. [root@centos7-web yum.repos.d]# yum makecache
  76. Loaded plugins: fastestmirror,langpacks
  77. Nginx | 2.9 kB 00:00:00
  78. (1/2): Nginx/x86_64/other_db | 16 kB 00:00:00
  79. (2/2): Nginx/x86_64/filelists_db | 39 kB 00:00:01
  80. Loading mirror speeds from cached hostfile
  81. Metadata Cache Created
  82.  
  83. 全局配置
  84. # cp /etc/skel/.bash_profile /etc/skel/.bash_profile.bk
  85. # vim /etc/skel/.bash_profile
  86. export http_proxy=http://172.24.8.254:8080
  87. export https_proxy=http://172.24.8.254:8080
  88.  
  89. # source /etc/skel/.bash_profile
  90. # env |grep http
  91. http_proxy=http://172.24.8.254:8080
  92. https_proxy=http://172.24.8.254:8080

猜你在找的CentOS相关文章