iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >nginx正向代理的配置和使用
  • 405
分享到

nginx正向代理的配置和使用

nginx运维服务器nginx正向代理 2023-08-18 18:08:19 405人浏览 泡泡鱼
摘要

Nginx正向代理的配置和使用 nginx正向代理的配置和使用 nginx正向代理的配置和使用安装包准备下载nginx安装包下载正向代理模块的包版本与模块对照表 部署nginx服务上传nginx包和正向模块包解压,改名安装ng

Nginx正向代理的配置和使用

nginx正向代理http,nginx正向代理https

  • 正向代理,指的是通过代理服务器 代理浏览器/客户端去重定向请求访问到目标服务器 的一种代理服务。正向代理服务的特点是代理服务器 代理的对象是浏览器/客户端,也就是对于目标服务器 来说浏览器/客户端是隐藏的。

  • nginx默认支持正向代理Http,不支持https

  • nginx官方并不支持直接转发https请求,nginx支持https需要ngx_http_proxy_connect_module模块。GitHub开源了模块 https://github.com/chobits/ngx_http_proxy_connect_module。不过维护的ngx_http_proxy_connect_module模块的补丁也是有nginx版本限制的,需根据自身使用的nginx版本选择相应的正向代理模块。可以在REDEME.md的Select patch中查看nginx版本和模块的对应关系

安装包准备

下载nginx安装包

下载地址

在这里插入图片描述

下载正向代理模块的包

下载地址
在这里插入图片描述

版本与模块对照表

在这里插入图片描述

部署nginx服务

  • 此处使用的是nginx-1.20.2,对应proxy_connect_rewrite_1018.patch

上传nginx包和正向模块包

mkdir /nginxcd /nginx[root@Mysql nginx]# ll-rw-r--r-- 1 root root 1062124 Feb 12 15:23 nginx-1.20.2.tar.gz-rw-r--r-- 1 root root   57926 Feb 12 15:23 ngx_http_proxy_connect_module-master.zip

解压,改名

tar -xf nginx.tar.gzunzip ngx_http_proxy_connect_module-master.ziplldrwxr-xr-x 9 1001 1001    4096 Feb 12 15:27 nginx-1.20.2-rw-r--r-- 1 root root 1062124 Feb 12 15:23 nginx-1.20.2.tar.gzdrwxr-xr-x 5 root root    4096 Feb  9 16:54 ngx_http_proxy_connect_module-master-rw-r--r-- 1 root root   57926 Feb 12 15:23 ngx_http_proxy_connect_module-master.zipmv ngx_http_proxy_connect_module-master ngx_http_proxy_connect_modulelldrwxr-xr-x 9 1001 1001    4096 Feb 12 15:27 nginx-1.20.2-rw-r--r-- 1 root root 1062124 Feb 12 15:23 nginx-1.20.2.tar.gzdrwxr-xr-x 5 root root    4096 Feb  9 16:54 ngx_http_proxy_connect_module-rw-r--r-- 1 root root   57926 Feb 12 15:23 ngx_http_proxy_connect_module-master.zip

安装nginx

