apache mod_rewrite mod_proxy subdomain to dir - linux

i have a domain www.example.com as front end, and i have many servers having front end servers
like games.example.com
what i want www.example.com in the background will access games.example.com
so games.example.com = www.example.com/games
this code will handle any access to directory /games will access the backend server
<Location /games>
ProxyPassReverse games.example.com
ProxyPassReverse games.example.com:80
RewriteEngine On
RewriteRule games(.*)$ http://games.example.com/$1 [QSA,P,L]
</Location>
it's working but
its giving me this url
www.example.com/web/login.php which will raise 404 page not found how can i append word games in any URL by being like this www.example.com/games/web/login.php ?

<Location /games>
ProxyPassReverse games.example.com
ProxyPassReverse games.example.com:80
RewriteEngine On
RewriteRule games(.*)$ http://games.example.com/games/$1 [QSA,P,L]
</Location>

Related

ProxyPass custom port Redirect to https

I would like to grand access to certain IP's to mysite.com:1234 but deny that port to other users and redirect them to port 80 or 443, the first part is working with my vhosts file but I can't seem to get the redirection part working, other IP's get 403 error
<VirtualHost 12.123.123.123:443>
<Directory "/home/mysite/public_html">
Options -Indexes -FollowSymLinks +SymLinksIfOwnerMatch
AllowOverride All
SSLRequireSSL
</Directory>
<IfModule mod_proxy.c>
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
ProxyPass / http://127.0.0.1:1234/
ProxyPassReverse / http://127.0.0.1:1234/
<Proxy "http://127.0.0.1:1234/">
Deny from all
allow from 1.1.1.1
allow from 1.1.1.2
allow from 1.1.1.3
allow from 1.1.1.4
</Proxy>
</IfModule>
One solution would be to implement your proxy rule with mod_rewrite. Rewrite rules have a [P] flag which means "reverse proxy". You could have a rewrite condition that checks for the correct IP addresses before a rule that does the proxy. Then all other requests would fall through to a second rule that does the redirect.
RewriteEngine On
# Reverse proxy for allowed remote addresses
RewriteCond %{REMOTE_ADDR} ^1.1.1.[1234]$
RewriteRule ^/?(.*)$ http://127.0.0.1:1234/$1 [P,L]
# Redirect everything else to the main site
RewriteRule ^/?(.*)$ https://mysite.example/$1 [R=301,L]

Converting Apache rewrite rules to IIS

I have these rules setup in Apache and similar ones in NGINX. Now I want to setup in IIS. I haven’t got my head around IIS yet. Does anyone have links to some good resources to help me understand IIS rewrite rules to convert the Apache rules below? I already have ARR and rewrite rule modules installed in IIS.
Thanks
<VirtualHost *:80>
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([A-Za-z0-9-]+)$
RewriteRule / http://127.0.0.1:3000/?portgw=8080&alias=%1&theme=%1 [P]
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>
<VirtualHost *:8080>
RequestHeader set Host "127.0.0.1:8010"
ProxyPreserveHost On
ProxyPass / ws://127.0.0.1:8010/
ProxyPassReverse / ws://127.0.0.1:8010/
</VirtualHost>
The URL Rewrite Module in IIS 7 and above provides an import feature that greatly simplifies the process of converting Apache mod_rewrite rules to IIS URL rewrite rules.
More information about importing apache mod_rewrite rules to iis you can refer to this link: https://learn.microsoft.com/en-us/iis/extensions/url-rewrite-module/importing-apache-modrewrite-rules.

Proxyreverse in Apache2 with wordpress

