当前位置:   article > 正文

ngxin 正向代理配置 访问互联网_nginx如何配置后能跳转至其他的公网地址

nginx如何配置后能跳转至其他的公网地址

      如果本地无法访问互联网,则通过nginx的正向代理,可以将本地的所有流量进行代理,从而达到访问互联网的目的,低版本的nginx需要安装插件ngx_http_proxy_connect_module才可以,否则浏览器访问时报错:ERR_TUNNEL_CONNECTION_FAILED

有的说高版本不需要插件,下载了https://nginx.org/download/nginx-1.24.0.zip,试了下,http协议没问题,但https协议也会报错:ERR_TUNNEL_CONNECTION_FAILED

话不多说,上代码:

1、下载nginx并安装相关依赖

  1. yum -y install gcc
  2. yum install -y gcc-c++
  3. yum install -y pcre pcre-devel
  4. yum install -y zlib zlib-devel
  5. yum install -y openssl openssl-devel
  6. mkdir -p /mnt/soft/installPackage
  7. cd /mnt/soft/installPackage
  8. wget http://nginx.org/download/nginx-1.18.0.tar.gz

2、解压nginx压缩包

tar -zxvf nginx-1.18.0.tar.gz

3、下载正向代理插件,此插件为必须

GitHub - chobits/ngx_http_proxy_connect_module: A forward proxy module for CONNECT request handling

  1. yum install git
  2. yum -y install patch
  3. git clone https://gitee.com/web_design_of_web_frontend/ngx_http_proxy_connect_module.git

4、编译正向代理插件

  1. cd /mnt/soft/installPackage/nginx-1.18.0
  2. patch -p1 < /mnt/soft/installPackage/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch

5、编译安装nginx

  1. ./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=/mnt/soft/installPackage/ngx_http_proxy_connect_module
  2. make && make install

安装目录:/usr/local/nginx

配置文件目录:/usr/local/nginx/conf/nginx.conf

6、配置正向代理

  1. # 参考 https://github.com/chobits/ngx_http_proxy_connect_module
  2. server {
  3. listen 9090;
  4. resolver 114.114.114.114 valid=60s ipv6=off;
  5. resolver_timeout 30s;
  6. proxy_connect;
  7. proxy_connect_allow 443 80;
  8. proxy_connect_connect_timeout 10s;
  9. proxy_connect_read_timeout 10s;
  10. proxy_connect_send_timeout 10s;
  11. location / {
  12. proxy_pass $scheme://$http_host$request_uri;
  13. proxy_set_header Host $host;
  14. proxy_http_version 1.1;
  15. #proxy_ssl_server_name on;
  16. }
  17. }

7、启动nginx

  1. cd /usr/local/nginx/sbin
  2. ./nginx
  3. /usr/local/nginx/sbin/ngxin

8、开机自启动

  1. cd /usr/lib/systemd/system/
  2. vi nginx.service
  3. #复制以下内容到nginx.service文件中[Unit]
  4. Description=nginx
  5. After=network.target
  6. [Service]
  7. Type=forking
  8. ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
  9. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  10. ExecStop=/usr/local/nginx/sbin/nginx -s quit
  11. PrivateTmp=true
  12. [Install]
  13. WantedBy=multi-user.target

9、重启nginx,加入开机自启动

  1. pkill -9 nginx
  2. systemctl start nginx
  3. systemctl enable nginx

10、配置系统代理

11、打开浏览器,访问外网地址,可以正常打开了

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

闽ICP备14008679号