yum -y install make GCc openssl openssl-devel pcre-devel zlib zlib-develcd nginx-1.20.2ll drwxr-xr-x 6 1001 1001   4096 Feb 12 15:20 auto-rw-r--r-- 1 1001 1001 312251 Nov 16  2021 CHANGES-rw-r--r-- 1 1001 1001 476577 Nov 16  2021 CHANGES.rudrwxr-xr-x 2 1001 1001   4096 Feb 12 15:20 conf-rwxr-xr-x 1 1001 1001   2590 Nov 16  2021 configuredrwxr-xr-x 4 1001 1001   4096 Feb 12 15:20 contribdrwxr-xr-x 2 1001 1001   4096 Feb 12 15:20 html-rw-r--r-- 1 1001 1001   1397 Nov 16  2021 LICENSEdrwxr-xr-x 2 1001 1001   4096 Feb 12 15:20 man-rw-r--r-- 1 1001 1001     49 Nov 16  2021 READMEdrwxr-xr-x 9 1001 1001   4096 Feb 12 15:20 srC# 查看正向代理模块proxy_connect_rewrite_1018.patch的位置ll ../ngx_http_proxy_connect_module/patch/-rw-r--r-- 1 root root 9849 Feb  9 16:54 proxy_connect_1014.patch-rw-r--r-- 1 root root 9697 Feb  9 16:54 proxy_connect.patch-rw-r--r-- 1 root root 9408 Feb  9 16:54 proxy_connect_rewrite_1014.patch-rw-r--r-- 1 root root 9505 Feb  9 16:54 proxy_connect_rewrite_101504.patch-rw-r--r-- 1 root root 9496 Feb  9 16:54 proxy_connect_rewrite_1015.patch-rw-r--r-- 1 root root 9553 Feb  9 16:54 proxy_connect_rewrite_1018.patch-rw-r--r-- 1 root root 9306 Feb  9 16:54 proxy_connect_rewrite_102101.patch-rw-r--r-- 1 root root 9337 Feb  9 16:54 proxy_connect_rewrite.patch# 导入模块 后面为模块路径patch -p1 < /nginx/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch# 编译./configure --add-module=/nginx/ngx_http_proxy_connect_module# 安装,默认安装在/usr/local/nginx/make && make install# 查看nginxll /usr/local/nginx/drwx------ 2 nobody root 4096 Feb 12 15:47 client_body_tempdrwxr-xr-x 2 root   root 4096 Feb 12 15:46 confdrwx------ 2 nobody root 4096 Feb 12 15:47 fastcgi_tempdrwxr-xr-x 2 root   root 4096 Feb 12 15:28 htmldrwxr-xr-x 2 root   root 4096 Feb 12 15:47 logsdrwx------ 2 nobody root 4096 Feb 12 15:47 proxy_tempdrwxr-xr-x 2 root   root 4096 Feb 12 15:33 sbindrwx------ 2 nobody root 4096 Feb 12 15:47 scgi_tempdrwx------ 2 nobody root 4096 Feb 12 15:47 uwsgi_temp 

配置正向代理

cd /usr/local/nginx/#gzip  on;下添加vim conf/nginx.conf    #gzip  on;    #正向代理转发http请求    server {    #指定DNS服务器IP地址        resolver 114.114.114.114;    #监听80端口,http默认端口80        listen 80;    #服务器IP或域名            server_name  localhost;    #正向代理转发http请求    location / {        proxy_pass                 http://$host$request_uri;        proxy_set_header           HOST $host;        proxy_buffers              256 4k;        proxy_max_temp_file_size   0k;        proxy_connect_timeout      30;        proxy_send_timeout         60;        proxy_read_timeout         60;        proxy_next_upstream error  timeout invalid_header http_502;    }    }    #正向代理转发https请求    server {        #指定DNS服务器IP地址            resolver 114.114.114.114;        #监听443端口,https默认端口443        listen 443;        #正向代理转发https请求        proxy_connect;        proxy_connect_allow            443 563;        proxy_connect_connect_timeout  10s;        proxy_connect_read_timeout     10s;        proxy_connect_send_timeout     10s;     location / {             proxy_pass http://$host;             proxy_set_header Host $host;   }   }

创建nginx用户

  • nginx服务以用户nginx身份启动
useradd nginx

检查nginx配置并启动

sbin/nginx -tsbin/nginx
ss -utNLP | grep nginxtcp    LISTEN     0      511       *:443                   *:*                   users:(("nginx",pid=6645,fd=7),("nginx",pid=6644,fd=7))tcp    LISTEN     0      511       *:80                    *:*                   users:(("nginx",pid=6645,fd=6),("nginx",pid=6644,fd=6))

nginx服务所在服务器验证正向代理功能

 curl -I http://www.baidu.com/ -v -x 127.0.0.1:80 curl -I https://www.baidu.com/ -v -x 127.0.0.1:443
