美文网首页开发技术散集
Nginx配置https并支持下载ipa、apk、plist文件

Nginx配置https并支持下载ipa、apk、plist文件

作者: henry技术探索记录员 | 来源:发表于2020-03-10 23:47 被阅读0次

1. 购买阿里云免费Symantec证书

申请证书审核通过后,下载对应Nginx下的.key和.pem证书

2 CentOS 7配置Nginx

先安装依赖

[root@izuf6a4o38c6o9qcshkw40z local]# yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

Ubuntu用apt-get和Centos用yum安装的nginx,安装目录在/etc/nginx/下,nginx.conf也在该目录下。

3 配置nginx反向代理https

进入/etc/nginx/,将之前申请的SSL证书的两个文件放入该目录。修改nginx.conf文件,添加下面的配置。(nginx.conf文件预留了443端口的监听配置,也可以把它的注释去掉,直接修改。)

server {
        listen       443 ssl;
        server_name  localhost;
 
        ssl_certificate      xxx.pem;
        ssl_certificate_key  xxx.key;
 
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
 
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
 
        location / {
            proxy_pass http://localhost:8080/;
        }
}

xxx.pemxxx.key填写你自己的文件名。proxy_pass http://localhost:8080表示访问代理到tomcat或者其他容器的8080端口。nginx的location用法可以自行百度或Google。

4. 配置mime.types,支持ipa、apk和plist直接下载

/etc/nginx/下找到mime.types,打开编辑,添加2行以支持ipa、apk程序包的下载:

application/vnd.android.package-archive         apk;
application/iphone                              pxl ipa;

nginx.conf中的http web的location配置中根据文件格式下载,添加对plist的支持

# 设置根据文件格式下载,而不是直接在浏览器打开显示    
if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|plist)$){
    add_header Content-Disposition: 'attachment;';
}

location的整体配置:

location / {
    root       /usr/local/nginx_webs/appleSign;     # 静态页面目录
    index      index.html;    # 默认首页

    # 设置根据文件格式下载,而不是直接在浏览器打开显示    
    if ($request_filename ~* ^.*?\.(txt|doc|pdf|rar|gz|zip|docx|exe|xlsx|ppt|pptx|plist)$){
        add_header Content-Disposition: 'attachment;';
    }

    # 用户浏览器端的缓存设置
    location ~* \.(css|js|jpg|jpeg|gif|png|swf|htm|html|json|xml|svg|woff|ttf|eot|map|ico|ipa|apk)$ {
    expires 1h;
        if (-f $request_filename) {  #存在文件则直接访问,不需要反向代理
            break;
        }
    }
         
    if ( !-e $request_filename) {  #文件和目录不存在,判断为http的api请求,反向代理给tomcat server处理
        #proxy_pass       https://iossign.koovi.com:88/YueJianShareServer/;
    }
    client_max_body_size    256m;
}

然后使用./nginx -s reload重启nginx即可。

5. web访问目录授权

如果浏览器访问ipa、plist的url提示403 forbidden错误,而访问html文件正常时,很可能是web目录权限不足,修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决:

1.    chmod -R 777 /data
2.    chmod -R 777 /data/www/

相关文章

网友评论

    本文标题:Nginx配置https并支持下载ipa、apk、plist文件

    本文链接:https://www.haomeiwen.com/subject/jikodhtx.html