A url https://www.example.com/knowledges/qa/123456
wants to proxy to https://www.example.com/knowledges/qa.html?id=123456
is configured in Nginx
as follows:
root /opt/statics;
location / {
index index.html;
}
location /knowledges/qa/ {
rewrite ([^\/]+)\/?$ /knowledges/qa.html?questionId=$1 last;
}
where regular ([^\ /] +)\ /? $
matches to the last part of the url path, id. (see https://regex101.com/r/CBuz0o/1/)
Update
add a slash after / knowledges/qa
/ knowledges/qa/
, otherwise it will rewrite endless loop
can now be accessed normally, but the document.location
obtained in qa.html is still the url, before Nginx forwarding, that is, https://www.example.com/knowledges/qa/123456
. How can I get the url https://www.example.com/knowledges/qa.html?id=123456
forwarded by Nginx?