How to redirect one URL to a different directory (outside of DOCKER) using apache2

huangapple go评论84阅读模式
英文:

How to redirect one URL to a different directory (outside of DOCKER) using apache2

问题

我有一个安装在 Docker 内的 WordPress 网站 example.com,同时我还有一个存储在 /var/www/store 中的 CI 项目。我希望当用户在地址栏中输入 example.com/store 时,能够重定向到 Docker 外部的 /var/www/store。以下是我的尝试:

<VirtualHost *:80>
  ServerName example.com
  Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
  RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
  RequestHeader set "X-Forwarded-SSL" expr=%{HTTPS}
  ServerAdmin kontak@japati.net
  ServerName example.com
  ProxyRequests Off

  <Location />
    ProxyPreserveHost On
    ProxyPass http://127.0.0.1:8100/
    ProxyPassReverse http://127.0.0.1:8100/
  </Location>
  
  RewriteEngine On
  RewriteRule ^/store(.*)$ /var/www/store$1 [L]

  SSLEngine on
  SSLCertificateFile /opt/acme-cert/example.com/cert.pem
  SSLCertificateKeyFile /opt/acme-cert/example.com/site.key
  SSLCertificateChainFile /opt/acme-cert/example.com/fullchain.cer

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

但是这并不起作用,WordPress 显示页面未找到错误。我还尝试了 alias /store /var/www/store,但仍然不起作用。

英文:

I have example.com which is an wordpress site installed inside docker, and I also have CI project stored in /var/www/store. I want user to be redirected to /var/www/store (outside docker) when entering example.com/store in address bar. This is my attempt.

&lt;VirtualHost *:80&gt;
  ServerName example.com
  Redirect permanent / https://example.com/
&lt;/VirtualHost&gt;

&lt;VirtualHost *:443&gt;
  RequestHeader set &quot;X-Forwarded-Proto&quot; expr=%{REQUEST_SCHEME}
  RequestHeader set &quot;X-Forwarded-SSL&quot; expr=%{HTTPS}
  ServerAdmin kontak@japati.net
  ServerName example.com
  ProxyRequests Off

  &lt;Location /&gt;
    ProxyPreserveHost On
    ProxyPass http://127.0.0.1:8100/
    ProxyPassReverse http://127.0.0.1:8100/
  &lt;/Location&gt;
  
  RewriteEngine On
  RewriteRule ^/store(.*)$ /var/www/store$1 [L]

  SSLEngine on
  SSLCertificateFile /opt/acme-cert/example.com/cert.pem
  SSLCertificateKeyFile /opt/acme-cert/example.com/site.key
  SSLCertificateChainFile /opt/acme-cert/example.com/fullchain.cer

  ErrorLog ${APACHE_LOG_DIR}/error.log
  CustomLog ${APACHE_LOG_DIR}/access.log combined
&lt;/VirtualHost&gt;

But this doesn't work, I get page not found error from wordpress. I also tried alias /store /var/www/store but it still doesn't work.

答案1

得分: 0

你的问题是你试图将Docker与Apache混合使用,但现在所有请求都被转发到端口8100,这意味着显然RewriteRule是无法帮助的。(它们也会被转发。)(无论如何,你不能使用RewriteRule去访问一个物理位置,只能访问一个URL。)

你的<Location />块正在将所有请求转发到端口8100。让我们来修复一下:

你需要添加这个:

<VirtualHost *:443>

  #.......一些配置.......
  Alias /store "/var/www/store" #<---------------- 将"/store"映射到/var/www/store
  ProxyPass /store ! #<-------------- 将这行放在其他ProxyPass声明之前
  <Location />
    ProxyPreserveHost On
    ProxyPass http://127.0.0.1:8100/
    ProxyPassReverse http://127.0.0.1:8100/
  </Location>

或者你可以这样做:

#.......一些配置.......
Alias /store "/var/www/store" #<---------------- 将"/store"映射到/var/www/store
<Location /store>
  ProxyPass !
</Location>
<Location />
  ProxyPreserveHost On
  ProxyPass http://127.0.0.1:8100/
  ProxyPassReverse http://127.0.0.1:8100/
</Location>
英文:

Your problem is that you're trying to mix Docker with Apache, but right now ALL requests are getting forwarded to port 8100, which means obviously RewriteRules aren't going to help. (They'll get forwarded too.) (Anyways, you can't use RewriteRule to go to a physical location, only to a URL.)

Your <Location /> block is forwarding ALL requests to port 8100. Let's fix that:

You'll want to add this:

&lt;VirtualHost *:443&gt;

  #.......Stuff.......
  Alias /store &quot;/var/www/store&quot; #&lt;---------------- map &quot;/store&quot; to /var/www/store
  ProxyPass /store ! #&lt;-------------- Place this before any other ProxyPass declarations
  &lt;Location /&gt;
    ProxyPreserveHost On
    ProxyPass http://127.0.0.1:8100/
    ProxyPassReverse http://127.0.0.1:8100/
  &lt;/Location&gt;

Or you could instead do this:

  #.......Stuff.......
  Alias /store &quot;/var/www/store&quot; #&lt;---------------- map &quot;/store&quot; to /var/www/store
  &lt;Location /store&gt;
    ProxyPass !
  &lt;/Location&gt;
  &lt;Location /&gt;
    ProxyPreserveHost On
    ProxyPass http://127.0.0.1:8100/
    ProxyPassReverse http://127.0.0.1:8100/
  &lt;/Location&gt;

huangapple
  • 本文由 发表于 2023年7月27日 17:47:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76778494.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定