如何为ipfs网关设置nginx?

时间:2019-07-11 19:14:05

标签: nginx ipfs

我需要通过安装了certbot的nginx来创建 ipfs http网关,并将 websockets重定向到端口9999 ,但是我无法使其按预期工作。

用于运行websocket服务的代码位于“ location /”上。 ipfs网关的代码在“ location / ipfs”处。

server {



location /ipfs {
        proxy_pass http://0.0.0.0:9001;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        allow all;
     }


location / {
proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;

        proxy_redirect http://0.0.0.0:9999/ /;
        proxy_redirect ws://0.0.0.0:9999/ /;
        proxy_pass http://0.0.0.0:9999/;
        proxy_read_timeout 86400s;
        proxy_send_timeout 86400s;
        allow all; # Any IP can perform any other requests

    proxy_set_header Connection "Upgrade";

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Cache-Control,Content-Type,Authorization';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'application/json charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }

     if ($request_method = 'POST') {
 #       add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'Content-Type,Authorization';
     }

     if ($request_method = 'GET') {

  #      add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
     }

}


    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/recall.network/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/recall.network/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}


server {
    if ($host = recall.network) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name recall.network;
    return 404; # managed by Certbot


}

我的自定义代码:

location /ipfs {
        proxy_pass http://0.0.0.0:9001;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        allow all;
     }

无法正常工作

我想通过以下网址访问ipfs网关:https:recall.network/ipfs/ 哈希*

1 个答案:

答案 0 :(得分:0)

解决方案是在位置添加正确的前缀^〜。

location ^~ /ipfs {
        proxy_pass http://0.0.0.0:9001;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        allow all;
     }