Apache、Ubuntu 多個連接埠或子網域,使用 SSL 反向代理程式到相同伺服器的不同連接埠

Apache、Ubuntu 多個連接埠或子網域,使用 SSL 反向代理程式到相同伺服器的不同連接埠

我在這裡遇到一些困難。我在 azure + Apache2 中安裝了 Ubuntu 18.04 VM。我有一個網域 (example.com) 指向我的虛擬機器的 Web 伺服器 /var/www/,最近設定了 SSL 來保護伺服器的安全。我使用 LetsEncrypt 和“certbot”獲得了一個證書,選擇將所有 HTTP 流量重定向到 HTTPS 的選項。這是我網站的 .conf :

root@wp-vm:/# cat /etc/apache2/sites-available/example.com.conf
<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/example.com/>
        AllowOverride All
    </Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>

我正在同一台伺服器上執行一些其他 Web 服務:RStudio閃亮伺服器(連接埠3838)和Rstudio Server IDE開源(8787)。這些是免費版本,不支援 SSL。我了解到,現在我的網站伺服器使用 SSL,我將無法透過 iframe 將閃亮伺服器的內容嵌入到我的網頁 (wordpress) 中。因此,雖然我之前可以連結到 example.com:3838/app,但在我確保連接埠安全之前,它將不再起作用。

我如何實現以下目標?

http(s)://example.com >> webserver (OK)
http(s)://example.com:48787 >> rstudio @ 8787
http(s)://example.com:43838 >> shiny @ 3838

或者甚至更好,如何使用這樣的子網域進行設定(如果沒有增加太多複雜度):

http(s)://example.com >> apache webserver @ /var/www (OK)
http(s)://rstudio.example.com >> rstudio @ 8787
http(s)://shiny.example.com >> shiny @ 3838

需要明確的是,瀏覽器需要使用 HTTPS 進行通信,但 8787/3838 上的服務仍將保持 HTTP。

我已經嘗試過但無法工作的一些事情:我嘗試加載一堆模組並使用 ProxyPass 和 ProxyPassReverse 以各種方式定義虛擬主機,使用“位置”/子域標籤修改現有虛擬主機,我'我們告訴apache 監聽ports.conf 中的新端口,並打開防火牆(azure 和ufw)。我顯然錯過了一些東西,但不知道是什麼。這是我目前的防火牆,讓我知道我還能提供哪些可能有助於解答的內容。

root@wp-vm:/# ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] Apache Full                ALLOW IN    Anywhere
[ 2] 3838                       ALLOW IN    Anywhere
[ 3] 8787                       ALLOW IN    Anywhere
[ 4] 43838                      ALLOW IN    Anywhere
[ 5] 48787                      ALLOW IN    Anywhere
[ 6] 22/tcp                     ALLOW IN    Anywhere
[ 7] Apache Full (v6)           ALLOW IN    Anywhere (v6)
[ 8] 3838 (v6)                  ALLOW IN    Anywhere (v6)
[ 9] 8787 (v6)                  ALLOW IN    Anywhere (v6)
[10] 43838 (v6)                 ALLOW IN    Anywhere (v6)
[11] 48787 (v6)                 ALLOW IN    Anywhere (v6)
[12] 22/tcp (v6)                ALLOW IN    Anywhere (v6)

阿帕契

root@wp-vm:/# apache2 -v
Server version: Apache/2.4.29 (Ubuntu)
Server built:   2020-08-12T21:33:25

編輯/更新:我意識到我遺漏了一個重要文件的內容,並且我可能一直在錯誤的文件(example.com.conf)中亂搞。

root@wp-vm:/etc/apache2/sites-available# cat example.com-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/example.com/>
        AllowOverride All
    </Directory>


Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

</VirtualHost>
</IfModule

所以我現在不理會 example.com.conf 文件,因為我認為它只是將所有請求重寫為 https 並重定向到 443。看來我將再次重試此文件中的所有內容..

ProxyRequests off
ProxyPass /shiny/ http://localhost:3838/
ProxyHTMLURLMap http://localhost:3838 /shiny

<Location /shiny/>
        ProxyPassReverse /
        ProxyHTMLEnable On
        ProxyHTMLURLMap  /      /shiny/
        RequestHeader    unset  Accept-Encoding
</Location>

編輯#2 離我這麼近我都能嚐到它的味道!我基本上可以使用子目錄(example.com/shiny 和 /rstudio)。然而,地址的輸入方式很挑剔 - 是否包含最後的尾隨“/”以及與“https”相同的內容。

這傢伙似乎在 rstudio 中遇到了類似的問題並透過一系列重寫和重定向「解決」了這個問題(向下滾動到「設定資源限制」部分之前的最終解決方案)。這適用於「根」路徑,但不適用於 /shiny 下的應用程式子目錄。因此 example.com/shiny/ 可以工作,example.com/shiny 透過重定向到 ../shiny/ 可以工作,但 example.com/shiny/app1 不起作用並解析為 /app1。我需要輸入 example.com/shiny/app1/。

我現在對 /shiny 和 /rstudio 做了同樣的事情。這是我當前的.conf。我認為我的問題和問題現在已經簡化為如何使其乾淨、正確且對“/”省略具有魯棒性?

root@wp-vm:/etc/apache2/sites-available# cat *-le*
<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerAdmin webmaster@localhost
    ServerName example.com
    ServerAlias www.example.com
    DocumentRoot /var/www/example.com
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/example.com/>
        AllowOverride All
    </Directory>


Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem

SSLProxyEngine On

# extra redirect for shiny subdirectory
Redirect /shiny /shiny/
# extra redirects for the RStudio subdirectory
Redirect /rstudio /rstudio/
Redirect /auth-sign-in /rstudio/auth-sign-in
Redirect /auth-sign-out /rstudio/auth-sign-out
# Catch RStudio redirecting improperly from the auth-sign-in page
<If "%{HTTP_REFERER} =~ /auth-sign-in/">
  RedirectMatch ^/$     /rstudio/
</If>
##

ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
    AddDefaultCharset Off
    Order deny,allow
    Allow from all
</Proxy>
ProxyPass /shiny/ http://127.0.0.1:3838/
ProxyPassReverse /shiny/ http://127.0.0.1:3838/
ProxyPass /rstudio/ http://localhost:8787/
ProxyPassReverse /rstudio/ http://localhost:8787/

##
RequestHeader set X-Forwarded-Proto "https"
##
</VirtualHost>
</IfModule>

相關內容