文章目录

  • 前言
  • 1. Nginx简介
  • 2. Nginx安装
    • 2.1 需求
    • 2.2 实验环境
    • 2.3 搭建环境
      • 安装nginx和所需的模块
    • 2.4 配置文件
    • 2.5 防火墙配置
  • 3. 客户端(被代理主机)配置
  • 4. 结果测试
  • 5. 坑点
    • 5.1 配置文件问题
    • 5.2 使用systemctl启动

前言

最近要帮同学弄一个正向代理环境,需要将内网80、443端口的流量转发出去,在解决的途中遇到了很多问题,有很多的坑。想起以前学习Nginx感觉没这么难,现在感觉以前的很多东西都没学深。
       在自己的印象中Nginx可以用来作web服务器,但只能处理静态页面,支持CGI协议的动态语言。可作为代理服务器,如果面向的对象是客户端那么它应该是一个反向代理,如果是面对的是服务器那么它应该是一个正向代理。以及和keepalived组建高可用的环境。

1. Nginx简介

Nginx是异步框架的网页服务器、正向代理、反向代理、负载均衡和动静分离服务器。是开源软件,其实大部分使用nginx的机器主要是作为负载均衡服务,nginx在官方测试的结果中能够支持五万个并发,当然在实际环境中估计也就三万左右。
       更多内容请访问<维基百科>

2. Nginx安装

2.1 需求

本文主要是基于nginx的正向代理,在A搭建代理服务器,让内网主机B能利用A的代理(HTTP)S出网。

2.2 实验环境

CentOS 8 两台
nginx-1.18.0
nginx安装包获取:http://nginx.org/download/
访问链接可根据需求下载需要的版本
实验拓扑如下:

2.3 搭建环境

安装nginx和所需的模块

由于本人需要代理https的流量,需要使用一个模块用于隧道SSL请求。正常的安装流程和模块地址<ngx_http_proxy_connect_module>
相关版本对应表:

       安装步骤

$ wget http://nginx.org/download/nginx-1.18.0.tar.gz
$ tar -xzvf nginx-1.18.0.tar.gz
$ cd nginx-1.18.0
$  patch -p1 < /root/ngx_http_proxy_connect_module/patch/proxy_connect_rewrite_1018.patch
$ ./configure  \
--user=www \
--group=www \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_stub_status_module \
--with-threads \
--add-module=/root/ngx_http_proxy_connect_module
$ make && make install # 编译安装

验证安装结果

$ /usr/local/nginx/sbin/nginx -V

2.4 配置文件

至于详细的配置文件解读除了查看官方文档外还可以查看【冯insist】的博客,该文全面而详细的讲解了nginx。

user  www www;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       8080;resolver     114.114.114.114;#server_name  192.168.204.146;location / {#root   html;#index  index.html index.htm;proxy_pass http://$http_host$request_uri;proxy_set_header HOST $http_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;}}    server {listen     8443;# dns resolver used by forward proxyingresolver  114.114.114.114;# forward proxy for CONNECT requestproxy_connect;proxy_connect_allow            443 563;proxy_connect_connect_timeout  10s;proxy_connect_read_timeout     10s;proxy_connect_send_timeout     10s;# forward proxy for non-CONNECT requestlocation / {proxy_pass http://$host;    proxy_set_header Host $host;}}
}

配置文件修改完毕重启nginx服务。

2.5 防火墙配置

需要开启http/https相关端口,使用以下命令查看开放的端口

firewall-cmd --list-ports

开启端口

firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --permanent --add-port=8443/tcp
firewall-cmd --reload # !!!修改配置后要重启防火墙

3. 客户端(被代理主机)配置

本次实验代理服务器采用两张网卡充当网关设备,其中内网IP为(192.168.54.4)

export http_proxy=192.168.54.4:8080 # 需要代理的http访问地址
export https_proxy=192.168.54.4:8443 # 需要代理的https访问地址

export no_proxy=192.168.54.4,域名 # 访问地址时不需要进行代理的是哪些

4. 结果测试

7层需要通过HTTP CONNECT来建立隧道,属于客户端有感知的普通代理方式,需要在客户端手动配置(HTTP)S代理端IP和端口,在客户端进行测试结果如下:
HTTP

