简体   繁体   English

如何使用后端和前端配置 nginx?

[英]How do I configurate nginx with backend and frontend?

I have a simple vue.js and django (as REST API) application that I want to combine with nginx.我有一个简单的 vue.js 和 django(作为 REST API)应用程序,我想与 ZEE434023CF89D7DFB274ZFD6 结合使用。 Currently the frontend is working, but the backend is not.目前前端正在工作,但后端没有。 Here's my nginx config:这是我的 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;
    }
}

Visiting localhost works for the static files, but localhost/api leads to a bad gateway error: [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"访问 localhost 适用于 static 文件,但 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"

Also, trying to visit localhost/api via the frontend (axios) just returns the 'You need javascript to display this page' site, which is just part of the frontend.此外,尝试通过前端 (axios) 访问 localhost/api 只会返回“您需要 javascript 来显示此页面”站点,这只是前端的一部分。

Running the backend seperately, outside of docker and nginx, works fine on localhost:8000.在 docker 和 nginx 之外单独运行后端,在 localhost:8000 上运行良好。

What can I do to make it work?我该怎么做才能让它发挥作用? It doesn't necessarily have to be done this way, as long as the frontend and backend can communicate.不一定非要这样,只要前端和后端可以通信即可。

You said you running Docker?你说你运行 Docker? Then you need to change localhost to the container name that running your backend.然后,您需要将localhost更改为运行后端的容器名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将文本字段输入从前端发送到后端 - how do I send a text field input from the frontend to the backend 如何使用 HTML/CSS/JS 前端和 Django 和 Flask 后端创建 Django Web 应用程序? - How do I create a Django web application with an HTML/CSS/JS frontend and a Django and Flask backend? 使用 istio 或 nginx 通过前端代理后端 - Proxy backend through frontend using istio or nginx nginx前端服务器未重定向到apache后端 - nginx frontend server is not redirecting to apache backend DjangoRestFramework-如何自定义前端? - DjangoRestFramework - How do I customize the frontend? 在服务器上有一个Django后端。 我还需要采取什么步骤才能将Angular前端移动到那里? - Have a Django backend on server. What steps do I need to take to move my Angular frontend there as well? 如何以更易读的方式显示从后端接收到的数据?我将 react 用于前端 - How can I display the data recieved from the backend in a more readable way?I use react for the Frontend 如何通过 Django 后端将数据从 HDFS 发送到 Angular 5 前端? - How can I send data from HDFS through Django backend to Angular 5 frontend? 如何测试我的 django/react 应用程序的前端? - How do I test the frontend of my django/react app? 如何使用Elasticsearch在Django中从前端编辑/删除数据? - How do i edit/delete data from the frontend in Django with Elasticsearch?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM