【问题标题】:Nginx - try_files: How to set up a remote file (url)?Nginx - try_files:如何设置远程文件(url)?
【发布时间】:2020-02-25 08:11:13
【问题描述】:

我有一个使用浏览器历史记录的 React 单页应用程序,Basename 是“邀请”,这是我当前使用本机 html 文件的 nginx 配置:

server{
    listen 9999;
    server_name 0.0.0.0;
    access_log  /var/log/nginx/activity_access.log;
    error_log /var/log/nginx/activity_error.log;
    location /invite {
        root html;
        index index.html;
        try_files $uri /invite/index.html;
    }
}

上面的配置效果很好。

但我想将我的本地 html 文件设置为 cdn 上的 html 文件。我该怎么办?这是我的伪代码:

server{
    listen 9999;
    server_name 0.0.0.0;
    access_log  /var/log/nginx/activity_access.log;
    error_log /var/log/nginx/activity_error.log;
    location /invite {
        root html;
        index index.html;
        try_files $uri https://mycdn.com/invite/index.html;
    }
}

谢谢!

【问题讨论】:

    标签: reactjs nginx react-router nginx-config


    【解决方案1】:

    try_files 语句中的最后一个参数只能为 内部重定向 指定一个 URI。请改用命名位置。

    例如:

    location /invite {
        ...
        try_files $uri @redirect;
    }
    location @redirect {
        return 301 https://mycdn.com/invite/index.html;
    }
    

    详情请见this document。


    要使用反向代理而不是重定向,请使用:

    location @redirect {
        rewrite ^ /invite/index.html break;
        proxy_pass https://mycdn.com;
    }
    

    【讨论】:

    • 不想重定向,可以用其他方法代理cdn上的html文件吗?
    猜你喜欢
    • 1970-01-01
    • 2018-03-01
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-08
    相关资源
    最近更新 更多