【发布时间】:2016-11-25 20:32:23
【问题描述】:
例如,我想从https://www.example.com/${pid}.txt 重定向到http://www.anotherExample.com/api?pid=${pid} 如何在nginx中配置它? 在此先感谢:)
【问题讨论】:
标签: redirect nginx url-rewriting
例如,我想从https://www.example.com/${pid}.txt 重定向到http://www.anotherExample.com/api?pid=${pid} 如何在nginx中配置它? 在此先感谢:)
【问题讨论】:
标签: redirect nginx url-rewriting
试试这个:
server {
listen 443;
server_name www.example.com;
location ~* ^/(.+)\.txt$ {
return 301 http://www.anotherexample.com/api?pid=$1;
}
}
如果您的 pid 具有特定格式(例如只有数字),则将 (.+) 替换为适当的模式。
如果您想在不重定向的情况下显示内容,请将return 301 替换为proxy_pass。
【讨论】:
http:// 和listen 80;。
\/ 而不是/。这取决于 nginx 版本
最好的方法是使用return 301,它将请求重定向到您设置的站点,也许最好使用return 301 $scheme://www.anotherexample.com/api?pid=$1;,这样当您使用时,您的URL将被重定向为HTTPS购买并安装 SSL
【讨论】: