mod_jk not working with apache2.2 and tomcat7 - apache

I have configured Apache 2.2 with mod_jk and it is working fine, But in one scenario I need to rewrite from mydomain1 to mydomain2.com
I have googled for this and found some solution (as in below), But It's not working.
Any help would be much appreciated., Thanks
<IfModule jk_module>
JkWorkersFile conf/workers.properties
JkLogFile logs/mod_jk.log
JkLogStampFormat "[%b %d %Y - %H:%M:%S] "
JkRequestLogFormat "%w %V %T"
JkLogLevel info
<VirtualHost *:80>
ServerAdmin admin#www.mydomain1.com
DocumentRoot "C:/apache_docs"
ServerName www.mydomain1.com
ErrorLog logs/www.mydomain1com_log
CustomLog logs/www.mydomain1com_log combined
JkMount / mydomain1
JkMount /* mydomain1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mydomain1.com$ [NC] RewriteRule ^(.*)$ mydomain2.com/$1 [R=301,L]
</IfModule>
</VirtualHost>
</IfModule>

Define a specific Virtualhost for the other domain and add a Redirect as follows:
Redirect / http://mydomain2.com/

Related

SetEnvIf and not logging an IP address in customlog

I'm trying to prevent any requests from a certain IP from being logged.
SetEnvIf is enabled on apache 2.4.6
# apachectl -V
Server version: Apache/2.4.6 (CentOS)
# apachectl -M | grep -i setenvif
setenvif_module (shared)
My vhost.conf entry:
<VirtualHost *:80>
SetEnvIf Remote_Addr "91\.121\.31\.64" dontlog=1
ServerAdmin webmaster#mydomain.com
ServerName mydomain.com
ServerAlias *.mydomain.com
DocumentRoot /home/domains/mydomain.com/htdocs
ScriptAlias /cgi-bin /home/domains/mydomain.com/cgi-bin
ErrorLog /home/domains/mydomain.com/logs/error_log
CustomLog /home/domains/mydomain.com/logs/combined_log combined env=!dontlog
RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.com [OR]
RewriteCond %{SERVER_NAME} =*.mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I restart apache, but that IP is still logged. What am I missing?

Websockets not working while running Portainer with Httpd proxying

I've set up the Portainer UI as a docker container, and it's working great if I connect my browser to port 9000 (as in http://foo.bar.com:9000). I can navigate around the UI and open up container consoles (which use websockets) without a problem.
But, what I need to do i connect with SSL (as in https://foo.bar.com).
I set up an httpd container on the same machine, and gave it the following configuration file:
<VirtualHost *:443>
ServerName foo.bar.com
ProxyPass / http://foo.bar.com:9000/
ProxyPassReverse / http://foo.bar.com:9000/
RequestHeader set X-Forwarded-Proto "https"
<Location /api/websocket/>
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule /api/websocket/(.*) ws://foo.bar.com:9000/api/websocket/$1 [P]
</Location>
## SSL directives
SSLEngine on
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
## Logging
ServerSignature Off
ErrorLog "logs/error_ssl.log"
CustomLog "logs/access_ssl.log" common
</VirtualHost>
Both httpd and portainer are being brought up by separate docker-compose.yml files.
Now, the Portainer web pages still come up just fine, but the consoles for the containers won't work. Somehow, my websocket configuration above is broken. Any ideas on what I might be doing wrong?
After hours of playing with it, I finally got this to work and wanted to share. First, at least in httpd 2.4, you need to explicitly load mod_proxy_wstunnel.so so make sure and put a LoadModule in. Until you do that, nothing will work.
Here is the httpd configuration that was successful:
<VirtualHost *:443>
ServerName foo.bar.com
ProxyPreserveHost on
ProxyPreserveHost On
ProxyRequests Off
# allow for upgrading to websockets
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^/?(.*) ws://foo.bar.com:9000/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^/(.*) http://foo.bar.com:9000/$1 [P,L]
ProxyPass "/" "http://foo.bar.com:9000/"
ProxyPassReverse "/" "http://foo.bar.com:9000/"
## SSL directives
SSLEngine on
SSLCertificateFile "/usr/local/apache2/conf/server.crt"
SSLCertificateKeyFile "/usr/local/apache2/conf/server.key"
## Logging
ServerSignature Off
ErrorLog "logs/error_ssl.log"
CustomLog "logs/access_ssl.log" common
</VirtualHost>
Thanks a lot - saved me lot of trouble setting this up in Apache. I'm using it in local network only so I don't care much about https and certs so I've modified it bit to go through http
<VirtualHost *:80>
ServerName foo.bar.com
ServerAlias foo.bar.alias.com
ProxyPreserveHost On
ProxyRequests Off
# allow for upgrading to websockets
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule ^/?(.*) ws://localhost:9000/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule ^/(.*) http://localhost:9000/$1 [P,L]
ProxyPass "/" "http://localhost:9000/"
ProxyPassReverse "/" "http://localhost:9000/"
# Logging
ErrorLog ${APACHE_LOG_DIR}/portainer_error.log
CustomLog ${APACHE_LOG_DIR}/portainer_access.log combined
</VirtualHost>

LetsEncrypt with Apache2 Redirection

I am struggling with the https redirection of my domain. My domain is hosted with godaddy and my server is hosted with ionos. I was able to install the ssl certification and if I go directly on https: //domain.com it is working as expected. Unfortunately, the redirection is not working at all.
the conf file looks like that:
<VirtualHost *:80>
ServerAdmin admin#domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet
and the ssl conf file like that:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin admin#domain.de
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
# Some rewrite rules in this file were disabled on your HTTPS site,
# because they have the potential to create redirection loops.
# RewriteCond %{SERVER_NAME} =domain.com [OR]
# RewriteCond %{SERVER_NAME} =www.domain.com
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/domain.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com/privkey.pem
</VirtualHost>
</IfModule>
I hope someone has an idea regarding this.
Thanks a lot!
This should work, place it out of the virtualhosts (might not make a difference), and also make sure mod_rewrite is on.
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Edit:
Added the flags R=301 and L for the redirect.
Info:
If the above doesn't work when you place it outside of virtualhosts, you should add the following after the RewriteEngine On:
RewriteOptions InheritDown

Aliasing a URL in Apache2 with mod_jk

I'm using Apache 2.4 with mod_jk and Tomcat running a Java servlet. The application I'm serving has an ugly index URL: accessing www.mydomain.com/ takes the user to www.mydomain.com/view/user/www/. I would like to alias this so that users see www.mydomain.com/app/ instead, and I'm trying to use mod_rewrite to achieve this.
This is the current setup I have:
LoadModule jk_module /usr/lib/apache2/modules/mod_jk.so
JkWorkersFile /etc/apache2/workers.properties
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories # Is it one of these options?
VirtualHost *:80>
ServerName www.mydomain.com
Redirect permanent / https:/www.mydomain.com
</VirtualHost>
<VirtualHost *:443>
ServerName www.mydomain.com
ServerAdmin webmaster#localhost
JkMount / tomcat
JkMount /* tomcat
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
RewriteRule ^/$ /app [PT]
RewriteRule ^/app/?$ /view/user/www [PT,L]
RewriteRule ^/app/(.*)$ /view/user/www/$1 [PT,L]
SSLEngine on
SSLCertificateFile /path/to/my_domain.crt
SSLCertificateKeyFile /path/to/my_domain.key
SSLCertificateChainFile /path/to/chainfile.crt
SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
</VirtualHost>
All of this is in apache2.conf. It's currently doing something different to what I would like. When you type in www.mydomain.com/app, the address in the bar changes to www.mydomain.com/view/user/www (and it serves the correct page). I want the bar to continue to display www.mydomain.com/app but display what tomcat has at /view/user/www, thus hiding the ugly URL from the user. How can I achieve this?
RewriteRule ... [PT] + mod_jk requires
JkOptions +ForwardURICompatUnparsed
Try this:
RewriteRule ^/$ /app [R]
RewriteRule ^/app/?$ /view/user/www [PT]
RewriteRule ^/app/(.*)$ /view/user/www/$1 [PT]
[R] is a redirection, the url should be modified in the address bar.
[PT,L] is not useful since [PT] includes [L].

Apache shows default page instead of https when using http

I'm trying to redirect all my foo.mydomain.com requests to https://foo.mydomain.com.
My VirtualHost contains this:
<VirtualHost *:443>
ServerName foo.mydomain.com
# Indexes + root dir
DirectoryIndex index.html
DocumentRoot /home/web/foo.mydomain.com/htdocs
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/server.crt
SSLCertificateKeyFile /etc/apache2/ssl/server.key
SSLVerifyClient none
<IfModule mime.c>
AddType application/x-x509-ca-cert .crt
AddType application/x-pkcs7-crl .crl
</IfModule>
# Logfiles
ErrorLog /home/web/foo.mydomain.com/logs/error.log
CustomLog /home/web/foo.mydomain.com/logs/access.log combined
</VirtualHost>
So now I'm trying to redirect all the http-request to https with mod_rewrite in this .htaccess file:
Options FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://foo.mydomain.com
Does anyone knows what I'm doing wrong? Thank you in advance!
Try the following in virtual host config for the non https site.
I.e. my settings to do this are:
<VirtualHost *:80>
RewriteCond %{SEVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
.....
</VirtualHost>
This should redirect all incoming http request to https. Then in the config for port 443 you can do whatever you need....
Note: should work in htaccess as well.
Hope that helps. Not sure what you meant by 'default page' but in title and too low rep to leave comments...
Let me know...
Cheers
Robin