我目前正在托管一个单页React应用,该应用托管在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
?
只是一个猜测。
您想在上面服务/
吗?
如果不是-很简单:
server
{
listen 80;
server_name localhost;
location /support/
{
alias /var/www/html/;
try_files $uri $uri/ /index.html;
}
location /
{
return 303 http://localhost/support$request_uri;
}
}
如果它不起作用,请在结尾斜杠之间随意摆弄(使用或不使用它们通常会有所不同)。
使用alias
代替,root
这样/support
就不会添加到该/var/www/html
文件夹中。
一切都重定向到/support
。
如果您想提供与以下服务/
不同的服务/support
:
使用sub_filter
或subs_filter
在/support
重写源代码的链接上,即时让他们将永远不会使用/
。
如果您在源代码(或proxy_pass
后端)中包含重定向,则需要proxy_redirect
和/或Lua来即时捕获和更改它们。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句