我对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;
}
}
答案 0 :(得分:2)
^/$
与URI /12345678
不匹配-它仅与URI /
匹配。
您可以使用:
rewrite ^ /index.html break;
^
只是匹配任何内容的许多正则表达式之一。后缀break
使重写的URI在同一location
块内进行处理。有关详细信息,请参见this document。
您可以使用try_files
指令获得相同的结果:
location ~ "^/[\d]{8}" {
root /usr/share/nginx/html;
try_files /index.html =404;
}
由于=404
始终存在,因此从未到达index.html
子句-但是try_files
必须至少具有两个参数。有关详细信息,请参见this document。
答案 1 :(得分:1)
请尝试这个
server {
listen 8008;
server_name default_server;
root /usr/share/nginx/html;
location / {
proxy_pass http://someotherplace/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location ~ "^/[\d]{8}" {
rewrite ^(.*)$ /index.html break;
}
}
proxy_pass
会将请求路由到您的远程目标并返回响应。
可以使用rewrite
代替try_files
,如Richard Smith所述。