title: How to remove a path segment from NGiNX fastcgi_script_name?
body: <p>This question could also be: <strong>"How to modify an NGiNX variable with a RegEx?"</strong>,
  or: <strong>"Can RegEx backreference have gaps?"</strong></p>
  
  <p>Either of those would resolve the problem I'm having. Perhaps a really simple solution exists, and after digging the web for few hours, it's time to ask for help.</p>
  
  <p>Here's the scenario:</p>
  
  <p>There's a part of the request URI that is going to be present always (for a sort of a gimmicky domain name + URI combination :-).  I enforce the presence of that always-present URI path component, which follows immediately after the domain name, like so:</p>
  
  <p>http://somedomain.com/basepart/rest/of/the/path?q=123</p>
  
  <p>In the above example the "/basepart" represents the always-present URI component.</p>
  
  <p>So far so good. The problem arises when I want the base file path to be <code>/var/www/somedomain.com/htdocs/</code> without the <code>basepart</code>, and php5_fpm proxy is used. I obviously set:</p>
  
  <pre><code>    location /basepart {
              alias /var/www/somedomain.com/htdocs;
              try_files $uri $uri/ /index.php?$args;
      }
  </code></pre>
  
  <p>But since the dynamic files are in PHP, I need to either use <code>fastcgi_split_path_info</code> or <code>$request_uri</code> to build/pass the <code>SCRIPT_FILENAME</code> to php5_fpm. How do I do that? How do I remove the <code>/basepart</code> from <code>$fastcgi_script_name</code>, or from <code>$request_uri</code>, as otherwise PHP will look for the file in <code>/var/www/somedomain.com/htdocs/basepart</code>?</p>
  
  <p>I've considered named backreferences, or "collecting" or "fragmented" backreferences (which I don't think exist in regex) so that I could capture the segment in <code>$fastcgi_script_name</code> before and after the basepart when <code>fastcgi_split_path_info</code> assignment happens, but haven't got them to work. <em>Dayo</em> <a href="https://stackoverflow.com/a/12057752/134536">writes earlier at SO</a>: »Nginx is a webserver and not a scripting application.», and suggests use of Lua for more complex scripting. But I have a feeling I may be overlooking some really simple, facepalm-worthy solution :-].</p>
  
  <p>Any thoughts, anyone?</p>