title: Gitlab Omnibus: how to rewrite URLs with bundled nginx
body: <h1>Current configuration</h1>
  
  <p>I migrated my GitLab to a new server. As part of the migration, some repositories were reorganized. We have a website that links directly to <em>raw</em> and <em>blob</em> files on GitLab. I'd like to redirect all HTTPS requests from the old URLs to the new ones, for these <em>raw</em> and <em>blob</em> files.</p>
  
  <p>I use Gitlab Omnibus package, with the bundled nginx install. I based myself on the accepted answer to this question: <a href="https://stackoverflow.com/questions/33601173/gitlab-omnibus-how-to-redirect-all-requests-to-another-domain">Gitlab Omnibus: how to redirect all requests to another domain</a>, which in turn refers to the official GitLab documentation: <a href="https://docs.gitlab.com/omnibus/settings/nginx.html#inserting-custom-settings-into-the-nginx-config" rel="nofollow noreferrer">https://docs.gitlab.com/omnibus/settings/nginx.html#inserting-custom-settings-into-the-nginx-config</a>.</p>
  
  <ol>
  <li><p>Create nginx config directory, because it did not yet exist:</p>
  
  <p><code>sudo mkdir -p /etc/nginx/conf.d/</code></p></li>
  <li><p>Create <code>/etc/nginx/conf.d/redirect.conf</code>:</p></li>
  </ol>
  
  <p>.</p>
  
  <pre><code>server {
    server_name gitlab.itextsupport.com;
    rewrite ^\/itext7\/samples\/(blob|raw)\/master\/(?!samples\/)(.*)$ https://$server_name/itext7/samples/$1/master/samples/$2 permanent;
  }
  </code></pre>
  
  <ol start="3">
  <li><p>Edit the configuration file at <code>/etc/gitlab/gitlab.rb</code> to add the following line:</p>
  
  <p><code>nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/redirect.conf;"</code></p></li>
  <li><p>Rewrite the nginx configuration:</p>
  
  <p><code>sudo gitlab-ctl reconfigure</code></p></li>
  <li><p>Restart the bundled nginx:</p>
  
  <p><code>sudo gitlab-ctl restart nginx</code></p></li>
  <li><p>Verify that the bundled nginx includes the redirect file:</p>
  
  <p><code>sudo grep 'redirect.conf' /var/opt/gitlab/nginx/conf/nginx.conf</code></p></li>
  </ol>
  
  <h1>Testing the configuration</h1>
  
  <pre><code>curl -I https://gitlab.itextsupport.com/itext/tutorial/blob/master/signatures/src/main/java/signatures/chapter4/C4_05_SignWithBEID.java
  </code></pre>
  
  <h1>Expected result</h1>
  
  <p><code>/samples</code> gets inserted after <code>/blob/master</code>. I expect to see a 301 rewrite to <code>https://gitlab.itextsupport.com/itext/tutorial/blob/master/samples/signatures/src/main/java/signatures/chapter4/C4_05_SignWithBEID.java</code></p>
  
  <h1>Actual result</h1>
  
  <p>200 OK on the unmodified URL.</p>
  
  <h1>Ugly hack</h1>
  
  <p>Add this line to <code>/var/opt/gitlab/nginx/conf/gitlab-http.conf</code> and restart the bundled nginx:</p>
  
  <pre><code>rewrite ^\/itext7\/samples\/(blob|raw)\/master\/(?!samples\/)(.*)$ https://$server_name/itext7/samples/$1/master/samples/$2 permanent;
  </code></pre>
  
  <p>By doing it this way, I have verified that my actual rewrite rule itself is correct.</p>
  
  <p>Drawback: this line will be lost every time <code>gitlab-ctl reconfigure</code> is run.</p>
  
  <h1>Question</h1>
  
  <p>What do I need to change to make the URL rewrite work as expected? Without the ugly hack?</p>
  
  <h1>Additional information</h1>
  
  <p>When I run <code>sudo /opt/gitlab/embedded/sbin/nginx -p /var/opt/gitlab/nginx -T</code>, I see two <code>server { }</code> blocks. My working theory so far is that nginx is picking up only the first server block, and ignoring the second one. If I could find a way to merge both server blocks, in a way that is compatible with the configuration file <code>/etc/gitlab/gitlab.rb</code>, then my problem is most likely solved.</p>