简体   繁体   English

nginx或apache如何从/ subfolder提供内容,无论您使用的是什么URL

[英]nginx or apache how to serve content from /subfolder no matter what url you are at

I have an frontend app, written in Angular, hosted at http://localhost/subfolder/myapp 我有一个用Angular编写的前端应用程序,托管在http:// localhost / subfolder / myapp

When I go to the app, it redirects to route1, the url appears as: http://localhost/subfolder/myapp/route1 当我转到应用程序时,它重定向到route1,URL显示为: http:// localhost / subfolder / myapp / route1

But when I refresh the browser, I get 404 not found error. 但是,当我刷新浏览器时,出现404 not found错误。 I think that's because there is no physical index.html in /subfolder/myapp/route1 and /subfolder/myapp/route1 is not a physical folder. 我认为这是因为/ subfolder / myapp / route1中没有物理index.html,并且/ subfolder / myapp / route1不是物理文件夹。

So I wrote a rewrite rule 所以我写了一个重写规则

rewrite ^/subfolder/myapp/.+$ /subfolder/myapp last;

But since when I go to http://localhost/subfolder/myapp I am redirected to http://localhost/subfolder/myapp/route1 , and the rule is redirecting me back to http://localhost/subfolder/myapp , I am in an infinite redirect state (too many redirect problem) 但是因为当我转到http:// localhost / subfolder / myapp时,我被重定向到http:// localhost / subfolder / myapp / route1 ,并且规则将我重定向回http:// localhost / subfolder / myapp ,所以我我处于无限重定向状态(太多重定向问题)

What can I do in nginx to solve my problem? 我可以在Nginx中做什么来解决我的问题? Or what about Apache? 还是Apache?

Figured this out: 想通了:

    location /subfolder/myapp/ {
        try_files $uri /subfolder/myapp/index.html;
    }

    location /subfolder2/myapp/ {
        try_files $uri /subfolder2/myapp/index.html;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM