我开发了一个网站,展示了商店中的一些产品。网站网址如下所示:
http://testsite.com
该站点具有共享产品功能(已在运行)的功能,该链接可在Facebook或WhatsApp或任何地方共享。共享产品的链接是:
http://testsite.com/product/1234
其中的1234是产品ID。所有产品都有带有ID名称的图像。例如:1234.jpg。产品ID 1234的图像的链接为:
http://testsite.com/static/imgs/1234.jpg
此站点使用一个简单的NGINX服务器托管,该服务器仅提供文件。
在index.html文件的开头,我有一个默认的og:image用于共享:
<meta property="og:image" content="http://testsite.com/static/imgs/main.jpg">
我想NGINX服务器用共享ID图像替换默认的og:image。我已经在NGINX知道如何做到这一点。在NGINX配置文件(/etc/nginx/conf.d/default.conf)中,我使用了sub_filter选项。我的NGINX配置文件是:
server {
listen 80;
server_name *.testsite.com;
root /var/www/testsite.com/dist;
location / {
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location ~ /product/(.*) {
index index.html index.htm;
try_files $uri $uri/ /index.html;
sub_filter 'http://testsite.com/static/imgs/main.jpg'
'http://testsite.com/static/imgs/$1.jpg';
sub_filter_once on;
}
}
此配置适用于位置/ ,但不适用于位置〜/ product /(.*)。
当我使用其他任何图像在位置/ 中测试sub_fiter选项时,它将正确替换。
问题:
1)如何从URL(http://testsite.com/product/1234)获得产品ID(1234)? $ 1不起作用。
2)我认为,在位置〜/ product /(.*)输入时,它还会重定向到位置/ 。如何修复此配置文件以使其按预期工作?