I have an Apache2 Configuration that rewrites all urls to localhost:8080. I need one url for images that is not rewritten and instead serves the files from var/www/html (i.e. https://.../images -> var/www/html).
I know how do to that in nginx, but apache2 seems to to complicated for me. All efforts so far did not help, and neither did the official mod-rewrite-docs... Here is the current config. I guess it is just two lines at the right spot...
Thanks for your help...
.
.
.
ServerName ---.---.---
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://localhost:8080/$1 [P,L]
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
.
.
.
Related
I have 2 applications running on port 8080 and 8085. Both has different URL queries/paths and application on port 8085 have keyword demo on all of its URL queries/paths and other doesn't. So it acts as an unique identifier
Can I make any change to /etc/httpd/conf/httpd.conf file so that if the server identifies demo as the keyword then request will reach application on port 8085, if not then request will reach port application on port 8080 ?
If there is a way, please provide sample configuration which goes into httpd.conf file
EDIT 1:
After trying from first 2 of below answers, I couldn't achieve this. I don't know if ProxyPass and ProxyPassReverse are not allowing to achieve this. I tried commenting them, adding them in VirtualHost etc. But did not help.
This is the flow we are expecting:
User will hit URL (without mentioning port) like - https://example.com/demo and this will be routed to app on port 8085 else routed to 8080
May be taking look at my complete httpd.conf might help
Link to my httpd.conf - https://gofile.io/d/tWIHvX
Rewrite Condition if the query string contains a word "demo":
<VirtualHost *:8080>
<Directory /var/www/example/>
Allow From All
RewriteEngine On
RewriteCond %{SERVER_PORT} !^8085$
RewriteCond %{QUERY_STRING} (demo)
RewriteRule ^Demo http://%{HTTP_HOST}:%{SERVER_PORT}/$1 [L,R]
</Directory>
</VirtualHost>
Rewrite condition if the query string does not contain the word "demo":
<VirtualHost *:8085>
<Directory /var/www/example/>
Allow From All
RewriteEngine On
RewriteCond %{SERVER_PORT} !^8080$
RewriteCond %{QUERY_STRING} (!demo)
RewriteRule ^Demo http://%{HTTP_HOST}:%{SERVER_PORT}/$1 [L,R]
</Directory>
</VirtualHost>
if the SERVER_PORT doesn't work, please try the port directly like below:
RewriteRule ^Demo http://%{HTTP_HOST}:8085/$1 [L,R]
Try:
<VirtualHost *:8080>
#some conf
RewriteCond %{REQUEST_URI} demo [OR]
RewriteCond %{QUERY_STRING} demo
RewriteRule ^ http://%{HTTP_HOST}:8085%{REQUEST_URI} [QSA,L]
#some more conf
</VirtualHost>
OR
<VirtualHost *:8085>
#some conf
RewriteCond %{REQUEST_URI} !demo
RewriteCond %{QUERY_STRING} !demo
RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [QSA,L]
#some more conf
</VirtualHost>
Note: self answer after being able to achieve this
Achieved this using LocationMatch directive easily. Below is the configuration.
As this is regex dependent you can change to check conditions like ends with, starts with, contains and etc. And both the below LocationMatch conditions are in my httpd.conf
1.If URL like 192.168.1.113/demo then go to 8085
<LocationMatch "^/(demo)(.*)$">
RewriteEngine on
ProxyPass http://localhost:8085/ nocanon
ProxyPassReverse http://localhost:8085/
</LocationMatch>
2.If URL not like 192.168.1.113/demo then go to 8080
<LocationMatch "^/((?!demo).)*$">
RewriteEngine on
ProxyPass http://localhost:8080/ nocanon
ProxyPassReverse http://localhost:8080/
</LocationMatch>
I'm running an app that utilizes laravel-echo-server. So for me to run it on my production server over https I need to use apache proxy reverse to redirect my /tools/socket.io/ urls to http://localhost:6001.
While that is working fine I keep getting this error Cannot Post / because I need to pass along the params from my domain url to my reverse proxy url.
Here is my apache config:
ProxyPreserveHost On
ProxyRequests off
ProxyPass /tools/socket.io/ http://localhost:6001/
ProxyPassReverse /tools/socket.io/ http://localhost:6001/
I came across this post stack overflow but the implementation didn't work for me.
So this are the parameters I need to pass and their values are dynamic EIO transport t. and my url is like
https://exampledomain.com/tools/socket.io/?EIO=3&transport=polling&t=Ml8lZDJ
Here's my apaxhe config within my SSL virtualhost for my domain:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) ws://localhost:6001/$1 [P,L]
ProxyPass /socket.io http://localhost:6001/socket.io
ProxyPassReverse /socket.io http://localhost:6001/socket.io
AWS CentOS 64b / HTTP / Wildfly 10 / PHPBB php forum / java project
Scenario :
My URL is : www.apisani.org
I have httpd listening on port 80
I have Wildfly listening on port 8080
My needs : I need to type in the browser : www.apisani.org and be redirected to wildfly - java application on port 8080 but dont want to show the port number in the URL.
I need to type in the browser : www.forum.apisani.org and be redirected to httpd on port 80 - phpbb forum website.
I know how to setup wildfly in standalone.xml and welcome-content to redirect www.apisani.org to www.apisani.org/app/indexapp.xhtl but the problem is given above on "My needs" section
[UPDATED BASED ON FISH REPLIES...]
We are almost there !
The <virtualHost> is working like a charm !
:( The ReWriteConde redirects to wildfly URL app instead of forum...
RewriteCond %{HTTP_HOST} ^www\.forum\.apisani\.org$
RewriteRule (.*) http://apisani.org:80/phpBB3/$1 [P,L]
RewriteCond %{HTTP_HOST} ^forum\.apisani\.org$
RewriteRule (.*) http://apisani.org:80/phpBB3/$1 [P,L]
<VirtualHost *:80>
Servername www.apisani.org
ServerAlias apisani.org
ProxyPreserveHost on
ProxyPass / http://apisani.org:8080/app/index
ProxyPassReverse / http://apisani.org:8080/app/index
</VirtualHost>
I'd suggest to use a rewrite and [P] for that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.apisani.org$
RewriteRule (.*) http://wildfly:8080/$1 [P,L]
RewriteCond %{HTTP_HOST} ^www.forum.apisani.org$
RewriteRule (.*) http://phpbb-forum-website:80/$1 [P,L]
$1 refers to (.*) --> the whole URI. If you want to pass that to your application. If not, just lose the brackets and $1
Let us know if that was what you were looking for. And if not, even more, so we can help furthermore ;)
UPDATE
RewriteCond %{REQUEST_URI} !^/php
RewriteCond %{HTTP_HOST} ^(www\.)?apisani\.org$
RewriteRule ^/(.*)$ apisani.org:8080/<path> [P,L]
RewriteCond %{HTTP_HOST} ^(www\.)?forum\.apisani\.org$
RewriteRule ^/(.*)$ apisani.org/<path> [P,L]
I'm sure this is a simple one but I'd like to get some insight on this. I have a vm running plex media server and I'm trying to setup mod_rewrite for this. I'd like to have an HTTP connection -> SSL -> Plex
Keeping it simple, without SSL the following works
ServerName plex
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
RewriteCond %{HTTPS} !=on
ProxyPass / http://127.0.0.1:32400/
ProxyPassReverse / http://127.0.0.1:32400/
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/web
RewriteCond %{HTTP:X-Plex-Device} ^$
RewriteRule ^/$ /web/$1 [R,L]
This allows me to enter
http://plex/
Which rewrites to
http://plex/web
And removes my port number, now if I take the same as above and use https at lines 9 and 10 I break the rewrite.
Interestingly, if I use
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
RewriteCond %{HTTPS} !=on
ProxyPass / http://127.0.0.1:32400/
ProxyPassReverse / http://127.0.0.1:32400/
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/web
RewriteCond %{HTTP:X-Plex-Device} ^$
RewriteRule ^/?(.*) https://%{SERVER_NAME}:32400/web/$1 [R,L]
This works fine, but the port number is not removed. I understand this is because I'm including it in my rewrite rule.
So my question here is really two fold.
A) Why would using https instead of http at lines 9 and 10 not work as desired?
B) What is the correct setup to forward http->https while using a rewrite rule?
tyia
Try %{HTTP_HOST} instead of %{SERVER_NAME}
I have to forward requests from internet client like this one :
https://www.app.com/AppServer?User=guest&ID=8PKX3Q2DT45&Type=laptop&Cmd=exec
to internal server with changing some parameters :
https://192.168.0.1/AppServer?User=guest&ID=NEW_ID&Type=NEW_TYPE&Cmd=exec
with Apache web server. NEW_ID and NEW_TYPE are static variables.
I've tried differents things with ProxyPass and RewriteCond %{QUERY_STRING} but without success.
RewriteEngine On
RewriteCond %{QUERY_STRING} .*User=(\w+).*&ID=(\w+).*&Type=(\w+).*&Cmd=(\w+).*$ [NC]
RewriteRule . HOST/AppServer?User=$1&ID=NEW_ID&Type=NEW_TYPE&Cmd=Exec [R=301,L]
ProxyPass HOST/AppServer
ProxyPassReverse HOST/AppServer
(URL has been replaced with HOST because of post restriction)
Also, I should be able to change header with RequestHeader (this work properly).
Could you help me to build the configuration ?
Kindest regards,
I answer to my question, this work :
#NEW UA
RequestHeader set User-Agent "NEW_UA"
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)OLD_ID(.*)OLD_DEVICE(.*)$
RewriteRule (.*) HOST/AppServer/?%1NEW_ID%2NEW_DEVICE%3 [P,L]
ProxyPass / HOST/
ProxyPassReverse / HOST
Thanks,