nginx实现https与http共存方案

这篇具有很好参考价值的文章主要介绍了nginx实现https与http共存方案。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

在日常开发中,到正式上线的时候大部分需要使用https来保证链路传输的安全性,这块相信大家都了解了,但有些特殊场景下可能需要http和https共存,并且端口都是同一个端口,只是协议不同,那这块我们就借助nginx来配置了。

nginx配置

nginx安装对应模块

  • 通过configure安装共存需要的模块stream、with-stream_ssl_preread_module、http_ssl_module。如果自身nginx配置了其他模块,记得把原来的模块的内容添加进去。
./configure --with-stream --with-stream_ssl_preread_module --with-http_ssl_module
  • 编译安装的模块,使用make命令,等待编译完成
make
  • 编译完成后复制新的nginx文件替换到原来的nginx启动文件。记得先备份原来的nginx文件
mv /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak   //备份原有文件
cp -r /usr/local/nginx-1.22.0/objs/nginx /usr/local/nginx/sbin/   //复制新的nginx
// 到nginx的目录检验是否安装好
cd /usr/local/nginx/sbin/
./nginx -V
  • 检查版本信息以及安装的模块是否正确,版本信息和模块都没问题的话就ok了。
    nginx 配置 https 访问 并且不影响 原来的 http 访问,liunx,学习工具,nginx,https,http,linux

配置文件修改

server模块分别配置http和https

  • 添加http的server监听
    server {
		listen       8083;   ##监听端口
        server_name  localhost;

		location {
			proxy_pass   http://127.0.0.1:8001; ##应用服务地址,如tomcat对应地址
		}
    }
  • 添加https的server监听
      #Https请求
    server {
        listen 8082 ssl; #使用不同的端口监听
        server_name localhost;
		
        ssl_certificate /usr/local/nginx/conf/cert/xxxx.pem; #证书放到 nginx的/conf/cert/文件夹内
        ssl_certificate_key /usr/local/nginx/conf/cert/xxxx.key; #证书放到 nginx的/conf/cert/文件夹内

        ssl_protocols TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        location / {  
	    	proxy_pass   http://127.0.0.1:8083;   #转发到http的请求中去
        }
     }

steam模块中的配置

  • steam是与http模块统一个级别的,需要先添加steam模块的配置。
stream{

    log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

      access_log /usr/local/nginx/logs/tcp_access.log proxy;
      error_log /usr/local/nginx/logs/tcp_error.log error;
      open_log_file_cache off;
	
}
  • 在steam模块中分别配置http和https的监听

	server {
    	  listen 8084;#nginx侦听端口,统一对外端口,不能与下面已有的端口使用同一个
    	  ssl_preread on;
    	  proxy_pass $upstream;
        }

 	# 根据不同的协议走不同的upstream
       map $ssl_preread_protocol $upstream {  
    	 default http_gateway;
   		"TLSv1.2" https_gateway;
    	"TLSv1.3" https_gateway;
       }
	#http请求
		upstream http_gateway {
   	    	server  127.0.0.1:8083; 
        }
	#https请求
        upstream https_gateway {
            server  127.0.0.1:8082; 
        }

完整配置文件


#user  nobody;
user root;
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;
}


stream{

    log_format proxy '$remote_addr [$time_local] '
                 '$protocol $status $bytes_sent $bytes_received '
                 '$session_time "$upstream_addr" '
                 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

      access_log /usr/local/nginx/logs/tcp_access.log proxy;
      error_log /usr/local/nginx/logs/tcp_error.log error;
      open_log_file_cache off;

	
	  # 根据不同的协议走不同的upstream
       map $ssl_preread_protocol $upstream {  
    	 default http_gateway;
		"TLSv1.2" https_gateway;
    	"TLSv1.3" https_gateway;
       }

       server {
    	  listen 8084;#nginx侦听端口
    	  ssl_preread on;
    	  proxy_pass $upstream;
        }
		
		#http请求
		upstream http_gateway {
			server  127.0.0.1:8083;
        }
		#https请求
        upstream https_gateway {
            server  127.0.0.1:8082; 
        }

}


http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    
    server {
		listen       8083;
        server_name  localhost;
		
		location {
			proxy_pass   http://127.0.0.1:8001; ##应用服务地址,如tomcat对应地址
		}

    }


   #Https请求
    server {
        listen 8082 ssl;
        server_name localhost;

        ssl_certificate /usr/local/nginx/conf/cert/xxxx.pem; #证书放到 nginx的/conf/cert/文件夹内
        ssl_certificate_key /usr/local/nginx/conf/cert/xxxx.key; #证书放到 nginx的/conf/cert/文件夹内

        ssl_protocols TLSv1.2;
        ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
        ssl_prefer_server_ciphers on;
        ssl_session_cache shared:SSL:10m;
        ssl_session_timeout 10m;

        location / {
	    proxy_pass   http://127.0.0.1:8083;
        }
     }

}

重启nginx并测试

  • 测试nginx配置文件是否有问题。
//到对应目录,检验配置文件是否有错误
./nginx - t

nginx 配置 https 访问 并且不影响 原来的 http 访问,liunx,学习工具,nginx,https,http,linux

  • 没有相关错误即可重启nginx
./nginx - s reload
  • reload命令在安装了模块后有可能不生效,可以使用kill的方式停止nginx后重新启动
