zoukankan      html  css  js  c++  java
  • Nginx 配置 HTTPS(多域名)

    平常开发要求比较低, 依然在用 HTTP, 但到了微信小程序就不行了, 腾讯和苹果都对 API 提出了 HTTPS 的要求. 尤其是苹果, 不仅要求 HTTPS, 还要求 TLS 协议版本要在 1.2 以上, 这又被称为 App Transport Security(ATS).

    关于自己的标准是否满足 ATS, 可以使用此工具检测: ATS(App Transport Security)检测.

    -w769

    服务器配置

    使用 Nginx 进行 HTTPS 配置, 服务器几乎不用做改动, 依旧是祖传的 8080 端口, 以我所使用的 Spring Boot 为例, 仅仅是在 application.yml 中增加了两行配置而已.

    server:
        port: 8080
        tomcat:
            protocol_header: x-forwarded-proto
        use-forward-headers: true
        address: 127.0.0.1
    

    证书申请/购买

    在域名的基本信息页, 点击免费开启 SSL 证书, 输入相应的域名, 点击「申请」即可申请免费的 DV SSL 证书, 还需要做一些简单的信息补全等, 即可提交申请. 如果备案信息都齐全的话, 很快就可以验证成功, 在证书管理页面即可查看证书.
    -w1026

    -w1174

    选择下载证书for Nginx, 证书一式两份, 后缀分别为 pemkey, 下载完上传到服务器.

    域名映射

    我选择为两个子域名申请证书, 同时将这两个域名映射到同一个IP.

    -w1181

    Nginx 配置

    首先, 需要把 http 都转发到 https, 需要使用 rewrite, 这样, 当访问 http://example.cn 会自动转发到 https://example.cn.

    	server {
    	   	listen 80; # redirect to 443
    	    	server_name AAA.example.cn www.AAA.example.cn;
    	    	rewrite ^(.*)$  https://$host$1 permanent; 
    	}
    
            server {
                    listen 80; # redirect to 443
                    server_name BBB.example.cn www.BBB.example.cn;
                    rewrite ^(.*)$  https://$host$1 permanent;
            }
    

    然后就是真正的 https 部分了, 虽然域名不同, 但都监听 443 端口, 但有着不同的 server_name, 这样当收到请求时就可以根据请求的 server_name 不同来转发到不同的服务.

    而服务自身像往常一样只要继续监听 80908091 即可.

    	server {
                	listen 443 ssl;
                	server_name AAA.example.cn www.AAA.example.cn;    
    
                	ssl_certificate "/home/yushan/demontf/2076603_AAA.example.cn.pem";   
                	ssl_certificate_key "/home/yushan/demontf/2076603_AAA.example.cn.key";    
                
                	location / {
                    			proxy_pass http://127.0.0.1:8090;
                    			proxy_set_header X-Real-IP $remote_addr;
                    			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    			proxy_set_header Host $http_host;
                    			proxy_set_header X-NginX-Proxy true;
                    			proxy_redirect default;
    		}
    	}
    
    
    	server {
        		listen 443 ssl; # redirect to https
        		server_name BBB.example.cn www.BBB.example.cn;
        		
        		ssl_certificate "/home/yushan/demontf/2005538_BBB.example.cn.pem";
        		ssl_certificate_key "/home/yushan/demontf/2005538_BBB.example.cn.key";
        		
        		location / {
        		              proxy_pass http://127.0.0.1:8091;
        		              proxy_set_header X-Real-IP $remote_addr;
        		              proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        		              proxy_set_header Host $http_host;
        		              proxy_set_header X-NginX-Proxy true;
        		              proxy_redirect default;
        		}
    	}
    

    配置修改好之后, 需要重启 Nginx.

    参考

    Nginx 配置 HTTPS 服务器 | Aotu.io「凹凸实验室」
    阿里云+Https+Nginx+SpringBoot | tt_study

  • 相关阅读:
    windows设置文件夹显示缩略图
    windows合并文件夹窗口
    crm高速开发之Entity
    在Eclipse中搭建Dagger和Dagger2使用环境
    CCEditBox/CCEditBoxImpl
    failed to sync branch You might need to open a shell and debug the state of this repo.
    五个方法:让站点用心服务于每一位客户
    Mobile first! Wijmo 5 + Ionic Framework之:Hello World!
    ST Nucleo mbed套件开发 一 MBED环境使用 以Nucleo-F401为例 (二)
    关于Windows通过远程桌面訪问Ubuntu
  • 原文地址:https://www.cnblogs.com/imzhizi/p/https-server-using-nginx.html
Copyright © 2011-2022 走看看