I have installed and configured Wordpress on my server using also apach2 virtualhosts.
I made a virtualhost with this config
<VirtualHost *:80 *:443>
ServerAdmin yourluxuryroad#gmail.com
ServerName yourluxuryroad.com
ServerAlias www.yourluxuryroad.com
DocumentRoot /var/www/yourluxuryroad
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.yourluxuryroad.com [OR]
RewriteCond %{SERVER_NAME} =yourluxuryroad.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
ProxyPreserveHost On
ProxyRequests Off
ProxyPass /node-yrl-book http://localhost:5000
ProxyPassReverse /node-yrl-book http://localhost:5000
</VirtualHost>
<Directory /var/www/yourluxuryroad/>
AllowOverride All
</Directory>
As you can see from the config i'm trying to set the ProxyPass directive for redirect the requests recived on the path /node-yrl-book to a nodejs service ( made using expressjs ) at port 5000 but this is not working, instead of getting a redirect to that service i get the 404 Page not found wordpress page.
If I make a request at my_ip/node-yrl-book instead it works correctly and i am redirected to the service at port :5000
I suppose that i'm missing something in my configuration but i'm not understanding what..
Maybe is something in wordpress that has to be changed?
You have way too much going on.
ProxyPass -or- DocumentRoot, not both.
You can either serve the page from apache (by using DocumentRoot), or you can serve the page from nodejs (by using ProxyPass).
Finally i solved this, I made an SSL certificate for my website using let's encrypt certbot, This script created a new virtualhost in another file for the https requests ( called /etc/apache2/sites-available/myDomain-le-ssl.conf ) That virtualhost was overriding my proxypass directive, editing also this virtualhost made all work

Node.js on Linux endpoint works with 'api//method' but not 'api/method'

I am running a node js server on linux aws ec2. I am trying to create a subdomain so I can run my node rest api at http://api.domain.com.
httpd.conf
<VirtualHost *:80>
ServerName api.domain.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /server.js
</IfModule>
Everything works great, but when I try and hit http://api.domain.com/method, it bombs out. However http://api.domain//method (emphasis on the double //) works fine. If I just hit http://api.domain/ I get the expected 'Cannot Get /' error message.
Also, http://ip-address:3000/method works just fine
I'm assuming I need some extra entry in my .htaccess file or something but I can't find anything out there...
Solution
I got it working by wrapping the proxies in double quotes:
<VirtualHost *:80>
ServerName api.domain.com
ProxyPreserveHost On
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass "/" "http://localhost:3000/"
ProxyPassReverse "/" "http://localhost:3000/"
</VirtualHost>
After running sudo service httpd restart everything worked as expected.

Config apache and nodejs socket.io

Hi I'm working on a website using apache 2.2 and feathersjs.
on this website we have DV SSL so we can't access subdomains using https.
my node application is running on 3030 port. so I can access to feathersjs app using this address:
http://mywebsite.com:3030
but https://mywebsite.com:3030 not working.
I need to use a Location like https://mywebsite.com/socket/ to connect to feathersjs websocket (guess ws://localhost:3030)
here is client side code:
const host = 'https://mywebsite.com/socket';
const socket = io(host,{
transports: ['websocket'],
forceNew: true
});
I need httpd configuration to connect ws over https.
post_virtualhost_global.conf
<VirtualHost 185.173.106.42:80>
ServerName mywebsite.com
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* ws://localhost:3030%{REQUEST_URI} [P]
ProxyPass /socket http://localhost:3030/
ProxyPassReverse /socket http://localhost:3030/
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
</VirtualHost>
<VirtualHost 185.173.106.42:443>
ServerName freevery.com
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule .* ws://localhost:3030%{REQUEST_URI} [P]
ProxyPass /socket http://localhost:3030/
ProxyPassReverse /socket http://localhost:3030/
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
<Location /feathers>
ProxyPass http://localhost:3030/
ProxyPassReverse http://localhost:3030/
</Location>
</VirtualHost>
if I try to connect web socket using http://mywebsite.com/socket it says
Failed to load
http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1:
Redirect from
'http://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1' to
'https://mywebsie.com/socket.io/?EIO=3&transport=polling&t=L-1lgZ1'
has been blocked by CORS policy: No 'Access-Control-Allow-Origin'
header is present on the requested resource. Origin
'http://localhost:3001' is therefore not allowed access.
and if I try to connect web socket using https://mywebsite.com/socket it says
Failed to load
https://mywebsite.com/socket.io/?EIO=3&transport=polling&t=L-1lUP5: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:3001' is therefore not allowed
access. The response had HTTP status code 404.
I added Header set Access-Control-Allow-Origin "*" to my .htaccess file but problem exists.
what is wrong with my configurations?
Problem solved. Apache had conflicts with nginx and 301 error was form it. So I used nginx to proxy websocket and it's working on a subdomain without any problem.

Resources