简体   繁体   English

NGINX 动态定位创建动态og:image

[英]NGINX dynamic location to create dynamic og:image

I developed a site that shows some products from a store.我开发了一个网站,展示商店的一些产品。 The site URL looks like that:站点 URL 看起来像这样:

http://testsite.com

The site has the functionality of sharing the product (it's already working) generating a link that can be shared at facebook or WhatsApp or anywhere.该站点具有共享产品的功能(它已经在工作)生成可以在 facebook 或 WhatsApp 或任何地方共享的链接。 The link of the shared product is:共享产品的链接是:

http://testsite.com/product/1234

Where 1234 is the product ID.其中 1234 是产品 ID。 All the products have images with the ID name.所有产品都有带有 ID 名称的图像。 Ex: 1234.jpg.例如:1234.jpg。 The link for the image of the product ID 1234 is:产品 ID 1234 的图片链接为:

http://testsite.com/static/imgs/1234.jpg

This site is hosted using a simple NGINX server, that just provides the files.该站点使用简单的 NGINX 服务器托管,该服务器仅提供文件。

At the head of my index.html file I have a default og:image for sharing:在我的 index.html 文件的开头,我有一个默认的 og:image 用于共享:

<meta property="og:image" content="http://testsite.com/static/imgs/main.jpg">

I wanna the NGINX server to replace this default og:image by the shared ID image.我想要 NGINX 服务器用共享 ID 图像替换这个默认的 og:image。 I already know how to do that at NGINX.我已经知道如何在 NGINX 做到这一点。 At the NGINX configuration file (/etc/nginx/conf.d/default.conf) I used the sub_filter option.在 NGINX 配置文件 (/etc/nginx/conf.d/default.conf) 我使用了 sub_filter 选项。 My NGINX configuration file is:我的 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;
    }
}

This configuration is working for the location / , but for the location ~ /product/(.*) it is not working.此配置适用于位置 / ,但对于位置 ~ /product/(.*)它不起作用。

When I test the sub_fiter option at the location / using any other image it replaces correctly.当我在位置 /使用任何其他图像测试 sub_fiter 选项时,它会正确替换。

QUESTIONS:问题:

1) How can I get the product ID (1234) from the URL ( http://testsite.com/product/1234 )? 1) 如何从 URL ( http://testsite.com/product/1234 ) 获取产品 ID (1234)? $1 is not working. 1 美元不起作用。

2) I think that when entering at the location ~ /product/(.*) , it also redirects for the location / . 2)我认为在位置 ~ /product/(.*)进入时,它也会重定向到位置 / How can I fix this configuration file to works as expected?如何修复此配置文件以按预期工作?

I think your alias statement is the problem.我认为您的alias声明是问题所在。 Reading in nginx docs:阅读 nginx 文档:

location /i/ { alias /data/w3/images/;位置 /i/ { 别名 /data/w3/images/; } }

on request of “/i/top.gif”, the file /data/w3/images/top.gif will be sent.根据“/i/top.gif”的请求,将发送文件/data/w3/images/top.gif。

It means that in your case on each ~/product/(.*) request /var/www/testsite.com/dist/index.html will be sent without taking product ID into account.这意味着在您的情况下,每个~/product/(.*)请求/var/www/testsite.com/dist/index.html将在不考虑产品 ID 的情况下发送。 You might want to configure alias on / to avoid that.您可能希望在/上配置别名以避免这种情况。 This is also likely to be the reason you get "redirected" to / .这也可能是您被“重定向”到/的原因。

As for $1, it should work as you have it now.至于 1 美元,它应该像你现在拥有的那样工作。 When you fix the alias , I think it will work then.当您修复alias时,我认为它会起作用。 If not, you can try the named match: (?<product>[0-9]+) instead of (.*) , then you can use the $product variable to reference the id.如果没有,您可以尝试命名匹配: (?<product>[0-9]+)而不是(.*) ,然后您可以使用$product变量来引用 id。

There's another small glitch in your code — you're adding extra quote marks on replace.您的代码中还有另一个小故障 - 您在替换时添加了额外的引号。 The second argument to sub_filter is in quotes twice. sub_filter 的第二个参数在引号中两次。


Working Example工作示例

UPDATE: Ok, I got it working on localhost with the following nginx config (testing on "Hello World"):更新:好的,我在 localhost 上使用以下 nginx 配置(在“Hello World”上测试):

        location ~ /product/(\d+)$ {
                set $product $1;
                try_files $uri $uri/ /index.html;
        }

        location / {
                if ( $product = '' ) {
                   set $search 'Non-Existent-String';
                }
                if ( $product != '' ) {
                   set $search 'World'; # the string you want to replace
                }

                index index.html index.htm;
                sub_filter '$search' 'Product #$product';
        }

The key here is that when you use try_files, it does get to location / .这里的关键是,当您使用 try_files 时,它确实会到达location / So we need to sub_filter in / .所以我们需要在/中进行 sub_filter 。 We also don't want to sub_filter regular /index.html requests.我们也不想 sub_filter 常规 /index.html 请求。 Something like if ($product) sub_filter would be nice, but is impossible with nginx.if ($product) sub_filter这样的东西会很好,但对于 nginx 是不可能的。 So I just leave sub_filter but only set real search string for product requests.所以我只留下 sub_filter 但只为产品请求设置真正的搜索字符串。

例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM