django - 使用 NGinx 路由网站(PHP 和 Django)
问题描述
我的第一个网站在显示主页的 Apache Web 服务器下正常运行,可以通过http://localhost:8989/访问它。
我还有我的第二个网站,它是一个 Django 应用程序(在 uWsgi 下运行)在http://localhost:8000/上正常运行,并且每个页面都正确显示。
现在,我想使用 Nginx 来服务两个网站的所有静态资源。由于我想为这两个网站使用我唯一的一个域,即 www.test.com (TEST),我将其配置如下:
server {
listen 80;
server_name _;
location / {
root /var/www/statics;
index index.html index.htm;
try_files $uri @home;
}
#for request: https://www.test.com/ , it should go to the 1st website
location @home {
proxy_pass http://localhost:8989;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
#for request: https://www.test.com/abc , it should go to the 2st website
location /abc {
rewrite ^/abc(.*) $1 break;
try_files $uri @abc;
}
location @abc {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
不知道有没有更好的配置,我的要求如下:
- 有一个在 TEST 上运行的网站
- 如果我去 TEST/ --> 它应该显示我的第一个网站(PHP 应用程序)
- 如果我去 TEST/abc --> 它应该显示我的第二个网站(Django 应用程序)和所有没有问题的链接,并且可能不需要更改其 settings.py 或 urls.py 文件
- 我想管理来自 NGinx 服务器的所有重新路由
上述配置的当前状态:
- 如果我去 TEST/,它会被路由到我的第一个网站(这是正确的)
- 如果我去 TEST/abc,它会被路由到我的第二个网站(这是正确的)。但是,每当我点击其中的超链接时,例如在http://localhost:8000/login上看到一个页面(运行正常),通过这种重新路由配置,它现在变成了http://www。 test.com/login,在这种情况下显示错误 404:未找到,并且似乎请求没有转到第二个网站而是第一个网站(这是错误的)。预期的请求应该是http://www.test.com/abc/login(第二个网站内的所有请求现在应该以某种方式通过以 "abc/" 开头的 uri 访问)
有没有人有过这样的经历?在 Nginx 配置中应该执行什么正确的正则表达式或重写?还是应该在 Django 配置上完成?我不想更改 Django 项目的每个应用程序的 urls.py,但如果实际上需要对其 settings.py 文件执行一个简单的技巧,那就太好了。请帮忙
解决方案
推荐阅读
- macos - 使用 ./configure --prefix=/usr/local 从终端安装 SQLite 时出现问题
- javascript - 修复 AWS API 网关不存在的 CORS“对预检的响应 ...”标头并放大
- blockchain - 超级账本锯齿提交交易错误“试图获取未经授权的地址”,状态无效
- windows - 当我们插入/拔出交流电缆时的事件
- java - 同一目的地的多个事件的合同测试
- ubuntu - 集成 Intel (Sandy Bridge) 视频的最佳性能
- git - GIT 在 Visual Studio Code 的集成终端中不再工作
- postgresql - 通过 sql 迁移授予 Flyway 授权 - 不起作用
- r - 如何在不显示先前值的情况下更改绘图中的 x-label?
- swiftui - 我可以将@State 用于多个 View 元素吗?[SwiftUI]