Gitlab and Apache – Uploaded images 404 errors

apache-2.4gitlab

I've installed Gitlab on my LAMP server and it's worked well until I decided to add a project avatar today.

The images upload as expected to /var/opt/gitlab/gitlab-rails/uploads/projects/1/projectlogo.png but when I reloaded the page the image didn't load.

Browsing directly to the image url http://gitlab.myserver.com:8080/uploads/project/avatar/2/projectlogo.png gives me a 404 error.

I've been searching all over and trying various things between adding a ProxyPass for /uploads to the Gitlab apache conf file. Changing the ownership of the uploads directory. I even attempted to modify production.rb so that Gitlab would be responsible for serving static files config.serve_static_files = true

Here is the apache config file I'm using at the moment.

# This configuration has been tested on GitLab 8.2
# Note this config assumes unicorn is listening on default port 8080 and
# gitlab-workhorse is listening on port 8181. To allow gitlab-workhorse to
# listen on port 8181, edit or create /etc/default/gitlab and change or add the following:
#
# gitlab_workhorse_options="-listenUmask 0 -listenNetwork tcp -listenAddr 127.0.0.1:8181 -authBackend http://127.0.0.1:8080"
#
#Module dependencies
# mod_rewrite
# mod_proxy
# mod_proxy_http
<VirtualHost *:80>
  ServerName gitlab.myserver.com
  ServerSignature Off

  ProxyPreserveHost On

  # Ensure that encoded slashes are not decoded but left in their encoded state.
  # http://doc.gitlab.com/ce/api/projects.html#get-single-project
  AllowEncodedSlashes NoDecode

  <Location />
    # New authorization commands for apache 2.4 and up
    # http://httpd.apache.org/docs/2.4/upgrading.html#access
    Require all granted

    #Allow forwarding to gitlab-workhorse
    ProxyPassReverse http://127.0.0.1:8181
    #Allow forwarding to GitLab Rails app (Unicorn)
    ProxyPassReverse http://127.0.0.1:8080
    ProxyPassReverse http://gitlab.myserver.com/
  </Location>

  # Apache equivalent of nginx try files
  # http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
  # http://stackoverflow.com/questions/10954516/apache2-proxypass-for-rails-app-gitlab
  RewriteEngine on

  #Forward these requests to gitlab-workhorse
  RewriteCond %{REQUEST_URI} ^/[\w\.-]+/[\w\.-]+/gitlab-lfs/objects.* [OR]
  RewriteCond %{REQUEST_URI} ^/[\w\.-]+/[\w\.-]+/builds/download.* [OR]
  RewriteCond %{REQUEST_URI} ^/[\w\.-]+/[\w\.-]+/repository/archive.* [OR]
  RewriteCond %{REQUEST_URI} ^/api/v3/projects/.*/repository/archive.* [OR]
  RewriteCond %{REQUEST_URI} ^/ci/api/v1/builds/[0-9]+/artifacts.* [OR]
  RewriteCond %{REQUEST_URI} ^/[\w\.-]+/[\w\.-]+/(info/refs|git-upload-pack|git-receive-pack)$
  RewriteRule .* http://127.0.0.1:8181%{REQUEST_URI} [P,QSA,NE]

  #Forward any other requests to GitLab Rails app (Unicorn)
  RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f [OR]
  RewriteCond %{REQUEST_URI} ^/uploads
  RewriteRule .* http://127.0.0.1:8080%{REQUEST_URI} [P,QSA,NE]

  # needed for downloading attachments
  DocumentRoot /opt/gitlab/embedded/service/gitlab-rails/public

  #Set up apache error documents, if back end goes down (i.e. 503 error) then a maintenance/deploy page is thrown up.
  ErrorDocument 404 /404.html
  ErrorDocument 422 /422.html
  ErrorDocument 500 /500.html
  ErrorDocument 503 /deploy.html

  # It is assumed that the log directory is in /var/log/httpd.
  # For Debian distributions you might want to change this to
  # /var/log/apache2.
  LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
  ErrorLog /var/log/apache2/gitlab.pyramidmines.com_error.log
  CustomLog /var/log/apache2/gitlab.pyramidmines.com_forwarded.log common_forwarded
  CustomLog /var/log/apache2/gitlab.pyramidmines.com_access.log combined env=!dontlog
  CustomLog /var/log/apache2/gitlab.pyramidmines.com.log combined

  ProxyPass /uploads !
  <Directory /var/opt/gitlab/gitlab-rails/uploads>
    Order allow,deny
    Allow from all
  </Directory>

</VirtualHost>

Which is mostly from the recipe for apache2.4, with changes to the log directory, document root and the proxy pass for uploads.

Best Answer

After bashing my head against the keyboard for a few days I noticed the port number attached to the external_url entry in /etc/gitlab/gitlab.rb

external_url 'http://gitlab.myserver.com:8080'

Removing the port number solved my image 404 issues.

external_url 'http://gitlab.myserver.com'

Related Topic