【发布时间】:2020-12-14 19:37:22
【问题描述】:
我目前正在托管一个托管在 URL 根目录中的单页响应应用程序,如下所示:
server {
listen 80;
server_name localhost;
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
我需要将站点放在 AWS 弹性负载均衡器后面,同时更改路径,以便所有内容都在 /support 目录中,例如http://example.com/index.html -> http://example.com/support/index.html.
AWS ALB 不支持 URL 重写,因此我必须在服务器上的 nginx 配置中执行此操作。首先,我尝试将配置更改为:
server {
listen 80;
server_name localhost;
location /support {
alias /var/www/html;
try_files $uri /index.html;
}
}
这种方法有效,但 javascript 内容中的 URL 不包含 /support 路径(例如,它们包含 http://example.com/script.js 而不是 http://example.com/support/script.js)。
然后我尝试创建一个反向代理配置以将 /support 代理到 /,这很遗憾地将 nginx 置于无限循环中,直到它耗尽工作线程:
server {
listen 80;
server_name localhost;
location /support {
proxy_pass http://localhost:80;
}
location / {
root /var/www/html;
try_files $uri /index.html;
}
}
我很困惑为什么请求会进入反向代理循环? proxy_pass 不应该在代理请求之前删除/support 前缀,因此它不应该被/support 位置再次“捕获”吗?
【问题讨论】:
-
proxy_pass不会重写任何内容。如果可以,接收请求的任何服务器如何知道URI正确响应?
标签: nginx