Routing 推送状态启用&;上下文路径路由:在服务器上找不到静态资源

Routing 推送状态启用&;上下文路径路由:在服务器上找不到静态资源,routing,cloud-foundry,pushstate,swisscomdev,Routing,Cloud Foundry,Pushstate,Swisscomdev,我使用静态构建包将react应用程序部署到CloudFoundry。目标是使应用程序可以在domain.com/路径下访问。因此,我根据他的博客帖子配置了路线: 另外,我在静态文件中设置了pushstate:enabled,并将上下文路径添加到静态资源URL中;例如,样式表的URL是domain.com/path/static/style.css。 当我访问domain.com/path时,我得到index.html文件。但是,没有找到index.html文件中链接的静态资产,我得到的是索引文件

我使用静态构建包将react应用程序部署到CloudFoundry。目标是使应用程序可以在domain.com/路径下访问。因此,我根据他的博客帖子配置了路线:

另外,我在静态文件中设置了
pushstate:enabled
,并将上下文路径添加到静态资源URL中;例如,样式表的URL是
domain.com/path/static/style.css
。 当我访问domain.com/path时,我得到index.html文件。但是,没有找到index.html文件中链接的静态资产,我得到的是索引文件。如果在服务器上找不到资源,这是pushstate路由的默认行为


为了在“子目录”中运行应用程序,我还需要配置其他任何东西吗?

当您使用
pushstate:enabled
启用推送状态时,buildpack为您的应用程序组装的Nginx配置大致如下所示。我还强调了专门为解决推送状态而添加的部分

server {
    listen 8080;
    server_name localhost;

    root /home/vcap/app/public;

    location / {
        # <-- this bit here is what's added for push state support
        if (!-e $request_filename) {
          rewrite ^(.*)$ / break;
        }
        #  -->

        index index.html index.htm Default.htm;

    }
}
请注意,这要求所有静态文件都位于名为
public
的文件夹下(或任何您想命名为文件根文件夹的文件夹)。使用
location\u include
时必须执行此操作

  • 添加一个自定义includes文件
    nginx/conf/includes/push_state.conf
    ,并在该文件中添加以下内容

    location /path/ {
    
        if (!-e $request_filename) {
          rewrite ^(.*)$ /path/ break;
        }
    
        index index.html index.htm Default.htm;
    
    }
    
  • 推送你的应用程序。它应该使用添加到构建包使用的基本Nginx配置中的自定义代码进行部署和运行


  • 希望有帮助

    我尝试了上述解决方案,但我的静态资源得到了404:bundle.js和css文件。@VishalGulati-开始一个新问题。包括尽可能多的关于你正在做什么的细节。
    location /path/ {
    
        if (!-e $request_filename) {
          rewrite ^(.*)$ /path/ break;
        }
    
        index index.html index.htm Default.htm;
    
    }