特定URL的Nginx规则
我的网站上有这个url组特定URL的Nginx规则,url,nginx,url-rewriting,proxy,rule,Url,Nginx,Url Rewriting,Proxy,Rule,我的网站上有这个url组 mywebsite.com.br/ mywebsite.com.br/sao-paulo/sp mywebsite.com.br/rio-de-janeiro/rj mywebsite.com.br/natal/rn mywebsite.com.br/aparecida-do-rio-doce/go 总共有5561个不同的URL。有没有一种方法可以为所有这些URL发送相同的html文件?但是,还有一些其他URL必须转发到我的nodejs服务器,如下所示:
mywebsite.com.br/
mywebsite.com.br/sao-paulo/sp
mywebsite.com.br/rio-de-janeiro/rj
mywebsite.com.br/natal/rn
mywebsite.com.br/aparecida-do-rio-doce/go
总共有5561个不同的URL。有没有一种方法可以为所有这些URL发送相同的html文件?但是,还有一些其他URL必须转发到我的nodejs服务器,如下所示:
mywebsite.com.br/update-password/1234
mywebsite.com.br/update-password/
mywebsite.com.br/user/confirm
mywebsite.com.br/user/confirm/123
mywebsite.com.br/api/v1/auth/facebook
mywebsite.com.br/api/v1/auth/local
mywebsite.com.br/api/v1/user/new
mywebsite.com.br/api/v1/user/statistics
如何设置Nginx模式,为第一组URL 5561个不同的URL提供相同的html文件,并将第二组URL转发给我的nodejs服务器?这里有一种使用映射的方法:
map $uri $forward2nodejs {
~^/update-password/ 1;
~^/user/confirm 1;
~^/api/v1/auth/ 1;
~^/api/v1/user/ 1;
}
server {
server_name mywebsite.com.br;
# default location for the 5561 different urls
location / {
try_files /default.html =404;
}
if ($forward2nodejs) {
return 301 http://nodejs;
}
}
下面是在前缀位置使用代理传递的另一个示例:
server {
server_name mywebsite.com.br;
# default location for the 5561 different urls
location / {
try_files /default.html =404;
}
location /update-password/ {
include nodejs_proxy_pass;
}
location /user/confirm {
include nodejs_proxy_pass;
}
location /api/v1/auth/ {
include nodejs_proxy_pass;
}
location /api/v1/user/ {
include nodejs_proxy_pass;
}
}
普特南希尔谢谢你的回答,这几乎是我所需要的。我需要向本地运行的nodejs服务器传递一个代理,传递到匹配$forward2nodejs映射模式的url,而不是重定向301。可能吗?因为if语句中不允许使用proxy_pass,所以您需要尝试不同的方法。我建议使用包含节点代理传递指令的文件作为前缀位置。我会把它添加到我的答案中,这样你就可以看到它是什么样子了。