curl -I http://www.baidu.com/ -v -x 127.0.0.1:80* About to connect() to proxy 127.0.0.1 port 80 (#0)*   Trying 127.0.0.1...* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)> HEAD http://www.baidu.com/ HTTP/1.1> User-Agent: curl/7.29.0> Host: www.baidu.com> Accept: **>< HTTP/1.1 200 OKHTTP/1.1 200 OK< Accept-Ranges: bytesAccept-Ranges: bytes< Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transfORMCache-Control: private, no-cache, no-store, proxy-revalidate, no-transform< Connection: keep-aliveConnection: keep-alive< Content-Length: 277Content-Length: 277< Content-Type: text/htmlContent-Type: text/html< Date: Sun, 12 Feb 2023 09:03:40 GMTDate: Sun, 12 Feb 2023 09:03:40 GMT< Etag: "575e1f60-115"Etag: "575e1f60-115"< Last-Modified: Mon, 13 Jun 2016 02:50:08 GMTLast-Modified: Mon, 13 Jun 2016 02:50:08 GMT< Pragma: no-cachePragma: no-cache< Server: bfe/1.0.8.18Server: bfe/1.0.8.18<* Connection #0 to host 127.0.0.1 left intact

配置不能访问外网的服务器(即内网服务器),使其可以访问外网

liunx服务器配置

1.只配置使用yum时,能够使用正向代理访问外网

#追加配置vim /etc/yum.confproxy=http://192.168.0.20:80#nginx正向代理服务器的地址proxy=ftp://192.168.0.20:80#nginx正向代理服务器的地址

2.只配置使用wget时,能够使用正向代理访问外网

#追加配置vim /etc/wgetrchttp_proxy=192.168.0.20:80     #nginx正向代理服务器的地址http_proxy=192.168.0.20:443    #nginx正向代理服务器的地址

3.全局配置,所以访问请求都能够使用正向代理访问外网

#追加配置vim /etc/profilehttp_proxy=192.168.0.20:80https_proxy=192.168.0.20:443ftp_proxy=192.168.0.20:443export http_proxyexport https_proxyexport ftp_proxy# 加载配置source /etc/profile

windows服务器配置

在这里插入图片描述

验证不能访问外网的服务器(即内网服务器),使用代理上网

curl -I http://www.baidu.comcurl -I https://www.baidu.com
curl -I http://www.baidu.comHTTP/1.1 200 OKServer: nginx/1.20.2Date: Sun, 12 Feb 2023 09:31:03 GMTContent-Type: text/htmlContent-Length: 277Connection: keep-aliveAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformEtag: "575e1f60-115"Last-Modified: Mon, 13 Jun 2016 02:50:08 GMTPragma: no-cache
curl -I https://www.baidu.comHTTP/1.1 200 Connection EstablishedProxy-agent: nginxHTTP/1.1 200 OKAccept-Ranges: bytesCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Length: 277Content-Type: text/htmlDate: Sun, 12 Feb 2023 09:31:07 GMTEtag: "575e1f60-115"Last-Modified: Mon, 13 Jun 2016 02:50:08 GMTPragma: no-cacheServer: bfe/1.0.8.18

来源地址:https://blog.csdn.net/qq_44659804/article/details/128997510

--结束END--

本文标题: nginx正向代理的配置和使用

