简体   繁体   English

使用 Raspbian 和 Apache 的反向代理

[英]Reverse Proxy using Raspbian and Apache

Reverse Proxy using Raspian and Apache使用 Raspian 和 Apache 的反向代理

I have a few machines running different applications on them.我有几台机器在它们上面运行不同的应用程序。 Recently I bought a Raspberry Pi 4, trying to make it to serve as a reverse proxy server for the applications.最近我买了一个 Raspberry Pi 4,试图让它作为应用程序的反向代理服务器。 Let say I have three machines A, B, and C, where假设我有三台机器 A、B 和 C,其中

A: Raspberry Pi 4, running Raspbian buster lite with Apache2 installed.答:Raspberry Pi 4,运行安装了 Apache2 的 Raspbian buster lite。 IP: 192.168.1.2. IP:192.168.1.2。 I am trying to configure it into a reverse proxy server.我正在尝试将其配置为反向代理服务器。

B: A VM running Ubuntu 18.04. B:运行 Ubuntu 18.04 的 VM。 IP: 192.168.1.3. IP:192.168.1.3。 Hosting a GitLab server.托管 GitLab 服务器。

C: A VM running Ubuntu 18.04. C:运行 Ubuntu 18.04 的 VM。 IP: 192.168.1.4. IP:192.168.1.4。 Hosting a RStudio Server.托管 RStudio 服务器。

I also have a domain name with ssl encrypted.我还有一个 ssl 加密的域名。 Let say the domain name is example.com .假设域名是example.com As there are a few applications that I want to be routed by A, I want to make the url example.com/gitlab to be routed to B and example.com/rstudio to be routed to C.由于我希望通过 A 路由一些应用程序,因此我希望将 url example.com/gitlab路由到 B,并将example.com/rstudio路由到 C。

With the code below stored in A:/etc/apache2/sites-available/gitlab.conf , I can route example.com to B. But it will affect the routing to C.将下面的代码存储在A:/etc/apache2/sites-available/gitlab.conf中,我可以将example.com路由到 B。但这会影响到 C 的路由。

# Specify path for Logs
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on

RewriteCond %{HTTP:Upgrade}     !=websocket
RewriteRule            /(.*)     http://192.168.1.3/(.*) [P,L]
ProxyPass              /         http://192.168.1.3/
ProxyPassReverse       /         http://192.168.1.3/
ProxyRequests off

I have also tried replace those / to /gitlab/ .我也尝试将那些/替换为/gitlab/ But it fails as, when I type in example.com/gitlab , the browser was redirected to example.com/users/sign_in rather than example.com/gitlab/users/sign_in as it should be.但它失败了,因为当我输入example.com/gitlab时,浏览器被重定向到example.com/users/sign_in而不是应该的example.com/gitlab/users/sign_in Please also refer to the script below另请参阅下面的脚本

# Specify path for Logs
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on

# Following lines should open gitlab directly from the url
# Map gitlab to gitlab/
RedirectMatch ^/gitlab$ /gitlab/

RewriteCond %{HTTP:Upgrade}     !=websocket
RewriteRule              /gitlab/(.*)     http://192.168.1.3/(.*) [P,L]
ProxyPass                /gitlab/         http://192.168.1.3/
ProxyPassReverse         /gitlab/         http://192.168.1.3/

ProxyRequests off

How should I configure the apache to make this work?我应该如何配置 apache 以使其工作? Many thanks!非常感谢!

Thanks to DusanBajic , I have reached the solution.感谢DusanBajic ,我已经找到了解决方案。

Configuration on GitLab Server GitLab服务器上的配置

On the GitLab server, locate the line在 GitLab 服务器上,找到该行

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

from gitlab.rb in /etc/gitlab/ and modify the line into从 /etc/ gitlab.rb /etc/gitlab/中的 gitlab.rb 并将该行修改为

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

Then run the following scripts然后运行以下脚本

sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
sudo reboot

Then you can access the GitLab via然后您可以通过以下方式访问 GitLab

http://localhost/gitlab/

but you may find it responding error 502. Just wait for a few minutes and try again.但您可能会发现它响应错误 502。请等待几分钟,然后重试。

Configuration on Proxy Server代理服务器上的配置

On the proxy server, create a file gitlab.conf in the directory /etc/apache2/sites-available/ with the following lines在代理服务器上,在目录/etc/apache2/sites-available/中创建一个文件gitlab.conf ,内容如下

# Specify path for Logs
ErrorLog                ${APACHE_LOG_DIR}/error.log
CustomLog               ${APACHE_LOG_DIR}/access.log    combined
RewriteEngine           on

# Following lines should open gitlab directly from the url
# Map gitlab to gitlab/
RedirectMatch           ^/gitlab$              /gitlab/

RewriteCond             %{HTTP:Upgrade}         !=websocket
RewriteRule             /gitlab/(.*)           http://192.168.1.3/gitlab/(.*) [P,L]
ProxyPass               /gitlab/               http://192.168.1.3/gitlab/
ProxyPassReverse        /gitlab/               http://192.168.1.3/gitlab/
ProxyRequests           off

Then run the following scripts然后运行以下脚本

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_html
sudo a2enmod proxy_wstunnel
sudo a2enmod rewrite

and

sudo a2ensite gitlab.conf
sudo systemctl reload apache2
sudo service apache2 restart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM