通過(guò)Https訪問(wèn)Parse Server和Parse Dashboard

使用Docker在MacOS上部署Parse Server和Parse Server Dashboard這篇文章提到,我們的部署只能通過(guò)Http訪問(wèn),而目前幾乎所有的服務(wù)器訪問(wèn)都要求使用Https訪問(wèn),所以本篇我們著重介紹如何通過(guò)Https訪問(wèn)Parse Server和Parse Dashboard

先說(shuō)結(jié)論和實(shí)現(xiàn)方案,最后再討論一些其他的探索過(guò)程。

結(jié)論:通過(guò)Nginx的反向代理來(lái)實(shí)現(xiàn)

我們先來(lái)看Parse Server的Https訪問(wèn)

參考使用Docker在MacOS上部署Nginx并配置Https,我們映射了Nginx的配置文件到本地,即工程目錄下的./nginx/conf.d/default.conf,編輯該文件的https部分:

server {
   listen  443 ssl;
   server_name  localhost; # 換成自己的host

   ssl_certificate  /etc/nginx/conf.crt/localhost.crt;             #指定證書(shū)位置(通過(guò)目錄掛載到容器內(nèi)部)
   ssl_certificate_key  /etc/nginx/conf.crt/localhost.key;         #指定私鑰位置(通過(guò)目錄掛載到容器內(nèi)部)

   location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location /parse/ {
        proxy_pass http://172.17.0.4:1337/parse/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
    }
}

location /parse/部分是新增的,即所有https://localhost/parse/關(guān)聯(lián)的訪問(wèn)都轉(zhuǎn)向http://172.17.0.1:1337/parse/,請(qǐng)注意,這里我們不能再使用localhost了,我們必須使用parse-server docker容器的內(nèi)部IP。

如何獲得parse docker的內(nèi)部IP?

docker ps
docker inspect parse-server

通過(guò)docker ps命令查看容器名字,然后通過(guò)docker inspect 查看該容器的詳細(xì)內(nèi)容,然后您就可以看到IPAddress的內(nèi)容了。

重啟Nginx服務(wù)器

此時(shí)我們使用https://localhost/parse進(jìn)行測(cè)試

curl -X POST \
-H "X-Parse-Application-Id: APPLICATION_ID" \
-H "Content-Type: application/json" \
-d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \
https://localhost/parse/classes/GameScore

返回結(jié)果類(lèi)似,這樣就是執(zhí)行成功了。

{
  "objectId":"Ixb4yaX8ZH",
  "createdAt":"2022-09-22T04:21:08.928Z"
}
parse-dashboard的Https訪問(wèn)

同樣parse-server,我們也通過(guò)修改nginx配置文件來(lái)配置反向代理,使parse-dashboard支持https的訪問(wèn)。

# 增加https部分設(shè)置
server {
   listen  443 ssl;
   server_name  localhost; # 換成自己的host

   ssl_certificate  /etc/nginx/conf.crt/localhost.crt;             #指定證書(shū)位置(通過(guò)目錄掛載到容器內(nèi)部)
   ssl_certificate_key  /etc/nginx/conf.crt/localhost.key;         #指定私鑰位置(通過(guò)目錄掛載到容器內(nèi)部)

   location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location /parse/ {
        proxy_pass http://172.17.0.4:1337/parse/;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_redirect off;
    }

    location /parse-dashboard/ {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://172.17.0.5:4040/parse-dashboard/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
}

注意,這里的/parse-dashboard/是和我們之前在生成parse-dashboard docker時(shí)的--mountPath參數(shù)是一致的。反向代理指向的地址為parse-dashboard docker的http地址。

此外,我們需要額外修改的是parse-dashboard docker的配置文件,即./parse-dashboard/config/parse-dashboard-config.json

{
    "apps": [
        { 
            "serverURL": "https://localhost/parse",
            "appId": "APPLICATION_ID",
            "masterKey": "MASTER_KEY",
            "appName": "Hello World",
            "mountPath": "parse-dashboard"
        }
    ],
    "users": [
        {
            "user":"parse",
            "pass":"password"
        }
    ]
}

"serverURL"由原先的http://localhost:1337/parse修改為https://localhost/parse

此時(shí)我們?cè)L問(wèn)https://localhost/parse-dashboard/就可以正確的訪問(wèn)了

額外的討論

除了反向代理,我們也可以通過(guò)創(chuàng)建基于https進(jìn)行訪問(wèn)的parse-server和parse-dashboard。

parse-dashboard的docker提供了https的支持,我們可以通過(guò)指定--sslKey和--sslCert提供https訪問(wèn)的鑰匙和證書(shū),來(lái)實(shí)現(xiàn),例如:

cd $HOME/parse.server.docker
docker run \
--name parse-dashboard \
-p 4040:4040 \
-v $(pwd)/parse-dashboard/config/parse-dashboard-config.json:/src/Parse-Dashboard/parse-dashboard-config.json \
-v $(pwd)/parse-dashboard/crt:/src/Parse-Dashboard/crt \
-d parseplatform/parse-dashboard:4.1.4 \
--sslKey /src/Parse-Dashboard/crt/localhost.key \
--sslCert /src/Parse-Dashboard/crt/localhost.crt \
--mountPath /parse-dashboard

這是基于parse-dashboard docker內(nèi)部的代碼實(shí)現(xiàn)的,在提供--sslKey和--sslCert參數(shù)的情況下,創(chuàng)建了一個(gè)https的服務(wù),來(lái)啟動(dòng)parse-dashboard。

然而,parse-server的docker目前沒(méi)有支持創(chuàng)建https的服務(wù),因此不能像parse-dashboard那樣。如果我們需要,那么就只能通過(guò)修改parse-server docker的代碼來(lái)實(shí)現(xiàn),這相對(duì)來(lái)說(shuō)會(huì)稍微復(fù)雜一些,單獨(dú)再開(kāi)篇來(lái)細(xì)說(shuō)吧。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容