我有一个Angular构建和一个Django后端,提供了在一台服务器上运行的API。我已经在nginx中配置了它们,并且前端具有到后端服务器的代理。
后端在url 127.0.0.1:8000/api上运行,而前端在localhost上运行
配置:
worker_processes 1;
events {
worker_connections 1024;
}
http {
server {
listen 80;
charset utf-8;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
gzip on;
gzip_types text/css text/javascript application/x-javascript application/json;
location /api {
proxy_pass http://127.0.0.1:8000/api;
}
location / {
try_files $uri $uri/ /index.html;
}
}
}
现在,当我从前端进行任何api调用时,都会收到502 Bad Gateway错误
获取http://localhost/api/posts/post/management 502(错误网关)
答案 0 :(得分:0)
我将转换器配置为
upstream backend {
server backend:8000;
}
server {
listen 80;
charset utf-8;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm;
include /etc/nginx/mime.types;
gzip on;
gzip_types text/css text/javascript application/x-javascript application/json;
# backend urls
location ~ ^/(admin|api|media) {
proxy_redirect off;
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
}
# static files
location /static {
proxy_pass http://backend;
}
# frontend
location / {
try_files $uri $uri/ /index.html;
}
工作正常