Reactjs s3上托管的react单页应用程序(SPA)的nginx配置不工作

Reactjs s3上托管的react单页应用程序(SPA)的nginx配置不工作,reactjs,nginx,amazon-s3,single-page-application,Reactjs,Nginx,Amazon S3,Single Page Application,我正在尝试让托管在s3文件夹上的单页应用程序运行nginx。对于主推送,我们使用常规的cloudfront、s3和route 53。但对于分支部署,我们不希望每个开发人员都使用cloudfront。因此,我们要做的是将资产推送到s3 bucket my.example.com/branch-name/,然后创建一条路由53 a记录-branch-name.my.example.com,它指向nginx服务器。 这是我的nginx配置: server { listen 8443

我正在尝试让托管在s3文件夹上的单页应用程序运行nginx。对于主推送,我们使用常规的cloudfront、s3和route 53。但对于分支部署,我们不希望每个开发人员都使用cloudfront。因此,我们要做的是将资产推送到s3 bucket my.example.com/branch-name/,然后创建一条路由53 a记录-branch-name.my.example.com,它指向nginx服务器。 这是我的nginx配置:

server {
    listen       8443;
    server_name  *.my.example.com;
    index   index.html;

    location / {
        resolver 8.8.8.8;

        set $bucket "my.example.com.s3-website-us-west-1.amazonaws.com";
        rewrite ^([^.]*[^/])$ $1/ permanent;

        # matches: branch-name
        if ($host ~ ^([^.]*)\.my\.example\.com) {
            set $branch $1;
            proxy_pass http://$bucket/${branch}$uri;
        }
        try_files '' /index.html =404;
        proxy_intercept_errors on;
        proxy_redirect off;
        proxy_set_header Host $bucket;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_hide_header x-amz-id-2;
        proxy_hide_header x-amz-request-id;
    }

}
我正在寻找的行为是,如果我转到它,应该加载http://$bucket/${branch}/index.html。 如果我去。。。它应该在浏览器中保留url,但仍然加载http://$bucket/${branch}/index.html 如果我去,它应该被路由到我的应用程序中的正确选项卡,因为react router有一个用于/正确路径的组件。 如果我去它应该仍然路由到正确的页面,但保留url

现在,我得到的行为是,我只能去根url,也就是说,我被路由到正确的index.html,然后我点击该页面上的“正确路径”选项卡,url将相应地改变,并且很好。 但是如果我尝试直接转到,我只会从s3中得到以下错误:

404 Not Found
Code: NoSuchKey
Message: The specified key does not exist.
Key: branch-name/correct-path/index.html
RequestId: 92ADA29566570E8C9
HostId: wT4JdQrkB7KI0+Gnb4+88WNdnX0NXCHB6/KP5RxqJMozAx8z3RlC4T6uefk2LA=
An Error Occurred While Attempting to Retrieve a Custom Error Document
Code: NoSuchKey
Message: The specified key does not exist.
Key: index.html
如果我转到(注意没有结束斜杠),它会无限期地加载

请帮忙。谢谢。

试试这个配置

server {
    listen       80;

    location = / {
        proxy_pass http://bucketname-ap-southeast-2.amazonaws.com;
        proxy_intercept_errors on;
        error_page 404 =200 @spa-root;
    }

    location / {
        proxy_pass http://bucketname-ap-southeast-2.amazonaws.com;
        proxy_intercept_errors on;
        error_page 404 =200 @spa-root;
    }

    location @spa-root {
        proxy_pass http://bucketname-ap-southeast-2.amazonaws.com;
    }
}