diff --git a/app/mailers/notify.rb b/app/mailers/notify.rb
index 5cd9b82900c1b10ea8348cf8c770a7c3cdcf8f32..46b8e05450929a25270f5785fc2e9011331f7b42 100644
--- a/app/mailers/notify.rb
+++ b/app/mailers/notify.rb
@@ -6,6 +6,7 @@ class Notify < ActionMailer::Base
   default_url_options[:host]     = Gitlab.config.gitlab.host
   default_url_options[:protocol] = Gitlab.config.gitlab.protocol
   default_url_options[:port]     = Gitlab.config.gitlab.port if Gitlab.config.gitlab_on_non_standard_port?
+  default_url_options[:script_name] = Gitlab.config.gitlab.relative_url_root
 
   default from: Gitlab.config.gitlab.email_from
 
diff --git a/config.ru b/config.ru
index 5ef2a0289fee14259ff60c5a460fc97690443efd..dfd2d862237323aa599be31b473d70a8a817943b 100644
--- a/config.ru
+++ b/config.ru
@@ -1,4 +1,7 @@
 # This file is used by Rack-based servers to start the application.
 
 require ::File.expand_path('../config/environment',  __FILE__)
-run Gitlab::Application
+
+map ENV['RAILS_RELATIVE_URL_ROOT'] || "/" do
+  run Gitlab::Application
+end
diff --git a/config/gitlab.yml.example b/config/gitlab.yml.example
index f47625eb132ab304a71e35bf2ccf998fbe413c1f..5546632cb7f8c93bdf89e5e3d55187be52b90272 100644
--- a/config/gitlab.yml.example
+++ b/config/gitlab.yml.example
@@ -18,6 +18,9 @@ gitlab:
   host: localhost
   port: 80
   https: false
+  # uncomment and customize to run in non-root path
+  # note that ENV['RAILS_RELATIVE_URL_ROOT'] in config/unicorn.rb may need to be changed
+  # relative_url_root: /gitlab
 
   ## Email settings
   # Email address used in the "From" field in mails sent by GitLab
diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb
index efc816118355a910d23cc6fe0d2801ae182ec96e..6b47a9cc948a4690f369786249188b8f44fc6e5a 100644
--- a/config/initializers/1_settings.rb
+++ b/config/initializers/1_settings.rb
@@ -25,7 +25,8 @@ def build_gitlab_url
       [ gitlab.protocol,
         "://",
         gitlab.host,
-        custom_port
+        custom_port,
+        gitlab.relative_url_root
       ].join('')
     end
   end
@@ -45,6 +46,7 @@ def build_gitlab_url
 Settings.gitlab['host']       ||= 'localhost'
 Settings.gitlab['https']      ||= false
 Settings.gitlab['port']       ||= Settings.gitlab.https ? 443 : 80
+Settings.gitlab['relative_url_root'] ||= ''
 Settings.gitlab['protocol']   ||= Settings.gitlab.https ? "https" : "http"
 Settings.gitlab['email_from'] ||= "gitlab@#{Settings.gitlab.host}"
 Settings.gitlab['url']        ||= Settings.send(:build_gitlab_url)
diff --git a/config/routes.rb b/config/routes.rb
index 4a02bd9f80981e8b7015238ec69584342259bbe9..1259496f6344b742bf17762fd7b3bd8f1475ca5c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -18,7 +18,7 @@
     project_root: Gitlab.config.gitolite.repos_path,
     upload_pack:  Gitlab.config.gitolite.upload_pack,
     receive_pack: Gitlab.config.gitolite.receive_pack
-  }), at: '/:path', constraints: { path: /[-\/\w\.-]+\.git/ }
+  }), at: '/', constraints: lambda { |request| /[-\/\w\.-]+\.git/.match(request.path_info) }
 
   #
   # Help
diff --git a/config/unicorn.rb.example b/config/unicorn.rb.example
index 425dbf33f8696ce36b75096b61be48fc48998901..4852cd65daaddad1b2a8021e994656651bda3a2b 100644
--- a/config/unicorn.rb.example
+++ b/config/unicorn.rb.example
@@ -1,3 +1,7 @@
+# uncomment and customize to run in non-root path
+# note that config/gitlab.yml web path should also be changed
+# ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
+
 app_dir = "/home/gitlab/gitlab/"
 worker_processes 2
 working_directory app_dir
diff --git a/lib/gitlab/backend/grack_auth.rb b/lib/gitlab/backend/grack_auth.rb
index 7c31117f01d9e83f3cc47db5612c48294d1af33b..cfad532a06ca478c6042d86700a1f9993cf5d3db 100644
--- a/lib/gitlab/backend/grack_auth.rb
+++ b/lib/gitlab/backend/grack_auth.rb
@@ -17,10 +17,6 @@ def valid?
       # Pass Gitolite update hook
       ENV['GL_BYPASS_UPDATE_HOOK'] = "true"
 
-      # Need this patch due to the rails mount
-      @env['PATH_INFO'] = @request.path
-      @env['SCRIPT_NAME'] = ""
-
       # Find project by PATH_INFO from env
       if m = /^\/([\w\.\/-]+)\.git/.match(@request.path_info).to_a
         self.project = Project.find_with_namespace(m.last)