Nextcloud 设置位置地址时出错 定位 天气

Linux   2023-02-21 18:54   2397   1  

分析原因

Nextcloud需要访问国外网站接口,所以导致国内机器获取失败或者超时。

解决方法

  • 用国外服务器

  • 使用代理

找到源代码中的地址

  • 查看配置文件

[root@bj-a-web Service]# pwd
/data/nextcloud/apps/weather_status/lib/Service
[root@bj-a-web Service]# ls WeatherStatusService.php 
WeatherStatusService.php
  • 相关的URL

https://nominatim.openstreetmap.org/reverse
https://api.opentopodata.org/v1/srtm30m
https://api.met.no/weatherapi/locationforecast/2.0/compact

改成

http://nominatim.openstreetmap.9421.ltd/reverse
http://api.opentopodata.9421.ltd/v1/srtm30m
http://api.met.9421.ltd/weatherapi/locationforecast/2.0/compact
  • 因为改写后的接口是好的,所以可以直接用了。

代理接口实现的方法

  • nginx(openresty) + lua

[root@TKY ~]# cat /usr/local/openresty/nginx/conf/www/proxy.conf 
    server {
        listen       80;
        server_name  nominatim.openstreetmap.9421.ltd api.opentopodata.9421.ltd api.met.9421.ltd ;

        default_type  application/json;

        access_log  logs/proxy_access.log  main;
        error_log  logs/proxy_error.log;

        location / {
            # 代替 nginx 响应正文
            content_by_lua '
                local ngx_host = ngx.var.host        -- 获取当前请求的域名
                local ngx_uri = ngx.var.request_uri    -- 获取url后面的路径参数
                local curl_host = ""                -- 设置一个变量
                if ( ngx_host  == "nominatim.openstreetmap.9421.ltd" )  -- 判断当前域名
                then
                        curl_host = "https://nominatim.openstreetmap.org"    -- 改写curl_host 变量
                elseif ( ngx_host  == "api.opentopodata.9421.ltd" )
                then
                        curl_host = "https://api.opentopodata.org"
                elseif ( ngx_host  == "api.met.9421.ltd" )
                then
                        curl_host = "https://api.met.no"
                else
                        ngx.exit(ngx.ERROR)     -- 按照逻辑 这步应该不会出现 除非 nginx 配置出错了
                end
                local next_url = curl_host .. ngx_uri    -- 拼接URL
                local shell_cmd_1 = [[curl -A "NextcloudWeatherStatus/1.5.0 nextcloud.com" "]]  -- 拼接 shell 命令
                local shell_cmd_2 = [["]]
                local shell_cmd = shell_cmd_1 .. next_url .. shell_cmd_2 -- 最终拼接好的 shell 命令 
                local cmd_ini = io.popen(shell_cmd)    -- 使用 io.popen 这个方法 执行shell 命令 可以获取到执行结果
                local resp = cmd_ini:read("*all")    -- 获取执行结果
                ngx.say(resp)    -- ngx 网页输出
            ';
        }
    }


博客评论
匿名用户
说:

没用,一样无法设置天气

1
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。