怎么用Nginx设置密码来保护以太坊JSON-RPC的API

发布时间:2021-12-13 09:43:50 作者:iii
来源:亿速云 阅读:216

这篇文章主要介绍“怎么用Nginx设置密码来保护以太坊JSON-RPC的API”,在日常操作中,相信很多人在怎么用Nginx设置密码来保护以太坊JSON-RPC的API问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”怎么用Nginx设置密码来保护以太坊JSON-RPC的API”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

Go Ethereum(geth)是以太坊节点最受欢迎的软件。其他流行的以太坊实现是Parity和cpp-ethereum等。分布式应用程序(Dapps)是JavaScript编码的网页,通过JSON-RPC API协议连接到任何这些以太坊节点软件,该协议是在HTTP协议之上自行运行的。

geth或没有节点软件本身不提供安全网络。将Ethereum JSON-RPC API暴露给公共Internet是不安全的,因为即使禁用私有API,这也会为琐碎的拒绝服务攻击打开一扇门。节点软件本身不需要提供安全的网络原语,因为这种内置功能会增加复杂性并为关键区块链节点软件增加攻击面。

Dapps本身是纯客户端HTML和JavaScript,不需要任何服务器,它们可以在任何Web浏览器中运行,包括移动和嵌入式浏览器,如Mist钱包内的一个。

使用Nginx代理作为HTTP基本身份验证器

有几种方法可以保护对HTTP API的访问。最常见的方法包括HTTP头中的API令牌,基于cookie的身份验证或HTTP基本访问身份验证。

HTTP基本身份验证是HTTP协议的一个非常古老的功能,其中Web浏览器打开一个本机弹出对话框,询问用户名和密码。它本质上的保护是有限的,但非常容易实现,非常适合需要为有限的互联网受众暴露私有Dapp的用例。这些用例包括显示Dapp演示,私有和许可的区块链应用程序或将以太坊功能作为软件即服务解决方案的一部分。

Nginx

Nginx是最受欢迎的开源Web服务器应用程序之一。我们将展示如何设置Nginx Web服务器,以便它使用HTTP Basic Auth私下为你的Dapp(HTML文件)和geth JSON-RPC API提供服务。

我们假设Ubuntu 14.04更新的Linux服务器。文件位置可能取决于使用的Linux发行版。

安装Nginx

在Ubuntu Linux 14.04或更高版本上安装Nginx:

sudo apt install nginx apache2-utils

配置Nginx

我们假设我们编辑默认的网站配置文件/etc/nginx/sites-enabled/default。我们使用proxy_pass指令与在localhost:8545中运行的geth进行通信:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;
    server_name demo.example.com;


    # Geth proxy that password protects the public Internet endpoint
    location /eth {
        auth_basic "Restricted access to this site";
        auth_basic_user_file /etc/nginx/protected.htpasswd;

        # Proxy to geth note that is bind to localhost port                        
        proxy_pass http://localhost:8545;            
    }

    # Server DApp static files
    location / {
        root /usr/share/nginx/html;
        index index.html

        auth_basic "Restricted access to this site";
        auth_basic_user_file /etc/nginx/protected.htpasswd;
    }       
}

使用密码创建HTTP Basic Auth用户演示:

sudo htpasswd -c /etc/nginx/protected.htpasswd demo

配置geth

开始使用geth守护进程的最简单方法是在UNIX screen中运行它:

screen

geth  # Your command line parameters here

退出screen 使用CTRL+A, D

请参阅geth private testnet说明

配置Dapp

在你的Dapp中,使web3.js使用/eth端点:

  function getRPCURL() {

    // ES2016 
    if(window.location.href.includes("demo.nordledger.com")) {      
      // Password protected geth deployment
      return "http://demo.nordledger.com/eth"

    } else {
        // Localhost development
      return "http://localhost:8545";  
    }
  }

  // ...

  web3.setProvider(new web3.providers.HttpProvider(getRPCURL()));

部署Dapp

将DApp文件复制到服务器上的/usr/share/nginx/html。这包括index.html以及相关的JavaScript和CSS资源。

Bonus - 部署shell脚本示例:

#!/bin/bash
#
# A simple static HTML + JS deployment script that handles Nginx www-data user correclty.
# Works e.g. Ubuntu Linux Azure and Amazon EC2 Ubuntu server out of the box.
#

set -e
set -u

# The remote server we are copying the files using ssh + public key authentication.
# Specify this in .ssh/config
REMOTE="nordledger-demo"

# Build dist folder using webpack
npm run build

# Copy local dist folder to the remote server Nginx folder over sudoed
# Assum the default user specified in .ssh/config has passwordless sudo
# https://crashingdaily.wordpress.com/2007/06/29/rsync-and-sudo-over-ssh/
rsync -a -e "ssh" --rsync-path="sudo rsync" dist/* --chown www-data:www-data $REMOTE:/usr/share/nginx/html/

重启Nginx

为Nginx做一次硬重启:

service nginx stop service nginx start

测试并迭代

访问网站,看看您的Dapp是否连接到代理的Geth。

检查/var/log/nginx/error.log以获取详细信息。

如果从/eth端点获得502 Bad Gateway,请确保geth正在作为服务器上的后台进程正常运行。

到此,关于“怎么用Nginx设置密码来保护以太坊JSON-RPC的API”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 以太坊代币空投怎么实现
  2. 以太坊智能合约怎么写

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

nginx json-rpc api

上一篇:Nginx 是怎么实现高并发

下一篇:怎么利用Plex和Syncthing搭建媒体中心

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》