本文链接: https://www.lsjlt.com/news/374528.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • nginx正向代理的配置和使用
    nginx正向代理的配置和使用 nginx正向代理的配置和使用 nginx正向代理的配置和使用安装包准备下载nginx安装包下载正向代理模块的包版本与模块对照表 部署nginx服务上传nginx包和正向模块包解压,改名安装ng...
    99+
    2023-08-18
    nginx 运维 服务器 nginx正向代理
  • nginx 做正向代理配置
    nginx在绝大数的场景中我们使用其用于做web中间件或反向代理使用,但是nginx实际上也提供了正向代理的功能。下面我们来进行nginx正向代理配置操作,以便大家能够掌握nginx正向代理配置方法。获取nginx正向代理模块# git c...
    99+
    2023-06-05
  • nginx反向代理配置
    文章目录 1.反向代理介绍2. 第一个实例2.1 实现效果2.2 准备工作(1) 在linux系统中安装tomcat,使用默认端口8080(2) 修改主机映射(3) 修改nginx配置(4) ...
    99+
    2023-09-16
    nginx tomcat 服务器
  • 四、nginx正向代理
    详细文章内容 一、正向代理 解释:正向代理指的是客户端通过访问目标服务端,再由目标服务端来转发流量访问互联网 结构图如下: 好处:这样做的好处是,当客户端通过服务端访问互联网某个网站时,该网站获得的...
    99+
    2023-09-03
    nginx 服务器 网络
  • nginx反向代理如何配置
    要配置Nginx反向代理,您可以按照以下步骤进行操作:1. 安装Nginx:首先,确保您的系统上安装了Nginx。您可以通过运行以下...
    99+
    2023-10-08
    nginx
  • nginx如何配置反向代理
    这篇文章主要为大家展示了“nginx如何配置反向代理”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“nginx如何配置反向代理”这篇文章吧。 nginx 配置实...
    99+
    2024-04-02
  • nginx怎么配置反向代理
    这篇文章主要讲解了“nginx怎么配置反向代理”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“nginx怎么配置反向代理”吧!准备工作系统环境:centos 6.5_64关闭SELinux临时...
    99+
    2023-06-27
  • CentOS如何配置Nginx反向代理
    这篇文章主要讲解了“CentOS如何配置Nginx反向代理”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“CentOS如何配置Nginx反向代理”吧!一、反向代理及演示环境描述1、反向代理在计...
    99+
    2023-06-27
  • Nginx http反向代理如何配置
    本篇内容主要讲解“Nginx http反向代理如何配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Nginx http反向代理如何配置”吧!概述什么是 NginxNginx (engine x...
    99+
    2023-06-02
  • nginx如何配置upstream反向代理
    这篇文章主要介绍nginx如何配置upstream反向代理,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!nginx配置upstream反向代理http {  .....
    99+
    2024-04-02
  • 如何配置Nginx反向代理Tomcat
    小编给大家分享一下如何配置Nginx反向代理Tomcat,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!编辑nginx.conf文件,在http/server/location层次结构下,添加proxy_pass http:...
    99+
    2023-06-04
  • 怎么使用Nginx作反向代理配置GZip压缩
    这篇文章主要介绍了怎么使用Nginx作反向代理配置GZip压缩的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇怎么使用Nginx作反向代理配置GZip压缩文章都会有所收获,下面我们一起来看看吧。前提条件: nod...
    99+
    2023-07-04
  • CentOS 7中怎么使用cPanel配置Nginx反向代理
    本文小编为大家详细介绍“CentOS 7中怎么使用cPanel配置Nginx反向代理”,内容详细,步骤清晰,细节处理妥当,希望这篇“CentOS 7中怎么使用cPanel配置Nginx反向代理”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢...
    99+
    2023-06-27
  • 配置LNMP,并部署php应用。配置nginx反向代理。
    目录 配置LNMP,并部署php应用 1、部署LNMP环境 2、配置LNMP环境  配置nginx反向代理 1.什么是反向代理 2.反向代理配置 配置LNMP,并部署php应用 1、部署LNMP环境 安装epel扩展源 wget -O...
    99+
    2023-09-01
    php nginx 服务器
  • Nginx正向代理与反向代理的含义是什么
    这篇文章主要介绍“Nginx正向代理与反向代理的含义是什么”,在日常操作中,相信很多人在Nginx正向代理与反向代理的含义是什么问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Nginx正向代理与反向代理的含义...
    99+
    2023-07-05
  • Linux中怎么配置Nginx反向代理
    Linux中怎么配置Nginx反向代理,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。反向代理是什么? 反向代理指以代理服务器来接受Internet上的连接请求,然后将请求转...
    99+
    2023-06-13
  • Linux下怎么配置Nginx反向代理
    这篇文章主要介绍Linux下怎么配置Nginx反向代理,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!反向代理功能是nginx的三大主要功能之一,反向代理服务器是一种代理服务器,用于管理从外部网络到内部网络的连接或任何...
    99+
    2023-06-28
  • nginx如何配置域名反向代理
    要配置nginx进行域名反向代理,可以按照以下步骤进行操作: 打开nginx配置文件,一般位于/etc/nginx/nginx....
    99+
    2024-04-22
    nginx
  • nginx怎么配置域名反向代理
    要配置域名反向代理,可以按照以下步骤操作: 打开nginx的配置文件,一般位于 /etc/nginx/nginx.conf 或者...
    99+
    2024-04-09
    nginx
  • Docker部署Nginx并配置反向代理
    准备工作 在docker内部署任何应用,都需要先下载对应的镜像;下载镜像之前,需要先搜索镜像来确认该镜像是否存在: docker search nginx 从列表可以看到,dock...
    99+
    2024-04-02
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作