我是nginx的新手,只是尝试做一些我认为应该很简单的事情.如果我做:-

卷曲http://localhost:8008/12345678

我希望会返回index.html页面.但相反,我得到404未找到. /usr/share / nginx / html / 12345678没有这样的文件

如果我卷曲http://localhost:8008/,我希望将请求路由到http://someotherplace/,但我却找到302,就这样.

对于基本问题深表歉意,但不胜感激!

这是代码:

server {
    listen 8008;
    server_name default_server;

    location / {
       rewrite ^/$http://someotherplace/ redirect;
    }

    location ~ "^/[\d]{8}" {
       rewrite ^/$/index.html;
       root /usr/share/nginx/html;
    }
}

最佳答案
^ / $与URI / 12345678不匹配-它仅与URI /相匹配.

您可以使用:

rewrite ^ /index.html break;

^只是匹配任何内容的许多正则表达式之一.分隔符后缀使重写的URI在同一位置块内进行处理.有关详细信息,请参见this document.

您可以使用try_files指令获得相同的结果:

location ~ "^/[\d]{8}" {
    root /usr/share/nginx/html;
    try_files /index.html =404;
}

永远不会到达= 404子句,因为index.html始终存在-但是try_files必须至少具有两个参数.有关详细信息,请参见this document.

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。