如何在 Apache 中使用 mod_rewrite -> mod_jk ->; tomcat设置?

发布于 2024-10-25 05:51:24 字数 1775 浏览 4 评论 0原文

与我之前的一些问题有关。

我现在有了一个我非常喜欢的设置;

Apache httpd 侦听端口 80,接受 http 和 https 连接。 多个 Tomcat 实例在多个 AJP 端口上运行。

Mod_Jk正在向不同的tomcat实例发送不同的url请求;

www.mydomain.com/demo -> tomcat:8101
www.mydomain.com/test -> tomcat:8102
www.mydomain.com/     -> tomcat:8100

这是通过 httpd.conf (或包含的子文件)中的以下配置来实现的;

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info

NameVirtualHost *:80

<VirtualHost *:80>
    JkMount /demo* demoTomcat (workers.properties not shown)
    JkMount /test* testTomcat
    JkMount /* rootTomcat
</VirtualHost>

这一切都非常有效。我还使用类似的 VirtualHost 标签设置了 SSL 并运行 https 连接;

<VirtualHost _default_:443>
    JkMount /demo* demoTomcat 
    JkMount /test* testTomcat
    JkMount /* rootTomcat
... SSL Stuff follows ....

我现在遇到的问题是我的 SSL 证书仅适用于 www.mydomain.com 而不是 mydomain.com。

建议我使用以下 mod_rewrite 调用;

Options +FollowSymlinks
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [PT,L]

我已将它们放在 httpd.conf 文件中 mod_jk 规则之前和之后。 Apache 起初抱怨 RewriteEngine 是一个无效命令,但是当我首先想起 LoadModule 命令时,这种情况就消失了:) 现在 Apache 重新启动得很好,服务器启动并接受请求,一切都按照它以前的方式工作......但是那就是了只是呢,这些mod_rewrite命令好像没有效果?

我在浏览器中输入 http://mydomain.com ,然后我就正常访问了我的网站。该网址似乎没有更改为 http://www.mydomain.com,当我开始访问受保护区域时,我收到警告,指出 mydomain.com 不安全并为我提供了来自其他名为 www.mydomain.com 的网站的证书(为什么这是一个问题,并且它不能仅使用某些逻辑来实现同一网站,我不知道)我不知道!)。

我是否将 mod_rewrite 规则放置在错误的位置?我读到它应该可以工作,重写应该将 url 更改为 www.然后通过 mod_jk 进行进一步的操作?

Related to some of my earlier questions.

I now have a setup I quite like;

Apache httpd listening on port 80 accepting http and https connections.
Several Tomcat instances running on several AJP ports.

Mod_Jk is sending different url requests to different tomcat instances;

www.mydomain.com/demo -> tomcat:8101
www.mydomain.com/test -> tomcat:8102
www.mydomain.com/     -> tomcat:8100

This is achieved with the following config in httpd.conf (or included sub files);

LoadModule jk_module modules/mod_jk.so
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogLevel info

NameVirtualHost *:80

<VirtualHost *:80>
    JkMount /demo* demoTomcat (workers.properties not shown)
    JkMount /test* testTomcat
    JkMount /* rootTomcat
</VirtualHost>

And this all works great. I also have SSL setup and running for https connections using a similar VirtualHost tag;

<VirtualHost _default_:443>
    JkMount /demo* demoTomcat 
    JkMount /test* testTomcat
    JkMount /* rootTomcat
... SSL Stuff follows ....

What I'm now having trouble is that my SSL Cert is only for www.mydomain.com and NOT mydomain.com.

I've been advised to use the follow mod_rewrite calls;

Options +FollowSymlinks
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [PT,L]

I've placed these before before and after the mod_jk rules in the httpd.conf file. Apache was at first complaining that RewriteEngine was an invalid command, but that went away when I remembered the LoadModule command first :) Now Apache restarts just fine, the server starts and accepts requests and everything works the way it use to ... but thats just it, these mod_rewrite commands appear to have no effect?

I type http://mydomain.com into the browser and I just get my website as per normal. The url does not appear to change to http://www.mydomain.com and when I start to access the secured areas I get warnings that mydomain.com is NOT secured and is serving me a cert from some other website called www.mydomain.com (why this is an issue at all and it can't just use some logic to realise its the same site, I don't know!).

Am I placing the mod_rewrite rules in the wrong place? I've read that it should work, the rewrites should change the url to www. and then pass through to the mod_jk stuff for anything further?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

但可醉心 2024-11-01 05:51:24

将此代码段放在您的 apache 配置中最后一个 jkmount 行之后

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} =on
        RewriteCond %{HTTP_HOST} !^www\.example\.name$ [NC]
        RewriteRule ^ https://www.example.name%{REQUEST_URI} [NE,L,R=301]
    </IfModule>

此规则的作用是 IF 方案是 https 并且您的 http 主机不是 www.mydaomain.com THEN 将请求 https://example.com/foo 重定向到 https://www.example。 com/foo 具有 301 http 状态。

Place this snippet right after last jkmount line in your apache config:

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTPS} =on
        RewriteCond %{HTTP_HOST} !^www\.example\.name$ [NC]
        RewriteRule ^ https://www.example.name%{REQUEST_URI} [NE,L,R=301]
    </IfModule>

What this rule is doing is IF scheme is https and your http host is NOT www.mydaomain.com THEN redirect request https://example.com/foo to https://www.example.com/foo with a 301 http status.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文