【问题标题】:gitlab docker nginx reverse proxy to a sub pathgitlab docker nginx 反向代理到子路径
【发布时间】:2019-04-16 21:32:24
【问题描述】:

我正在尝试将 nginx 设置为反向代理,以将 apps.mycompany.com/gitlab 路由到与 nginx 在同一服务器上运行的 gitlab docker 容器:

nginx 配置有:

location /gitlab/ {
    proxy_pass     http://127.0.0.1:3000/;
    proxy_redirect default;
}

第一个 http 调用 apps.mycompany.com/gitlab 进行得很顺利,但基本上 html 中的所有 href(例如 href:"/assets/...")仍然路由到 apps.mycompany.com/assets/... 而不是 apps.mycompany.com/gitlab/assets/...

因此没有找到资产和 css 文件。呈现的页面有结构但没有样式,我什至不知道还有什么不起作用。

我对 nginx 的了解不够,无法知道我在做什么错

【问题讨论】:

  • 您能否检查您的资产请求是否收到了redirect 响应?你查了吗here
  • 没有。他们都得到 404 not found

标签: docker nginx gitlab


【解决方案1】:

NGINX

在您的 nginx 配置中添加proxy_set_header 选项并更改proxy_pass,如下所示:

location /gitlab/ {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:3000/gitlab/;
}

GITLAB

您正在寻找的是 GitLab 中的 相对 URL 配置。

如果您有版本 8.5 或更高版本的 GitLab,请根据您的 GitLab 部署类型执行以下操作之一:

DOCKER-COMPOSE 部署

将环境变量external_url 添加到您的docker-compose.yml 文件,示例文件:

gitlab:
    image: 'gitlab/gitlab-ce:11.5.2-ce.0'
    restart: always
    environment:
            GITLAB_OMNIBUS_CONFIG: |
                    external_url 'http://apps.mycompany.com/gitlab/'
    ports:
            - '3000:80'

然后重启 GitLab docker:

docker-compose up -d

DOCKER 部署

如果您没有使用 docker-compose(我强烈推荐),那么您可以在 docker run 命令中添加 external_url 选项,示例执行:

docker run --detach --publish 3000:80 --restart always --env GITLAB_OMNIBUS_CONFIG="external_url 'http://apps.mycompany.com/gitlab/'" gitlab/gitlab-ce:11.5.2-ce.0

GitLab 配置文件更新 - 可用于各种部署

另一种方法是直接修改 GitLab 配置文件,但我建议独立 GitLab 安装而不是 docker 部署。

修改/etc/gitlab/gitlab.rb 中的GitLab 配置,将external_url 值更改为以下:

external_url "http://apps.mycompany.com/gitlab"

进行此更改后,您必须重新配置 GitLab:

sudo gitlab-ctl reconfigure

然后重启服务:

sudo gitlab-ctl restart

您可以在official documentation 中找到有关 GitLab 配置的更多详细信息。

我建议你也检查一下 docker 部署中的 GitLab official documentation

请注意,Omnibus GitLab 中的相对 URL 支持是实验性,并在 8.5 版中引入(对于早期版本,您需要从源代码编译它 - doc)。

【讨论】:

    【解决方案2】:

    我对 https 的 gitlab 设置:

    docker-compose.yml:

    web:
      image: 'gitlab/gitlab-ee:latest'
      restart: always
      hostname: 'gitlab.yourdomain.com'
      environment:
        GITLAB_OMNIBUS_CONFIG: |
          external_url 'https://gitlab.yourdomain.com'
          gitlab_rails['gitlab_shell_ssh_port'] = 9122
          nginx['enable'] = true
          nginx['listen_port'] = 9180
          nginx['listen_https'] = false
      ports:
        - '9180:9180'
        - '9122:22'
      volumes:
        - '/opt/gitlab/config:/etc/gitlab'
        - '/opt/gitlab/logs:/var/log/gitlab'
        - '/opt/gitlab/data:/var/opt/gitlab'
    

    nginx gitlab.conf:

    server {
      listen       443;
      server_name  gitlab.yourdomain.com;
    
      location / {
        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_pass http://127.0.0.1:9180;
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-12-14
      • 2018-03-24
      • 2016-07-22
      • 1970-01-01
      • 2021-11-13
      • 2019-08-05
      • 1970-01-01
      相关资源
      最近更新 更多