【问题标题】:How to configure nginx to serve gitlabhq on a SubURI如何配置 nginx 在 SubURI 上服务 gitlabhq
【发布时间】:2012-11-05 12:53:11
【问题描述】:

gitlab的nginx配置为:

# GITLAB
# Maintainer: @randx
# App Version: 3.0

upstream gitlab {
  server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
}

server {
  listen YOUR_SERVER_IP:80;         # e.g., listen 192.168.1.1:80;
  server_name YOUR_SERVER_FQDN;     # e.g., server_name source.example.com;
  root /home/gitlab/gitlab/public;

  # individual nginx logs for this gitlab vhost
  access_log  /var/log/nginx/gitlab_access.log;
  error_log   /var/log/nginx/gitlab_error.log;

  location / {
    # serve static files from defined root folder;.
    # @gitlab is a named location for the upstream fallback, see below
    try_files $uri $uri/index.html $uri.html @gitlab;
  }

  # if a file, which is not found in the root folder is requested,
  # then the proxy pass the request to the upsteam (gitlab unicorn)
  location @gitlab {
    proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
    proxy_redirect     off;

    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host              $http_host;
    proxy_set_header   X-Real-IP         $remote_addr;

    proxy_pass http://gitlab;
  }
}

我应该改变什么以将 gitlab 用作 surURI,www.mysuperserver.com/gitlab

我尝试了很多不同的方法,但没有任何效果 谢谢

