diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb
index 02d3161f769e2393c85d87b15bb0ec347160a74c..63f4c8c9e0afa7bc429490f742448880d7e3d383 100644
--- a/config/initializers/1_settings.rb
+++ b/config/initializers/1_settings.rb
@@ -223,7 +223,7 @@ Settings.gitlab['default_can_create_group'] = true if Settings.gitlab['default_c
 Settings.gitlab['host']       ||= ENV['GITLAB_HOST'] || 'localhost'
 Settings.gitlab['ssh_host']   ||= Settings.gitlab.host
 Settings.gitlab['https']        = false if Settings.gitlab['https'].nil?
-Settings.gitlab['port']       ||= Settings.gitlab.https ? 443 : 80
+Settings.gitlab['port']       ||= ENV['GITLAB_PORT'] || (Settings.gitlab.https ? 443 : 80)
 Settings.gitlab['relative_url_root'] ||= ENV['RAILS_RELATIVE_URL_ROOT'] || ''
 Settings.gitlab['protocol'] ||= Settings.gitlab.https ? "https" : "http"
 Settings.gitlab['email_enabled'] ||= true if Settings.gitlab['email_enabled'].nil?
diff --git a/lib/api/helpers/related_resources_helpers.rb b/lib/api/helpers/related_resources_helpers.rb
index 769cc1457fc1c7311a92ad129f35a4f6d1453b3f..1f677529b07482f69d1625dfdac8291d4211c528 100644
--- a/lib/api/helpers/related_resources_helpers.rb
+++ b/lib/api/helpers/related_resources_helpers.rb
@@ -12,7 +12,7 @@ module API
       end
 
       def expose_url(path)
-        url_options = Rails.application.routes.default_url_options
+        url_options = Gitlab::Application.routes.default_url_options
         protocol, host, port = url_options.slice(:protocol, :host, :port).values
 
         URI::HTTP.build(scheme: protocol, host: host, port: port, path: path).to_s
diff --git a/spec/features/dashboard/issues_spec.rb b/spec/features/dashboard/issues_spec.rb
index 7c0bf8de14cc41b2b2fc09a2084c0530251be036..82adde6258f4f1598b83d56091808b2b7ef27fc2 100644
--- a/spec/features/dashboard/issues_spec.rb
+++ b/spec/features/dashboard/issues_spec.rb
@@ -79,15 +79,7 @@ RSpec.describe 'Dashboard Issues' do
       end
     end
 
-    it 'shows the new issue page', js: true do
-      original_defaults = Gitlab::Application.routes.default_url_options
-
-      Gitlab::Application.routes.default_url_options = {
-        host: Capybara.current_session.server.host,
-        port: Capybara.current_session.server.port,
-        protocol: 'http'
-      }
-
+    it 'shows the new issue page', :js do
       find('.new-project-item-select-button').trigger('click')
       wait_for_requests
       find('.select2-results li').click
@@ -97,8 +89,6 @@ RSpec.describe 'Dashboard Issues' do
       page.within('#content-body') do
         expect(page).to have_selector('.issue-form')
       end
-
-      Gitlab::Application.routes.default_url_options = original_defaults
     end
   end
 end
diff --git a/spec/models/project_wiki_spec.rb b/spec/models/project_wiki_spec.rb
index 7fcbeb459e0f4e53a7134dc959c721bcfa8d0c89..c6ceb092810fb9353112ecaf20f0d395c01e3e3c 100644
--- a/spec/models/project_wiki_spec.rb
+++ b/spec/models/project_wiki_spec.rb
@@ -21,7 +21,7 @@ describe ProjectWiki do
 
   describe '#web_url' do
     it 'returns the full web URL to the wiki' do
-      expect(subject.web_url).to match("https?://[^\/]+/#{project.path_with_namespace}/wikis/home")
+      expect(subject.web_url).to eq("#{Gitlab.config.gitlab.url}/#{project.path_with_namespace}/wikis/home")
     end
   end
 
diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb
index e7329210896f851c35dbd7050832a516bf1c887d..8533564392129b53708ad7d304fd7fcc81658e0a 100644
--- a/spec/spec_helper.rb
+++ b/spec/spec_helper.rb
@@ -59,6 +59,7 @@ RSpec.configure do |config|
   config.include Gitlab::Routing, type: :routing
   config.include MigrationsHelpers, :migration
   config.include StubFeatureFlags
+  config.include StubENV
 
   config.infer_spec_type_from_file_location!
 
diff --git a/spec/support/capybara.rb b/spec/support/capybara.rb
index 3e5d6cf1364d0a85628732de4eebd893f33b63aa..c45c4a4310d98fbc700f8b120f7fbd9dd9edd182 100644
--- a/spec/support/capybara.rb
+++ b/spec/support/capybara.rb
@@ -36,7 +36,14 @@ RSpec.configure do |config|
     $capybara_server_already_started = true
   end
 
-  config.after(:each, :js) do |example|
+  config.before(:example, :js) do
+    allow(Gitlab::Application.routes).to receive(:default_url_options).and_return(
+      host: Capybara.current_session.server.host,
+      port: Capybara.current_session.server.port,
+      protocol: 'http')
+  end
+
+  config.after(:example, :js) do |example|
     # capybara/rspec already calls Capybara.reset_sessions! in an `after` hook,
     # but `block_and_wait_for_requests_complete` is called before it so by
     # calling it explicitely here, we prevent any new requests from being fired