AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 565064
Accepted
mr.simonski
mr.simonski
Asked: 2014-01-05 10:41:28 +0800 CST2014-01-05 10:41:28 +0800 CST 2014-01-05 10:41:28 +0800 CST

使用 Apache 和 mod_proxy 重写子域的 URL

  • 772

我有一个带有 mod_proxy 的 Apache Web 服务器和一个运行我的 Grails Web 应用程序的 Tomcat 服务器。Apache 正在使用代理将对 example.com:80 的请求重定向到我在 example.com:8008 上运行的 Tomcat。

我需要通过以下方式重定向请求:

http://subdomain1.example.com:80/some-nice-seo-url

从 apache 应该得到每个代理到

http://example.com:8008/some-nice-seo-url-subdomain1

如何使用以下 apache2 配置来归档它:

NameVirtualHost *:80

<VirtualHost *:80>
        ServerName example.com
        ServerAlias www.example.com
        <Proxy *>
                Order deny,allow
                Allow from all
        </Proxy>

        # compress text, html, javascript, css, xml:
        AddOutputFilterByType DEFLATE text/plain
        AddOutputFilterByType DEFLATE text/html
        AddOutputFilterByType DEFLATE text/xml
        AddOutputFilterByType DEFLATE text/css
        AddOutputFilterByType DEFLATE application/xml
        AddOutputFilterByType DEFLATE application/xhtml+xml
        AddOutputFilterByType DEFLATE application/rss+xml
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE application/x-javascript

        ExpiresActive On
        ExpiresByType text/html "access plus 7 days"
        ExpiresByType image/gif "access plus 1 months"
        ExpiresByType image/jpeg "access plus 1 months"
        ExpiresByType image/png "access plus 1 months"
        ExpiresByType text/css "access plus 14 days"
        ExpiresByType text/javascript "access plus 14 days"
        ExpiresByType application/x-javascript "access plus 14 days"
        ExpiresByType image/ico "access plus 1 months"

        RewriteEngine on
        # www to non-www using search-engine friendly 301
        RewriteCond %{HTTP_HOST} ^www.example.com [NC]
        RewriteRule ^(.*)$ http://example.com$1 [L,R=301]

        # IP canonicalization
        RewriteCond %{HTTP_HOST} ^1\.2\.3\.4
        RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]


        <Directory />
                Options FollowSymLinks
                 AllowOverride None
        </Directory>
        <Directory /opt/example/web-app>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        # redirect everything else to TOMCAT
        ProxyPass               / http://1.2.3.4:8008/
        ProxyPassReverse        / http://1.2.3.4:8008/
        ProxyPassReverseCookieDomain localhost example.com
        ProxyPreserveHost On
</VirtualHost>

感谢您的任何帮助!

apache-2.2
  • 1 1 个回答
  • 4327 Views

1 个回答

  • Voted
  1. Best Answer
    mr.simonski
    2014-02-10T14:54:59+08:002014-02-10T14:54:59+08:00

    好的,我开始运行了...

    以下mod_rewrite条件的关键元素

        RewriteCond   %{HTTP_HOST} ^((?!www\.)[^.]+)\.example\.com$
        RewriteCond %{REQUEST_URI} ^/some.+$
        RewriteRule   ^(.+)  %{HTTP_HOST}$1  [C]
        RewriteRule   ^([^.]+)\.example\.com(.*) http://example.com:8008$2-$1 [P]
        ProxyPassReverse http://example.com/ http://example.com:8008/
    

    第一个条件告诉 Apache 服务器捕获所有指向子域的 URL - 除了子域 www。

    第二个条件告诉 Apache 请求的路径 (URI) 必须以特殊指示符开头。在我的示例中,这是“一些”。这很重要,否则所有 css/images/js 等都属于这种重写条件,我的 tomcat 不会提供所有这些东西。

    现在有两行重写规则。第一个匹配子域并将其保存在 var $1 中。第二个匹配 URI 并将其保存在 $2 中。在第二个重写规则之后,我告诉系统如何建立新的 URL:

    http://example.com:8008$2-$1 [P]
    

    在这里,我重定向到在我的系统上运行的 tomcat 服务器。使用 IP 而不是域名可能没问题。[P] 告诉系统使用 mod_proxy 重定向流量。

    在最后一行中,我们定义了一个 ProxyPassReverse 来重新构建来自代理并进入客户端浏览器的答案文档中的所有链接 - 所以所有http://example.com:8008锚点都返回到http://subdomain1 .example.com。

    使用上述代码时,我的 tomcat 配置出现了一些问题。[P] 标志不保留原始主机。因此,我必须在 tomcat server.xml 中更改我使用的引擎的默认主机:

     <Engine name="Catalina" defaultHost="example.com">
    

    希望这些信息在某个时候对某人有所帮助..

    最好的祝愿

    • 1

相关问题

  • Apache Django Mod_Wsgi - 自动重新加载应用程序

  • Apache:对多个虚拟主机使用相同的目录指令

  • Apache 上的子域不工作 - 找不到服务器

  • PHP 作为 CGI 还是 Apache 模块?

  • 避免将某些丢失的文件记录到 Apache2 错误日志中

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve