NGINX服务器上的API文档

发布于 2025-02-10 04:51:42 字数 2204 浏览 1 评论 0原文

我在Localhost:8020上运行了一个FastApi,并且可以使用以下内容访问文档:Localhost:8020/docs和OpenAPI.JSON文件在Localhost:8020/OpenAPI.JSON。

我想将Localhost:8020/Docs重定向到Localhost:带有NGINX的8080/DOCS。这是我的nginx.conf:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    sendfile on;

    upstream docss {
    server 172.17.0.1:8020;
    }


    server {
    client_max_body_size 500M;
    proxy_connect_timeout   600;
    proxy_send_timeout      600;
    proxy_read_timeout      600;
    send_timeout            600;
    client_body_timeout     600;
    listen       8080;
    resolver 127.0.0.11;
    autoindex off;

    server_name localhost;
    server_tokens off;
    location /docs/ {
       proxy_pass http://docss/docs;

    }

    }
}

使用上述配置,当我打开Localhost:8080/docs/时,我会收到此错误:

无法加载API定义。错误隐藏未找到错误错误 /openapi.json

和Nginx docker日志中,我收到此错误:

172.21.0.1--- [23/jun/2022:18:10:3​​5 +0000]“ get/docs/http/http/1.1” 200 931“ - ”“ Mozilla/5.0(Windows NT 10.0; Win64; x64) Applewebkit/537.36 (Khtml,像壁虎一样)Chrome/102.0.0.0 Safari/537.36”

2022/06/23 18:10:36 [错误] 23#23: *1 Open() “ /etc/nginx/html/openapi.json”失败(2:无文件或目录), 客户端:172.21.0.1,服务器:localhost,请求:“ get /openapi.json http/1.1“,主机:“ localhost:8080”,推荐人: “ http:// localhost:8080/docs/”

172.21.0.1--- [23/jun/2022:18:10:10:3​​6 +0000] 5.0(Windows NT 10.0; Win64; x64)AppleWebkit/537.36(Khtml,像壁虎一样) Chrome/102.0.0.0 Safari/537.36“

想这意味着可以在“ /etc/nginx/html/openapi.json”上找到OpenApi.json。

我 ”的上游连接。

.json是

docker run --link ds-ai-ocr_web_1 --link ds-ai-ocr_api_1 --net ds-ai-ocr_default --name nginx -v c:/Users/ab/Documents/ds-nginx-conf:/etc/nginx -p 8080:8080 -d nginx

“ /etc/nginx/html/openapi.json 这:

 docker run --link ds-ai-ocr_web_1 --link ds-ai-ocr_api_1 --net ds-ai-ocr_default --name nginx -v c:/Users/amd/Documents/ds-nginx-conf:/etc/nginx -v http://172.17.0.1:8020/openapi.json:/etc/nginx/html -p 8080:8080 -d nginx

I have a Fastapi running on localhost:8020 and I have access to documentation with:localhost:8020/docs and the openapi.json file is in localhost:8020/openapi.json.

I want to redirect localhost:8020/docs to localhost:8080/docs with nginx. Here is my nginx.conf:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    sendfile on;

    upstream docss {
    server 172.17.0.1:8020;
    }


    server {
    client_max_body_size 500M;
    proxy_connect_timeout   600;
    proxy_send_timeout      600;
    proxy_read_timeout      600;
    send_timeout            600;
    client_body_timeout     600;
    listen       8080;
    resolver 127.0.0.11;
    autoindex off;

    server_name localhost;
    server_tokens off;
    location /docs/ {
       proxy_pass http://docss/docs;

    }

    }
}

with the above config, when I open localhost:8080/docs/, I receive this error:

Failed to load API definition. Errors Hide Fetch error Not Found
/openapi.json

and in the nginx docker log, I receive this error:

172.21.0.1 - - [23/Jun/2022:18:10:35 +0000] "GET /docs/ HTTP/1.1" 200 931 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36"

2022/06/23 18:10:36 [error] 23#23: *1 open()
"/etc/nginx/html/openapi.json" failed (2: No such file or directory),
client: 172.21.0.1, server: localhost, request: "GET /openapi.json
HTTP/1.1", host: "localhost:8080", referrer:
"http://localhost:8080/docs/"

172.21.0.1 - - [23/Jun/2022:18:10:36 +0000] "GET /openapi.json HTTP/1.1" 404 548 "http://localhost:8080/docs/" "Mozilla/5.0 (Windows
NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/102.0.0.0 Safari/537.36"

I guess it means that it couldnot find openapi.json at "/etc/nginx/html/openapi.json".

So my question is that how can I import 172.17.0.1:8020/openapi.json which is the upstream connection to "/etc/nginx/html/openapi.json".

This is the command I use to create nginx docker:

docker run --link ds-ai-ocr_web_1 --link ds-ai-ocr_api_1 --net ds-ai-ocr_default --name nginx -v c:/Users/ab/Documents/ds-nginx-conf:/etc/nginx -p 8080:8080 -d nginx

I guess I should mount openapi.json through this command but it doesn't work like this:

 docker run --link ds-ai-ocr_web_1 --link ds-ai-ocr_api_1 --net ds-ai-ocr_default --name nginx -v c:/Users/amd/Documents/ds-nginx-conf:/etc/nginx -v http://172.17.0.1:8020/openapi.json:/etc/nginx/html -p 8080:8080 -d nginx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文