[root@localhost ~]# curl http://baidu.com -svo /dev/null -x 192.168.54.4:8080
* Rebuilt URL to: http://baidu.com/
*   Trying 192.168.54.4...
* TCP_NODELAY set
* Connected to 192.168.54.4 (192.168.54.4) port 8080 (#0)
> GET http://baidu.com/ HTTP/1.1
> Host: baidu.com
> User-Agent: curl/7.61.1
> Accept: */*
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 OK
< Server: nginx/1.18.0
< Date: Fri, 08 Jan 2021 07:26:01 GMT
< Content-Type: text/html
< Content-Length: 81
< Connection: keep-alive
< Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
< ETag: "51-47cf7e6ee8400"
< Accept-Ranges: bytes
< Cache-Control: max-age=86400
< Expires: Sat, 09 Jan 2021 07:26:01 GMT
<
{ [81 bytes data]
* Connection #0 to host 192.168.54.4 left intact

HTTPS

[root@localhost ~]# curl https://baidu.com -svo /dev/null -x 192.168.54.4:8443
* Rebuilt URL to: https://baidu.com/
*   Trying 192.168.54.4...
* TCP_NODELAY set
* Connected to 192.168.54.4 (192.168.54.4) port 8443 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to baidu.com:443
> CONNECT baidu.com:443 HTTP/1.1
> Host: baidu.com:443
> User-Agent: curl/7.61.1
> Proxy-Connection: Keep-Alive
>
< HTTP/1.1 200 Connection Established
< Proxy-agent: nginx
<
* Proxy replied 200 to CONNECT request
* CONNECT phase completed!
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/pki/tls/certs/ca-bundle.crtCApath: none
} [5 bytes data]
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
} [512 bytes data]
* CONNECT phase completed!
* CONNECT phase completed!
{ [5 bytes data]
* TLSv1.3 (IN), TLS handshake, Server hello (2):
{ [104 bytes data]
* TLSv1.2 (IN), TLS handshake, Certificate (11):
{ [3023 bytes data]
* TLSv1.2 (IN), TLS handshake, Server key exchange (12):
{ [333 bytes data]
* TLSv1.2 (IN), TLS handshake, Server finished (14):
{ [4 bytes data]
* TLSv1.2 (OUT), TLS handshake, Client key exchange (16):
} [70 bytes data]
* TLSv1.2 (OUT), TLS change cipher, Change cipher spec (1):
} [1 bytes data]
* TLSv1.2 (OUT), TLS handshake, Finished (20):
} [16 bytes data]
* TLSv1.2 (IN), TLS handshake, Finished (20):
{ [16 bytes data]
* SSL connection using TLSv1.2 / ECDHE-RSA-AES128-GCM-SHA256
* ALPN, server accepted to use http/1.1
* Server certificate:
*  subject: C=CN; ST=Beijing; O=BeiJing Baidu Netcom Science Technology Co., Ltd; OU=service operation department; CN=www.baidu.cn
*  start date: Feb 27 00:00:00 2020 GMT
*  expire date: Feb 26 12:00:00 2021 GMT
*  subjectAltName: host "baidu.com" matched cert's "baidu.com"
*  issuer: C=US; O=DigiCert Inc; CN=DigiCert SHA2 Secure Server CA
*  SSL certificate verify ok.
} [5 bytes data]
> GET / HTTP/1.1
> Host: baidu.com
> User-Agent: curl/7.61.1
> Accept: */*
>
{ [5 bytes data]
< HTTP/1.1 302 Moved Temporarily
< Server: bfe/1.0.8.18
< Date: Fri, 08 Jan 2021 07:31:48 GMT
< Content-Type: text/html
< Content-Length: 161
< Connection: keep-alive
< Location: http://www.baidu.com/
<
{ [161 bytes data]
* Connection #0 to host 192.168.54.4 left intact

通过上面的详细情况可以了解客户端和代理服务器建立HTTP CONNECT隧道的详细情况,代理服务器回复HTTP/1.1 200 Connection Established后就开始交互TLS/SSL握手和流量交互。

5. 坑点

在使用源码安装nginx后使用systemctl start控制nginx启动问题

5.1 配置文件问题

在nginx安装完成后,会根据需求对配置文件进行配置,往往对于新手或者不熟悉nginx的人来说,可能会输入错误和忘记一些特殊符号造成语法错误,在这个时候需要检查配置文件是否存在语法错误,使用命令<nginx -t>来检查配置文件。测试通过截图如下:

5.2 使用systemctl启动

在使用源码安装后,会新建/etc/init.d/nginx文件来设置开机自启,当使用systemctl start nginx来控制启动nginx服务时,无法启动使用journalctl -ex查看,出现下图所示的告警:

       解决方法,进入 /lib/systemd/system该目录下新建nginx.service文件,在文件内添加内容如下:

[Unit]
Description=nginx
After=network.target[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true[Install]
WantedBy=multi-user.target

参考连接:https://blog.csdn.net/u012463871/article/details/90573545
参考连接:https://www.cnblogs.com/stulzq/p/9291223.html?spm=a2c4e.10696291.0.0.12e019a4a0Ldkl

Nginx-正向代理HTTPS相关推荐

  1. nginx正向代理https

    nginx正向代理https 需求 当出现下述情形时,我们需要使用正向代理: 我们的client不能直接访问到服务器,倒是其他的client可以访问,且我们的client到OtherClient 的网 ...

  2. nginx 正向代理https配置

    1.需求描述 nginx正向代理 如图所示,公司内网为了安全考虑,只提供一台服务器可与外网作为网关,进行访问. 1.服务器A可以访问外网: 2.服务器B,C无法访问外网: 3.服务器ABC之间内网互通 ...

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

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

  4. (二)nginx正向代理https

    原文章地址:https://www.cnblogs.com/zhaobin-diray/p/10861195.html 使用nginx作为http/https正向代理 默认nginx是不支持https ...

  5. Nginx正向代理https请求

    默认开源版本的nginx是不支持代理https请求的,如果要实现这个功能,需要第三方模块 https://github.com/chobits/ngx_http_proxy_connect_modul ...

  6. nginx正向代理https,zabbix内网实现企业微信告警

    1.首先给nginx添加模块ngx_http_proxy_connect_module模块主要用于隧道SSL请求的代理服务器 模块下载地址: https://github.com/chobits/ng ...

  7. nginx正向代理转发https出现502

    nginx正向代理转发https出现502 现象1 原因1 解决方案1 参考博客1 现象2 原因2 解决方案2 参考博客2 其他原因 现象1 nginx的error日志显示如下(xxx是替换过了真实信 ...

  8. Linux:Nginx 正向代理实现内网访问互联网

    前言: 因为公司的某些系统需要访问互联网上的某些功能,每个系统的服务器都开通访问互联网的能力太麻烦并且不方便管理,所以打算只对一台服务器开通访问互联网的能力,并在此服务器基础上搭建 nginx 正向代 ...

  9. nginx正向代理的配置和使用

    nginx正向代理的配置和使用 nginx正向代理的配置和使用 nginx正向代理的配置和使用 安装包准备 下载nginx安装包 下载正向代理模块的包 版本与模块对照表 部署nginx服务 上传ngi ...

  10. nginx 正向代理http和https

    nginx正向代理默认只支持http,不支持https,需借助第三方模块"ngx_http_proxy_connect_module"来实现(https://github.com/ ...

最新文章

  1. 逻辑回归,朴素贝叶斯,KMeans,决策树的不足和优势
  2. ArrayList和数组间的相互转换
  3. 071_html语言代码
  4. java 获取nodejs端Gson数据案例(请求MongoDB)
  5. 经典C语言程序100例之八五
  6. LiveVideoStack线上交流分享 ( 八 ) —— TCP的困境与解决方案
  7. css3中的渐变效果及花斑动画的实现
  8. [obc学习日记]3.10
  9. VMware虚拟网络设置(NAT模式,桥接模式,仅主机模式设置),再也不用担心虚拟机连不上网了。
  10. cadence 常见pcb电阻_不加端接电阻的快乐,你们绝对想象不到!
  11. 从小白到大牛,程序员必读的经典套系书
  12. 多径瑞利信道的一种matlab产生方法
  13. 第二章:JAVA编程基础
  14. 使用SMART原则快速上手一门编程语言
  15. 阿里云Oss获取图片的大小信息
  16. logback日志配置说明
  17. [转载]三、二、一 …… Geronimo!,第 3 部分: 状态问题
  18. 【前端】jQuery上传图片插件 : uploadifive
  19. 定时任务监控服务Healthchecks
  20. 网页 浏览器兼容性问题的解决

热门文章

  1. sublime和vscode 格式化Json ——两步走
  2. 2020年中国色纺纱行业市场现状分析,国内参与者呈现寡头模式「图」
  3. 有了TeamViewer,在家工作不是难事
  4. Win10升级后C盘莫名其妙满了怎么办
  5. python基础:找基因
  6. 项目管理工具之Git/GitHub/Gitee/Gitlab
  7. vue后台系统管理项目-openlayers地图定位、港口数据标记功能
  8. 统计学(五):非参数检验
  9. 04 【CSS选择器 】
  10. Revit修改部分楼层上的轴网