为网站添加https证书

Nginx服务器安装SSL证书

最近为网站加个证书加了很久,一开始是想用let’s encrypt免费证书的,但弄着弄着发现有个问题解决起来很麻烦,要用外国的dns服务才可能能够解决,所以就干脆用阿里云的免费证书了。

在证书控制台下载Nginx版本证书。下载到本地的压缩文件包解压后包含:

  • .crt文件:是证书文件,crt是pem文件的扩展名。
  • .key文件:证书的私钥文件(申请证书时如果没有选择自动创建CSR,则没有该文件)。

我们最好将上面两个文件都放在cert文件夹中,方便操作,并且我将两个文件改名为a.crta.key

将nginx中的配置修改如下
定义了两个server,第一个server监听80端口,如果是http请求过来的,就会重定向到https

server{
        listen 80;
        server_name fatalblow.top www.fatalblow.top;
        return 301 https://www.fatalblow.top$request_uri;
    }

server {
        listen 443 ssl http2;
        server_name www.fatalblow.top fatalblow.top;
        charset     utf-8;
        access_log      /home/project/blog/conf/nginx_access.log;
        error_log      /home/project/blog/conf/nginx_error.log;
        client_max_body_size 75M;

        ssl on;
        ssl_certificate   /etc/nginx/conf.d/cert/a.pem;
        ssl_certificate_key  /etc/nginx/conf.d/cert/a.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNUL
L:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        
    location /media  {
        alias /home/project/blog/media;  # 指向django的media目录
     }

    location /static {
        alias /static ;
        }

    location / {
        include     /etc/nginx/uwsgi_params;
        uwsgi_pass  0.0.0.0:8080;  # 这个一定要和uwsgi.ini文件的端口一致
    }
    }

然后输入启动命令,指定配置文件

uwsgi -c /home/project/blog/conf/default.conf
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将 HTTPS 证书添加到 Android 应用程序中的 `MediaPlayer`,需要完成以下步骤: 1. 将证书文件添加到 Android 项目的 `res/raw` 目录中。可以在 Android Studio 的 Project 视图中找到该目录并将证书文件复制到该目录中。 2. 在代码中获取证书文件的输入流,并将其加载到 `SSLContext` 中: ```java InputStream inputStream = getResources().openRawResource(R.raw.your_certificate_file); CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(inputStream); KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); keyStore.load(null, null); keyStore.setCertificateEntry("your_alias", certificate); TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); trustManagerFactory.init(keyStore); SSLContext sslContext = SSLContext.getInstance("TLS"); sslContext.init(null, trustManagerFactory.getTrustManagers(), null); ``` 3. 在 `MediaPlayer` 中设置 `DataSource` 时,使用 `HttpsURLConnection` 并设置其 `SSLSocketFactory`: ```java URL url = new URL("https://your_media_file_url"); HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection(); httpsURLConnection.setSSLSocketFactory(sslContext.getSocketFactory()); mediaPlayer.setDataSource(httpsURLConnection.getURL().toString()); mediaPlayer.prepareAsync(); ``` 这样,你的 `MediaPlayer` 就可以使用 HTTPS 证书加载加密的媒体文件了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值