反向代理Nginx本身

笑脸

我目前正在托管一个单页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

qräbnö

只是一个猜测。

您想在上面服务/吗?

如果不是-很简单:

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_filtersubs_filter/support重写源代码的链接上,即时让他们将永远不会使用/

如果您在源代码(或proxy_pass后端)中包含重定向,则需要proxy_redirect和/或Lua来即时捕获和更改它们。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章