[英]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.