【问题标题】:How do I configure nginx with backend and frontend?如何使用后端和前端配置 nginx?
【发布时间】:2021-05-19 18:35:57
【问题描述】:

我有一个简单的 vue.js 和 django(作为 REST API)应用程序,我想与 nginx 结合使用。目前前端正在工作,但后端没有。这是我的 nginx 配置:

server {
    listen 80;
    location / {
        root   /usr/share/nginx/html/;
        index  index.html index.htm;
    }
    location /api {
        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://localhost:8000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

访问 localhost 对静态文件有效,但 localhost/api 会导致网关错误: [error] 29#29: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: , request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "localhost"

此外,尝试通过前端 (axios) 访问 localhost/api 只会返回“您需要 javascript 来显示此页面”站点,这只是前端的一部分。

在 docker 和 nginx 之外单独运行后端,在 localhost:8000 上运行良好。

我该怎么做才能让它发挥作用?不一定非要这样,只要前端和后端可以通信即可。

【问题讨论】:

    标签: django nginx webserver


    【解决方案1】:

    你说你在运行 Docker?然后您需要将localhost 更改为运行您的后端的容器名称

    【讨论】:

      猜你喜欢
      • 2019-09-17
      • 2019-05-10
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-19
      • 2021-02-03
      • 2020-12-27
      相关资源
      最近更新 更多