简体   繁体   English

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

[英]How to set correct content-type for apple-app-site-association file on Nginx/Rails

In order to set up universal links for an iOS app, I have created an apple-app-site-association file, and placed it in the /public directory of my Rails app.为了为 iOS 应用程序设置通用链接,我创建了一个 apple-app-site-association 文件,并将它放在我的 Rails 应用程序的 /public 目录中。

I can curl it at the correct address, but it returns the wrong content type.我可以将它卷曲到正确的地址,但它返回了错误的内容类型。 Instead of application/json or application/pkcs7-mime it returns application/octet-stream , as you can see in the response here:它返回application/octet-stream ,而不是application/jsonapplication/pkcs7-mime ,如您在此处的响应中所见:

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*"
        ]
      }
    ]
  }

I am attempting to specify a Content-Type in the nginx configuration:我试图在 nginx 配置中指定一个 Content-Type:

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

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

I have saved this change and restarted nginx.我已保存此更改并重新启动了 nginx。 This doesn't make any difference to the response from curl.这对 curl 的响应没有任何影响。 I've also tried location /public/apple-app-site-association {} and a few other variations, to no effect.我还尝试了location /public/apple-app-site-association {}和其他一些变体,但没有效果。

What is the correct way to set up nginx to deliver this file with the correct content type?设置 nginx 以提供具有正确内容类型的文件的正确方法是什么?

Add in nginx:在 Nginx 中添加:

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

The above didn't work for me but thought I'd post here in case it helps anyone else.以上对我不起作用,但我想我会在这里发布以防它对其他人有帮助。 I needed apple-app-site-association to be application/json so changed the filename to apple-app-site-association.json then added this to .htaccess within /.well-known我需要 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] 

This worked for me within .htaccess:这在 .htaccess 中对我有用:

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

It turns out the nginx configuration file described two servers, and I was adding the location snippet to the wrong one.原来 nginx 配置文件描述了两台服务器,而我将位置片段添加到了错误的一台。

When I added it to the correct one and reloaded nginx, the file was returned with the expected content-type:当我将它添加到正确的一个并重新加载 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*"
    ]
  }
]

} }

In my case, I'd create a separate nginx for "apple-app-site-association"就我而言,我会为“apple-app-site-association”创建一个单独的 nginx

  1. locate this file to /usr/share/nginx/html/apple-app-site-association and in /usr/share/nginx/html/.well-known/apple-app-site-association将此文件定位到 /usr/share/nginx/html/apple-app-site-association 和 /usr/share/nginx/html/.well-known/apple-app-site-association

  2. edit, /etc/nginx/nginx.conf and set default_type application/json;编辑 /etc/nginx/nginx.conf 并设置 default_type application/json;

  3. that is it.就是这样。

to test it,测试它,

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

    HTTP/1.1 200 OK Server: nginx/1.19.3 Date: Thu, 29 Oct 2020 13:34:00 GMT Content-Type: application/json Content-Length: 198 Last-Modified: Fri, 23 Oct 2020 13:54:03 GMT Connection: keep-alive ETag: "5f92e07b-c6" Accept-Ranges: bytes HTTP/1.1 200 OK 服务器:nginx/1.19.3 日期:2020 年 10 月 29 日星期四 13:34:00 GMT 内容类型:application/json 内容长度:198 最后修改时间:2020 年 10 月 23 日星期五 13:54: 03 GMT 连接:保持活动 ETag:“5f92e07b-c6”接受范围:字节

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM