【问题标题】:Apache2 ProxyPass for Rails App Gitlab用于 Rails 应用程序 Gitlab 的 Apache2 ProxyPass
【发布时间】:2012-06-12 20:20:39
【问题描述】:

我正在尝试使用 Apache2 设置代理,以便对 http://myipaddress.com 的传入请求转到运行 Gitlab(rails 应用程序)的http://localhost:3000/。以下是我在 Ubuntu 10.04 上的 Apache 配置文件中的内容。我最初可以成功访问 gitlab 默认页面,但是之后我通过单击其他页面执行的任何后续请求都会转到 404 NOT FOUND 页面。我可以在任何这些失败的重定向前手动输入 /gitlab/ ,它们工作得很好。如何在初始请求之后的每个重定向请求之后重写 /gitlab/ 来完成这项工作?

## Setup a proxy which listens on the port that gitlabh does ( from start_server.sh )
ProxyRequests Off
ProxyPass /gitlab/ http://localhost:3000/
ProxyPassReverse /gitlab/ http://localhost:3000/
#DocumentRoot /home/gitlabhq/gitlabhq/public
<Proxy http://localhost:3000/>
  Order deny,allow
  Allow from all
</Proxy>

我知道我可以使用下面的代码来解决我的问题。但是我不知道如何修改gitlab rails服务的前缀。非常感谢您的帮助!

ProxyPass /gitlab/ http://localhost:3000/gitlab/
ProxyPassReverse /gitlab/ http://localhost:3000/gitlab/

更新:

感谢 Friek 的评论,我已经非常接近解决这个问题了。以下是我的 http.conf 文件的一部分。唯一的问题是当我点击主页按钮或 gitlab 应用程序上的徽标时,它会尝试重定向到 gitlab/,这给了我来自 Apache2 的基本 index.html 文件说“它有效!”。如何配置它以允许我简单地获取 /gitlab 并将我带到 gitlab 的根主视图?谢谢!

## For Gitlab using Apache2 Passenger
## Install on Ubuntu by:
## sudo gem install passenger && sudo passenger-install-apache2-module
## but only after running the install_and_configure_git.py script
## and creating a soft link to the rails gitlab /public directory like so:
## sudo ln -s /home/gitlabhq/gitlabhq/public /var/www/gitlab
LoadModule passenger_module /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.13/ext/apache2/mod_passenger.so
PassengerRoot /usr/local/lib/ruby/gems/1.9.1/gems/passenger-3.0.13
PassengerRuby /usr/local/bin/ruby
<VirtualHost *:80>

        ServerName gitlab

        ## Set the overall Document Root
        DocumentRoot /var/www
        <Directory /var/www>
                Allow from all
        </Directory>

        ## Set the Rails Base URI
        RackBaseURI /gitlab
        RailsBaseURI /gitlab
        <Directory /var/www/gitlab>
                Allow from all
                Options -MultiViews
        </Directory>

</VirtualHost>

【问题讨论】:

  • 我一直在尝试做一些非常相似的事情,并且遇到了这个。 github.com/gitlabhq/gitlabhq/pull/642 似乎这个功能没有也可能不会包含在 gitlab 中。
  • 为什么不安装 apache 乘客模块(gem installpassenger &&passenger-install-apache2-module)并将新虚拟主机的 documentroot 指向 /public?对我来说效果很好..
  • 感谢大家的投入,请查看我在上面发布的新信息。
  • 嘿@phil999,只是想告诉您,至少您在上面写下的内容与乘客文件所说的内容相比至少是不安全的。您应该将目录指向&lt;rails_app_path&gt;/public。编辑:刚刚看到您正确链接...也许您应该以某种方式为像我这样的盲人加粗...:/干杯!

标签: apache redirect proxy apache2 proxypass


【解决方案1】:

这个问题也困扰了我很久。现在已经修复了。
最有用的资源来自 Gitlab 官方文档关于using-a-non-bundled-web-server
他们还提供完全可行的recipe

如果您使用的是 Apache 2.4 或更高版本,请使用文件 gitlab-apache24.conf 或 gitlab-ssl-apache24.conf 用于 HTTP 和 vhost 的 HTTPS 版本。

如果您使用的是 Apache 2.2 版,请使用文件 gitlab-apache22.conf 或 gitlab-ssl-apache22.conf 用于 HTTP 和 vhost 的 HTTPS 版本。

问题现在很简单

确保根据您是否选择正确的配置文件 选择是否使用 SSL 服务 GitLab。你唯一需要做的 更改为 YOUR_SERVER_FQDN 和您自己的 FQDN,如果您使用 SSL,则 您的 SSL 密钥当前所在的位置。您可能还需要 更改日志文件的位置。

要确保 Apache2 版本,请使用 apache2 -version
如果使用 HTTPS 版本,您可能需要确保您的证书文件已正确部署,例如文件的放置与指定的配方文件一致。

【讨论】:

    【解决方案2】:

    我在谷歌上搜索我在使用 Apache(在端口 80 上)设置 Rails + unicorn 以代理到 unicorn(在端口 3000 上)时遇到的错误最终来到了这里。如果它对其他人有用,这是我的配置:

    <VirtualHost example.com:80>
      ServerAdmin webmaster@example.com
      ServerName example.com
      ServerAlias www.example.com
    
      ProxyPreserveHost On
      <Location />
          Require all granted
          ProxyPassReverse http://example.com:3000
      </Location>
    
      RewriteEngine on
      RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
      RewriteRule .* http://example.com:3000%{REQUEST_URI} [P,QSA]
    
      DocumentRoot /home/user/rails-dir/public
      ErrorDocument 404 /404.html
      ErrorDocument 422 /422.html
      ErrorDocument 500 /500.html
      ErrorDocument 503 /deploy.html
    
      LogLevel warn
      ErrorLog /home/user/rails-dir/log/apache-error.log
      CustomLog /home/user/rails-dir/log/apache-access.log combined
    </VirtualHost>
    

    【讨论】:

      【解决方案3】:

      这是为了以防新人遇到此问题。

      这对我有帮助,请注意 ProxyPassReverse 行。我的完整问题和解决方案在https://stackoverflow.com/a/22390543/3112527

      <IfModule mod_ssl.c>
      <VirtualHost *:443>
        Servername gitlab.my_domain.com
        ServerAdmin my_admin@my_domain.com
      
        SSLCertificateFile /etc/apache2/ssl.crt/gitlab_my_domain.crt
        SSLCertificateKeyFile /etc/apache2/ssl.crt/gitlab_my_domain_private.key
        SSLCACertificateFile /etc/apache2/ssl.crt/gitlab.ca-bundle
      
        ##### All the other Apache SSL setup skipped here for StackOverflow ####
      
        ProxyPreserveHost On
      
        <Location />
          # New authorization commands for apache 2.4 and up
          # http://httpd.apache.org/docs/2.4/upgrading.html#access
          Require all granted
      
          # For relative URL root "host:your_gitlab_port/relative_root"
          #ProxyPassReverse http://127.0.0.1:8085/gitlab
          #ProxyPassReverse https://gitlab.my_domain.com/gitlab
      
          # For non-relative URL root
          ProxyPassReverse http://127.0.0.1:8085
          ProxyPassReverse https://gitlab.my_domain.com/
        </Location>
      
        # apache equivalent of nginx try files
        # http://serverfault.com/questions/290784/what-is-apaches-equivalent-of-nginxs-try-files
        # https://stackoverflow.com/questions/10954516/apache2-proxypass-for-rails-app-gitlab
        RewriteEngine on
        RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
        RewriteRule .* http://127.0.0.1:8080%{REQUEST_URI} [P,QSA]
        RequestHeader set X_FORWARDED_PROTO 'https'
      
        # needed for downloading attachments
        DocumentRoot /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      /var/log/apache2/gitlab-ssl_error.log
        CustomLog /var/log/apache2/gitlab-ssl_forwarded.log common_forwarded
        CustomLog /var/log/apache2/gitlab-ssl_access.log combined env=!dontlog
        CustomLog /var/log/apache2/gitlab-ssl.log combined
      </VirtualHost>
      </IfModule>
      

      (来自https://github.com/gitlabhq/gitlab-recipes/blob/master/web-server/apache/gitlab-ssl-apache2.4.conf

      【讨论】:

        【解决方案4】:

        我遇到了对我有用的this gist。万一它死了,我会重新发布它。


        独角兽配置文件​​

        编辑文件/home/gitlab/gitlab/config/unicorn.rb

        找线听"#{app_dir}/tmp/sockets/gitlab.socket"并评论。取消注释行听"127.0.0.1:8080"

        apache 所需的模块

        • sudo a2enmod 代理
        • sudo a2enmod proxy_balancer
        • sudo a2enmod proxy_http
        • sudo a2enmod 重写

        /home/gitlab/gitlab/config/gitlab.conf

        <VirtualHost *:80>
          ServerName git.domain.com
        
          # Point this to your public folder of teambox
          DocumentRoot /home/gitlab/gitlab
        
          RewriteEngine On
        
          <Proxy balancer://unicornservers>
            BalancerMember http://127.0.0.1:8080
          </Proxy>
        
          # Redirect all non-static requests to thin
          RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
          RewriteRule ^/(.*)$ balancer://unicornservers%{REQUEST_URI} [P,QSA,L]
        
          ProxyPass / balancer://unicornservers/
          ProxyPassReverse / balancer://unicornservers/
          ProxyPreserveHost on
        
          <Proxy *>
            Order deny,allow
            Allow from all
          </Proxy>
        
          # Custom log file locations
          ErrorLog  /var/log/apache2/gitlab_error.log
          CustomLog /var/log/apache2/gitlab_access.log combined
        </VirtualHost>
        

        【讨论】:

        • 我已经修改了 /etc/apache2/sites-available/default-ssl.conf 并添加了 Alias /gitlab "/var/www/gitlab" 和目录指令。当我尝试通过 example.com/gitlab 访问它时,它不起作用/不加载主页,但 example.com/gitlab/deploy.html 运行良好。如何访问Gitlab首页?
        【解决方案5】:
        <VirtualHost *:80>
        
                ServerName gitlab
        
                ## Set the overall Document Root
                DocumentRoot /var/www
                <Directory /var/www>
                        Allow from all
                </Directory>
        
                ## Set the Rails Base URI
                RackBaseURI /gitlab
                RailsBaseURI /gitlab
                <Directory /var/www/gitlab>
                        Allow from all
                        Options -MultiViews
                </Directory>
        
        </VirtualHost>
        

        这些设置在你的 httpd.conf 或你的站点配置文件中应该可以。如果你有任何反向代理设置,请删除并尝试,它会起作用。,

        如果您在上面的配置中有以下行,请删除以下行,

        ProxyPass /gitlab/ http://localhost:3000/gitlab/
        ProxyPassReverse /gitlab/ http://localhost:3000/gitlab/
        Proxy on
        

        重启你的网络服务器

        service apache2 restart
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-08-08
          • 2012-09-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多