【问题讨论】:

    标签: ruby-on-rails nginx gitlab


    【解决方案1】:

    我已经成功地让它在一个子目录 url 下工作。

    • 遵循源代码中的指令,如/home/git/gitlab/config/gitlab.yml
    # 取消注释并自定义最后一行以在非根路径下运行 # 警告:我们建议创建一个 FQDN 以在根路径中托管 GitLab 而不是这个。 # 请注意,需要更改四个设置才能使其正常工作。 # 1) 在你的 application.rb 文件中:config.relative_url_root = "/gitlab" # 2) 在你的 gitlab.yml 文件中:relative_url_root: /gitlab # 3) 在你的 unicorn.rb 中:ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab" # 4) 在 ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab" # 要更新路径,运行: sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production # relative_url_root: /gitlab
    • 更改 nginx 配置以提供 suburi,请参考下面的示例:

    关键是上下文server下的rootlocation下的alias。请参考nginx pitfallsnginx root note 了解更多详情。

    # default.conf for nginx
    upstream gitlab {
      server unix:/home/git/gitlab/tmp/sockets/gitlab.socket;
    }
    server {
        listen       80;
        server_name  $YOUR_DOMAIN;
        # other settings, especially root settings, like below
        # root /usr/local/nginx/html;
        location /gitlab {
            # serve static files from defined root folder;
            alias /home/git/gitlab/public;
    
            # individual nginx logs for this gitlab vhost
            access_log  /var/log/nginx/gitlab_access.log;
            error_log   /var/log/nginx/gitlab_error.log;
    
            # @gitlab is a named location for the upstream fallback, see below
            try_files $uri $uri/index.html $uri.html @gitlab;
        }
    
        location @gitlab {
            proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
            proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
            proxy_redirect     off;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   Host              $http_host;
            proxy_set_header   X-Real-IP         $remote_addr;
            proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
    
            proxy_pass http://gitlab;
        }
        # other locations' settings...
    }
    

    【讨论】:

    • +1。你用的是什么版本的 GitLab?您是否使用推/拉 ssh url 对其进行了测试? https 网址?
    • 分支6-4-stable。没有人抱怨推/拉。尚未设置 https url。
    【解决方案2】:

    从 Gitlab 5.3 开始,您可以使用官方安装文档将其配置为在开箱即用的 suburi 中运行。

    取消注释 config/puma.rb 的第 8 行: ENV['RAILS_RELATIVE_URL_ROOT'] = "/"

    config/gitlab.yml 中的第 23 行类似: relative_url_root:/

    我根本不需要修改我的 nginx 配置来让它工作。

    【讨论】:

      【解决方案3】:

      对于旧版本,例如 gitlab 7.4.5 ,没有 gitlab-git-http-server (gitlab-workhorse)。但是 gitlab 7.4.5 在文档中使用 suburi 有一些解决方案。

      config/application.rb

      # Relative url support
      # Uncomment and customize the last line to run in a non-root path
      # WARNING: We recommend creating a FQDN to host GitLab in a root path instead of this.
      # Note that following settings need to be changed for this to work.
      # 1) In your application.rb file: config.relative_url_root = "/gitlab"
      # 2) In your gitlab.yml file: relative_url_root: /gitlab
      # 3) In your unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
      # 4) In ../gitlab-shell/config.yml: gitlab_url: "http://127.0.0.1/gitlab"
      # 5) In lib/support/nginx/gitlab : do not use asset gzipping, remove block starting with "location ~ ^/(assets)/"
      #
      # To update the path, run: sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=production
      #
      # config.relative_url_root = "/gitlab"
      

      config/gitlab.yml

      # WARNING: See config/application.rb under "Relative url support" for the list of
      # other files that need to be changed for relative url support
      # relative_url_root: /gitlab
      

      在 nginx 配置或 apache 配置文件中将 gitlab 7.4.5 与使用相同端口和相同域名的其他网站配置似乎仍然是一个挑战。我没有得到这个。我可以以 www.mydomain.com/gitlab 的身份访问 gitlab,但我没有在 www.mydomain.com 的另一个根目录中获得我的另一个网站。它应该通过配置 nginx 或 apache 来解决。希望熟悉nginx或者apache的朋友给个解决办法。

      其他参考。Support installing GitLab in a relative URL path or sub directory #1950

      编辑
      现在可以使用了。

      stackoverflow 建议在这里使用 www.example.com 作为示例。

      www.example.com/gitlab 访问 gitlab。

      www.example.com 访问另一个网站,比如我的博客。

      步骤:

      1. config/application.rb 文件:config.relative_url_root = "/gitlab"
      2. config/gitlab.yml 文件:relative_url_root:/gitlab
      3. config/unicorn.rb: ENV['RAILS_RELATIVE_URL_ROOT'] = "/gitlab"
      4. ../gitlab-shell/config.yml:gitlab_url:"http://www.example.com/gitlab"
      5. 将 lib/support/nginx/gitlab 复制到 gitlab.conf 用于 nginx 并且不要使用资产 gzipping,删除以 "location ~^/(assets)/" 开头的块
      6. 运行:sudo -u git -H bundle exec rake assets:precompile RAILS_ENV=生产

      网址:

      1. config/gitlab.yml 文件:主机:example.com 端口:80
      2. config/unicorn.rb:听“127.0.0.1:9095”
      3. ../gitlab-shell/config.yml: gitlab_url: http://www.example.com/gitlab

      gitlab 会给 git 两种访问方式:

      1. git@example.com:sample-project.git
      2. http://example.com/gitlab/sample-project.git

      我没有使用 https。

      如果你得到:

      您可以将config/gitlab.yml 配置为host: example.com/gitlab。只需删除/gitlab

      nginx配置文件:

      ###################################
      ##         configuration         ##
      ###################################
      ##
      
      upstream gitlab {
        server unix:/home/git/gitlab/tmp/sockets/gitlab.socket fail_timeout=0;
      }
      
      ## Normal HTTP host
      server {
        #listen *:80 default_server;
        listen *:80 default_server;
        server_name www.example.com; ## Replace this with something like gitlab.example.com
        server_tokens off; ## Don't show the nginx version number, a security best practice
        #root /home/git/gitlab/public;
        root html;
        location /{
          #root html;
          index index.html index.htm;
        }
        ## Increase this if you want to upload large attachments
        ## Or if you want to accept large git objects over http
        client_max_body_size 20m;
      
        ## Individual nginx logs for this GitLab vhost
        access_log  logs/example.gitlab_access.log;
        error_log   logs/example.gitlab_error.log;
      
        location /gitlab {
          alias /home/git/gitlab/public;
      
          ## Serve static files from defined root folder.
          ## @gitlab is a named location for the upstream fallback, see below.
          try_files $uri $uri/index.html $uri.html @gitlab;
        }
      
        ## If a file, which is not found in the root folder is requested,
        ## then the proxy passes the request to the upsteam (gitlab unicorn).
        location @gitlab {
          ## If you use HTTPS make sure you disable gzip compression
          ## to be safe against BREACH attack.
          # gzip off;
      
          ## https://github.com/gitlabhq/gitlabhq/issues/694
          ## Some requests take more than 30 seconds.
          proxy_read_timeout      300;
          proxy_connect_timeout   300;
          proxy_redirect          off;
      
          proxy_set_header    Host                $http_host;
          proxy_set_header    X-Real-IP           $remote_addr;
          proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
          proxy_set_header    X-Forwarded-Proto   $scheme;
          proxy_set_header    X-Frame-Options     SAMEORIGIN;
      
          proxy_pass http://gitlab;
        }
      
        ## Enable gzip compression as per rails guide:
        ## http://guides.rubyonrails.org/asset_pipeline.html#gzip-compression
        ## WARNING: If you are using relative urls remove the block below
        ## See config/application.rb under "Relative url support" for the list of
        ## other files that need to be changed for relative url support
        #location ~ ^/(assets)/ {
        #  root /home/git/gitlab/public;
        #  #gzip_static on; # to serve pre-gzipped version
        #  expires max;
        #  add_header Cache-Control public;
        #}
      
        error_page 502 /502.html;
      }
      

      Apache(2.2.9) 配置文件:参考 gitlab.conf 用于 gitlab 6.0.6 和 gitlab-8.0-apache2.2.conf 用于 Apache 2.2 上的 gitlab 8.0.0

      #  Module dependencies
      #  mod_rewrite
      #  mod_proxy
      #  mod_proxy_http
      <VirtualHost *:80>
      
      
        ServerAdmin admin@example.com
        DocumentRoot "/data/webapp/www/wordpress"
      
        ServerName www.example.com
        ServerAlias example.com
      
        #ErrorLog "logs/wordpress-error_log"
        #CustomLog "logs/wordpress-access_log" common
      
          #SetEnv ZF2_PATH "/data/webapp/www/ZendFramework-2.3.3/library"
        SetEnv APPLICATION_ENV "development"
        <Directory /data/webapp/www/wordpress>
          DirectoryIndex index.php
          AllowOverride All
          Order allow,deny
          Allow from all
        </Directory>
      
        #ServerName www.example.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 /gitlab>
          Order deny,allow
          Allow from all
      
          ProxyPassReverse http://127.0.0.1:9095
          ProxyPassReverse http://www.example.com//
      
          RewriteEngine on
          #RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
          RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]
        </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
        #  RewriteCond /code/gitlab/{REQUEST_FILENAME} !-f
        #  RewriteRule .* http://127.0.0.1:9095%{REQUEST_URI} [P,QSA,NE]
      
        # needed for downloading attachments
        #DocumentRoot /home/git/gitlab/public
        Alias /gitlab /home/git/gitlab/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
      
        LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
        ErrorLog  logs/example.com_error.log
        CustomLog logs/example.com_forwarded.log common_forwarded
        CustomLog logs/example.com_access.log combined env=!dontlog
        CustomLog logs/example.com.log combined
      
      </VirtualHost>
      

      【讨论】:

        【解决方案4】:

        您解决了这个问题吗?

        如果没有,请尝试将location / 指令更新为:

        location /gitlab {
          # serve static files from defined root folder;.
          root /home/gitlab/gitlab/public;
          # @gitlab is a named location for the upstream fallback, see below
          try_files $uri $uri/index.html $uri.html @gitlab;
        }
        

        如果这不起作用,请粘贴/var/log/nginx/gitlab_error.log 的最后几行。

        【讨论】:

        • 我有一个与原始问题类似的问题,我尝试了您的方法来解决它。我的错误日志中没有任何新条目,看来仅此一项并不能解决问题。我想做的是将您的建议应用于以下内容:unicorn.bogomips.org/examples/nginx.conf
        【解决方案5】:

        此配置有效

        # GITLAB
        # Maintainer: @randx
        # App Version: 3.0
        
        upstream gitlab {
          server unix:/home/gitlab/gitlab/tmp/sockets/gitlab.socket;
        }
        
        server {
          listen 80;         # e.g., listen 192.168.1.1:80; 37.59.125.28:
          server_name gitlab.<YOUR_DOMAIN>.com;     # e.g., server_name source.example.com;
          root /home/gitlab/gitlab/public;
        
          # individual nginx logs for this gitlab vhost
          access_log  /var/log/nginx/gitlab_access.log;
          error_log   /var/log/nginx/gitlab_error.log;
        
          location / {
            # serve static files from defined root folder;.
            # @gitlab is a named location for the upstream fallback, see below
            try_files $uri $uri/index.html $uri.html @gitlab;
          }
        
          # if a file, which is not found in the root folder is requested,
          # then the proxy pass the request to the upsteam (gitlab unicorn)
          location @gitlab {
            proxy_read_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
            proxy_connect_timeout 300; # https://github.com/gitlabhq/gitlabhq/issues/694
            proxy_redirect     off;
        
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   Host              $http_host;
            proxy_set_header   X-Real-IP         $remote_addr;
        
            proxy_pass http://gitlab;
          }
        }
        

        我在 nginx 可用配置和已启用配置之间有一个错误的符号链接。

        【讨论】:

        • 我很难看出您的配置与您问题中的配置之间的区别(服务器名称除外)。允许www.mysuperserver.com/gitlab 的部分在哪里?
        • 上述配置似乎是针对子域的。我的回答虽然未经测试,但应该适用于被问到的问题,www.mysuperserver.com/gitlab。如果有人尝试过,最好得到确认。
        猜你喜欢
        • 2018-05-30
        • 2018-01-24
        • 1970-01-01
        • 2020-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多