如何在Nginx / Rails上为apple-app-site-association文件设置正确的内容类型

时间:2016-11-21 16:43:13

标签: ios nginx ios-universal-links

为了设置iOS应用的通用链接,我创建了一个apple-app-site-association文件,并将其放在我的Rails应用程序的/ public目录中。

我可以在正确的地址卷曲它,但它返回错误的内容类型。正如您在此处的回复中所看到的那样,它返回application/json而不是application/pkcs7-mimeapplication/octet-stream

curl -i https://example.com/apple-app-site-association

HTTP/1.1 200 OK
Server: nginx/1.10.1
Content-Type: application/octet-stream
Content-Length: 245
Last-Modified: Mon, 21 Nov 2016 12:45:00 GMT
Strict-Transport-Security: max-age=31536000

{
  "applinks": {
    "apps": [],
    "details": [
      {
        "appID": "APPPREFIX.com.mycompany.app",
        "paths": [
          "/home*"
        ]
      }
    ]
  }

我正在尝试在nginx配置中指定Content-Type:

/etc/nginx/sites/sites-available/sitename: 

server {
   ...
   location /apple-app-site-association {
      default_type application/pkcs7-mime;
   }
}

我保存了此更改并重新启动了nginx。这对curl的响应没有任何影响。我还尝试了location /public/apple-app-site-association {}和其他一些变体,但没效果。

设置nginx以正确的内容类型传送此文件的正确方法是什么?

5 个答案:

答案 0 :(得分:2)

以上内容不适用于我,但我认为我会在这里发表,以免对其他人有所帮助。我需要apple-app-site-association为application / json,因此将文件名更改为apple-app-site-association.json,然后将其添加到/.well-known

中的.htaccess中
RewriteEngine On
RewriteBase /.well-known/

# iOS
RewriteRule ^apple-app-site-association$ apple-app-site-association.json [NC,L] 

答案 1 :(得分:2)

这在.htaccess中对我有用:

<FilesMatch "apple-app-site-association">
    ForceType application/json
</FilesMatch>

答案 2 :(得分:1)

事实证明,nginx配置文件描述了两个服务器,我将位置片段添加到了错误的位置。

当我将其添加到正确的并重新加载nginx时,返回的文件包含预期的内容类型:

HTTP/1.1 200 OK
Server: nginx/1.10.1
Content-Type: application/pkcs7-mime
Content-Length: 245

{
"applinks": {
"apps": [],
"details": [
  {
    "appID": "APPPREFIX.com.mycompany.app",
    "paths": [
      "/home*"
    ]
  }
]

}

答案 3 :(得分:1)

添加nginx:

location ~ /.well-known/apple-app-site-association {
         default_type application/pkcs7-mime;
 }

答案 4 :(得分:0)

就我而言,我将为“ apple-app-site-association”创建一个单独的nginx

  1. 将此文件定位到/ usr / share / nginx / html / apple-app-site-association 并在/usr/share/nginx/html/.well-known/apple-app-site-association

  2. edit,/ etc / nginx / nginx.conf 并设置default_type application / json;

  3. 就是这样。

进行测试

  1. curl -i localhost / apple-app-site-association

    HTTP / 1.1 200确定 伺服器:nginx / 1.19.3 日期:2020年10月29日,星期四,格林尼治标准时间 内容类型:application / json 内容长度:198 上次修改时间:2020年10月23日,星期五,格林尼治标准时间 连接:保持活动状态 ETag:“ 5f92e07b-c6” 接受范围:字节

    { “应用链接”:{ “应用”: [], “细节”: [ { “ appID”:“ APPPREFIX.com.mycompany.app”, “路径”:[“ *”] } ] } }