首页 > 系统相关 >nginx实现一个对外端口同时支持http和https协议访问

nginx实现一个对外端口同时支持http和https协议访问

时间:2024-02-04 12:00:33浏览次数:21  
标签:http stream ssl server nginx https gateway

前言

  • 最近有一个需求,需要让一个非80端口的http服务支持https访问;但是业务牵扯太多没法将http直接改为https,因此需要一个端口同时支持http和https

方案一. 使用nginx的stream、 stream_ssl_preread模块

参考资料

1.准备工作

  • nginx版本1.11.5及以上
  • 由于stream和stream_ssl_preread模块非默认引入,需要在编译安装nginx时引入;编译时添加配置参数 --with-stream --with-stream_ssl_preread_module
  • 这里给一个笔者编译tengine时的配置,仅供参考
./configure --add-module=./modules/ngx_http_upstream_vnswrr_module --add-module=./modules/ngx_http_proxy_connect_module --without-http_rewrite_module --with-openssl=/root/openssl-1.0.1j --with-pcre  --without-http_gzip_module --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --with-stream_sni

2.配置stream

stream {
  upstream http_gateway {
    # 20036端口是一个开启http的端口
    server  127.0.0.1:20036;
  }
  upstream https_gateway {
    # 20037端口是一个开启https的端口
    server  127.0.0.1:20037;
  }
  # 根据不同的协议走不同的upstream
  map $ssl_preread_protocol $upstream{
    default http_gateway;
    "TLSv1.0" https_gateway;
    "TLSv1.1" https_gateway;
    "TLSv1.2" https_gateway;
    "TLSv1.3" https_gateway;
  }
  server {
    listen 12345;
    ssl_preread on;
    proxy_pass $upstream;
  }
}

3.简单的nginx.conf示例供参考

user  root;
worker_processes  2;
worker_rlimit_nofile  100000;
stream {
  upstream http_gateway {
    server  127.0.0.1:20036;
  }
  upstream https_gateway {
    server  127.0.0.1:20037;
  }
  map $ssl_preread_protocol $upstream{
    default http_gateway;
    "TLSv1.0" https_gateway;
    "TLSv1.1" https_gateway;
    "TLSv1.2" https_gateway;
    "TLSv1.3" https_gateway;
  }

  server {
    listen 12345;
    ssl_preread on;
    proxy_pass $upstream;
  }
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
     upstream gateway_service{
          server  127.0.0.1:8080  weight=1;
          server  127.0.0.1:8081  weight=2;
     }

    server {
        listen       20036;
        listen       20037 ssl;
        server_name  xxx.com;
        ssl_certificate      xxx.pem;
        ssl_certificate_key  xxx.key;
        location / {
            proxy_pass http://gateway_service;
        }
    }

}

上面配置即可实现端口12345同时支持http和https协议访问。

但是上面配置存在一个弊端就是服务端无法获取到客户端请求的真实ip地址,因为使用stream之后,上面配置无法获取到客户端的真实ip。

于是对上面配置进行进一步修改,

user  root;
worker_processes  2;
worker_rlimit_nofile  100000;
stream {
  upstream http_gateway {
    server  127.0.0.1:20036;
  }
  upstream https_gateway {
    server  127.0.0.1:20037;
  }
  map $ssl_preread_protocol $upstream{
    default http_gateway;
    "TLSv1.0" https_gateway;
    "TLSv1.1" https_gateway;
    "TLSv1.2" https_gateway;
    "TLSv1.3" https_gateway;
  }

  server {
    listen 12345;
    ssl_preread on;
    proxy_pass $upstream;
	proxy_protocol on; #开启protocol
  }
}

http {
	real_ip_recursive on;
    real_ip_header proxy_protocol; #基于stream层传来protocol数据进行后续修改
    set_real_ip_from 127.0.0.0/24; #从real_ip_header中删除这个ip段的ip,也可直接写具体ip;


    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
     upstream gateway_service{
          server  127.0.0.1:8080  weight=1;
          server  127.0.0.1:8081  weight=2;
     }

    server {
        listen       20036;
        listen       20037 ssl;
        server_name  xxx.com;
        ssl_certificate      xxx.pem;
        ssl_certificate_key  xxx.key;
        location / {
            proxy_pass http://gateway_service;
        }
    }
}