ps -ef|grep nginx   ##查看nginx对应的进程
kill -TERM pid   ##kill掉查看出来的进程id
./nginx -c /usr/local/nginx/conf/nginx.conf   ##指定配置文件重新启动
  • 使用对应的地址进行测试。按上面配置则可以访问
    http://ip:8084/
    https://ip:8084/
    两个地址都能访问则代表配置成功。

总结

以上呢就是http和https共存的基本配置了,大家可以尽情发挥了,可以跟着这块去延续其他的各种配置,比如请求http的时候默认跳转到https等等。文章来源地址https://www.toymoban.com/news/detail-690300.html

到了这里,关于nginx实现https与http共存方案的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • SpringBoot + Vue2项目打包部署到服务器后,使用Nginx配置SSL证书,配置访问HTTP协议转HTTPS协议

    SpringBoot + Vue2项目打包部署到服务器后,使用Nginx配置SSL证书,配置访问HTTP协议转HTTPS协议

    配置nginx.conf文件,这个文件一般在/etc/nginx/...中,由于每个人的体质不一样,也有可能在别的路径里,自己找找... 证书存放位置,可自定义存放位置 两个文件 后端配置 把.pfx拷贝到resource下,然后配置一下yml

    2024年02月02日
    浏览(51)
  • 通过nginx的upstream配置域名进行http/htts的访问最佳实践方案(406/404问题解决)

    通过nginx的upstream配置域名进行http/htts的访问最佳实践方案(406/404问题解决)

    ​ 最近,开发部门有一个访问需求,被访问方给了我们两个https的域名访问接口,这里假设为: ​ 这两个域名解析出来的地址和接口信息都是一样的,但是根据要求,需要将两个域名访问接口作为主备的方式进行配置,在https://aaa.target.com/mytarget/login/出现异常不能使用的时候

    2024年01月19日
    浏览(18)
  • Linux tomcat 8 配置访问本地文件,并且配置https

    Linux tomcat 8 配置访问本地文件,并且配置https

    就可以通过 【http:// ip + 端口号+ tomcat配置的代理访问路劲+文件名】 来访问文件 本地要有jdk 【已经配置好环境变量那些】 打开cmd 输入: 秘钥库口令要记住,后边tomcat配置 有用 一步一步回答问题,最后会在F 盘生成文件 这边注意打开 HTTP/1.1 下的注释配置,并且进行修改

    2024年02月09日
    浏览(12)
  • 前端vue部署到nginx并且配置https安全证书全流程

    前端vue部署到nginx并且配置https安全证书全流程

            说明一下: 本人原本使用的是docker安装nginx通过挂载实现部署,但是出现了很多bug(例如部署安全证书后还是无法访问),所以困扰了很久,最后改为本地安装nginx,最终在不懈的努力下终于按照好了,特此记录一下。         一:整个流程:            

    2024年02月07日
    浏览(14)
  • nextcloud设置https nextcloud docker配置阿里云SSL证书实现HTTPS访问 亲测方案

    1、下载阿里云ssl证书,类型为:apache,得到三个文件两个crt,一个key。 2、把文件夹更名为cert,文件名改为chain.crt,pubilc.crt,web.key。 3、把文件夹复制进nextcloud容器/etc/apache2文件夹中 4、进入容器 执行 5、修改ssl.load配置文件 如果有下面这一句就不用修改了,没有就添加上 6、

    2024年02月13日
    浏览(15)
  • nginx支持一个端口访问多个前端项目(http以及https)

        最近做项目结构优化,前端项目都是部署在nginx上,想实现同一个端口可以访问多个前端项目.这样可以提高服务器的端口复用率,降低项目部署以及维护成本.根据平常的需求,用两台nginx服务器分别支持http、https同一端口访问不同项目。下面将配置方式以及相关注意事项做简

    2024年02月03日
    浏览(16)
  • nginx下添加http_ssl_module并且配置域名,指定端口

    1.切换到源码包: 2.进行编译: 3.配置完成后,运行命令: make命令执行后,不要进行make install,否则会覆盖安装。 4.备份原有已安装好的nginx: 5.停止nginx状态: 6.将编译好的nginx覆盖掉原有的nginx: 7.提示是否覆盖,输入yes即可。 8.然后启动nginx: 9.进入nginx/sbin目录下,通过

    2024年02月11日
    浏览(16)
  • nginx配置https访问

    为什么需要使用HTTPS,因为HTTP不安全,当使用http进行消息传输时,可能会遭到黑客的劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。 实现https:需要使用ssl协议。 首先需要申请证

    2024年01月22日
    浏览(11)
  • 关于IIS安全设置http能访问https不能访问的解决方案

    关于IIS安全设置http能访问https不能访问的解决方案

    最近折腾IIS,发现网站的http能访问但是https不能访问。 我确认所有关于HTTPS的配置我都配置正确了,结果还是不能访问,一番折腾发现,服务器本身的防火墙和阿里云服务器的安全组规则不是一回事。改完防火墙也没有用,重要的是阿里云平台里改服务器的安全规则。 只需要

    2024年02月17日
    浏览(15)
  • Nginx配置http和https

    配置文件 默认放置位置:{nginx}/conf.d/,以conf结尾 一、http简单配置 说明: 1,http默认端口是80 2,http://127.0.0.1:8888;为实际本地服务端口 3,一般服务域名为二级域名www,一级域名一般也配置指向www域名。 二、https配置 首先得申请ssl证书,百度,阿里都有免费证书可用,申请成

    2023年04月09日
    浏览(9)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包