title: NGINX dynamic location to create dynamic og:image
body: <p>I developed a site that shows some products from a store. The site URL looks like that:</p>
  
  <pre><code>http://testsite.com
  </code></pre>
  
  <p>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. The link of the shared product is:</p>
  
  <pre><code>http://testsite.com/product/1234
  </code></pre>
  
  <p>Where 1234 is the product ID. All the products have images with the ID name. Ex: 1234.jpg. The link for the image of the product ID 1234 is:</p>
  
  <pre><code>http://testsite.com/static/imgs/1234.jpg
  </code></pre>
  
  <p>This site is hosted using a simple NGINX server, that just provides the files.</p>
  
  <p>At the head of my index.html file I have a default og:image for sharing:</p>
  
  <pre><code>&lt;meta property="og:image" content="http://testsite.com/static/imgs/main.jpg"&gt;
  </code></pre>
  
  <p>I wanna the NGINX server to replace this default og:image by the shared ID image. I already know how to do that at NGINX. At the NGINX configuration file (/etc/nginx/conf.d/default.conf) I used the sub_filter option. My NGINX configuration file is:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>This configuration is working for the <strong>location /</strong>, but for the <strong>location ~ /product/(.*)</strong> it is not working.</p>
  
  <p>When I test the sub_fiter option at the <strong>location /</strong> using any other image it replaces correctly. </p>
  
  <p>QUESTIONS:</p>
  
  <p>1) How can I get the product ID (1234) from the URL (<a href="http://testsite.com/product/1234" rel="nofollow noreferrer">http://testsite.com/product/1234</a>)? $1 is not working.</p>
  
  <p>2) I think that when entering at the <strong>location ~ /product/(.*)</strong>, it also redirects for the <strong>location /</strong>. How can I fix this configuration file to works as expected?</p>