【发布时间】:2022-01-07 22:50:00
【问题描述】:
我正在尝试使用 Nginx 在我的 VPS 上运行 django 应用程序和 angular 应用程序。以下是我的配置文件代码:
server {
listen 80;
server_name www.the-patron.com the-patron.com;
charset utf-8;
location = /favicon.ico { access_log off; log_not_found off; }
location /staticfiles/ {
root /root/thepatron/The-Patron-Backend;
}
# Django Backend
location /back/ {
include proxy_params;
proxy_pass http://unix:/root/thepatron/The-Patron-Backend/thepatron.sock;
}
# Angular Frontend
location / {
proxy_pass http://localhost:4200/;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_cache_bypass $http_upgrade;
}
}
我的 Angular 应用程序运行良好,而 Django 应用程序却没有。如果我在第 12 行将 location /backend/ 更改为 location / 并在第 18 行将 location / 更改为 location /frontend/,那么我将让 Django 应用程序运行而 Angular 应用程序不会运行。
如何同时运行两者并更改它们的位置?
【问题讨论】:
-
我认为完全不正确的一件事是您没有将 Angular 应用程序作为静态文件提供服务,您应该通过创建 Angular 应用程序构建并通过 ngnix 提供静态文件来做到这一点。
-
@Vipulw 我该怎么做?
-
您应该构建您的 Angular 应用程序并提供静态文件。此外,如果您的 Django 后端在端口号上运行,则需要使用上述用于 Angular 的配置
标签: nginx nginx-reverse-proxy nginx-config nginx-location