上面配置即可实现将客户端ip透传

 

参考文章:

nginx实现一个对外端口同时支持http和https协议访问 - 知乎 (zhihu.com)

nginx开启stream模块获取真实ip | 忐忑の博客 (twbhub.top)

标签:http,stream,ssl,server,nginx,https,gateway
From: https://www.cnblogs.com/longan-wang/p/18005937

相关文章

  • 实现前端调度器nginx收到请求,调度到后端Apache、实现动静分离
    一、Apache服务器上安装httpdyuminstallhttpd-y二、生成包含IP地址的新页面echo192.168.1.4>/var/www/html/index.html三、开启服务,并设置为开机自启动systemctlstarthttpdsystemctlenablehttpd四、测试访问本机IP地址[17:13:26root@apache~]#curl192.......
  • nginx---防止盗链
    ngx_http_referer_module模块:用来阻止Referer首部无有效值的请求访问,可防止盗链valid_referersnone|blocked|server_names|string...;定义referer首部的合法可用值,不能匹配的将是非法值none:请求报文首部没有referer首部blocked:请求报文有referer首部,但无有效值se......
  • Nginx 可视化配置神器NginxConfig
    Nginx是前后端开发工程师必须掌握的神器。该神器有很多使用场景:比如反向代理、负载均衡、动静分离、跨域等等。把Nginx下载下来打开conf文件夹的nginx.conf文件,Nginx服务器的基础配置和默认的配置都存放于此。配置是让程序员非常头疼的事,比如Java后端框架SSM,大量配......
  • lua 语法介绍与 NGINX lua 高级用法实战操作
    目录一、概述二、lua安装三、lua语法1)lua数据类型2)lua变量3)lua拼接字符串4)lua循环5)lua函数6)lua条件控制7)lua库模块四、NGINXlua高级用法一、概述lua是一种轻量小巧的脚本语言,用标准C语言编写并以源代码形式开放,其设计目的是为了嵌入应用程序中,从而为应用程序提供灵活......
  • nginx默认的启动停止重启命令是什么?
    在Linux系统中,Nginx服务的启动、停止和重启命令通常如下:启动Nginx:对于基于systemd的系统(如Ubuntu15.04+、CentOS7+):sudosystemctlstartnginx对于不使用systemd管理的传统init系统(如Ubuntu14.04及以前版本):sudoservicenginxstart或者直接执行nginx可执行文件(如......
  • onlyoffice编译和https设置资料
    编译文档:https://helpcenter.onlyoffice.com/installation/docs-community-compile.aspxhttps设置:https://helpcenter.onlyoffice.com/installation/docs-community-https-linux.aspxhttps://blog.csdn.net/u013930899/article/details/134428379......
  • Angular 17+ 高级教程 – HttpClient
    前言HttpClient是Angular对 XMLHttpRequest和 Fetch的封装。HttpClient的DX(DeveloperExperience)比 XMLHttpRequest和 Fetch都好,只是学习成本比较高,因为它融入了RxJS概念。要深入理解HttpClient最好先掌握3个基础技能:XMLHttpRequest--看这篇Fetch......
  • FastGateway 一个可以用于代替Nginx的网关
    在我本人研究Yarp的时候经常用于公司项目的业务网关代理,这时候就个大佬问我是否可以实现动态加载HTTPS证书?那时候我说不太可能实现,然而在某一天我看到微软使用Yarp代替了Nginx吞吐量提升了百分之八十!这个时候我就萌生了自己使用yarp造一个Gateway的项目,应为我本身也经常使用ngi......
  • SpringBoot项目支持https的nacos地址
    问题描述Causedby:sun.security.validator.ValidatorException:PKIXpathbuildingfailed:sun.security.provider.certpath.SunCertPathBuilderException:unabletofindvalidcertificationpathtorequestedtarget atsun.security.validator.PKIXValidator.doBuild(......
  • nginx的安装windows
    参考网站:nginxforWindows1.下载https://nginx.org/en/download.html最新版本,nginx-1.25.3,解压到当前文件夹2.启动,停止nginx启动:D:\nginx\nginx-1.25.3>startnginx.exe查看nginx的启动进程:D:\nginx\nginx-1.25.3>tasklist/fi"imagenameeqnginx.exe"映像名称......