Apache Reverse Proxy produces too many redirects - redirect

I have configured apache to redirect HTTPS reqests to the local running application server PUMA. PUMA is part of the Ruby on Rails framework.
<IfModule mod_ssl.c>
<VirtualHost *:443>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
Puma Server is listening on http://localhost:3000/
When I access this apache web site from a browser I get: Too many redirections.
And idea?

I had the same issue. Try to add after your Proxy:
RequestHeader set X-Forwarded-Proto https to your ...ssl.conf which is in sites-available folder.

I had same issue, I was trying to setup a SSL termination reverse proxy with apache. I followed this article.
Using 0.0.0.0 instead of localhost worked for me.
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName exemple.com
SSLCertificateFile /path/fullchain.pem
SSLCertificateKeyFile /path/privkey.pem
ProxyPass / http://0.0.0.0:80/
ProxyPassReverse / http://0.0.0.0:80/
</VirtualHost>
</IfModule>

Related

Apache HTTPS Reverse Proxy URL Redirection

I've a node application running on 3001 port. For HTTP to HTTPS, I've configured apache virtual host with reverse proxy and it is working fine. Now I need to redirect
http://nodeapp.mydomain.com to https://nodeapp.mydomain.com and http://nodeapp.mydomain.com:3001 to https://nodeapp.mydomain.com and http://100.100.100.100:3001 to https://nodeapp.mydomain.com
Can anyone please help me how to achieve it using virtual host configuration instead of writing a .htaccess file?
<VirtualHost *:443>
ServerAdmin admin#mydomain.com
ServerName nodeapp.mydomain.com
ProxyPreserveHost On
ProxyRequests Off
ProxyPass / http://100.100.100.100:3001/
ProxyPassReverse / http://100.100.100.100:3001/
ErrorLog "/var/log/httpd/mydomain.com-error_log"
CustomLog "/var/log/httpd/mydomain.com-access_log" common
SSLEngine on
SSLCertificateFile /etc/pki/tls/certs/nodeapp_mydomain_com.crt
SSLCertificateKeyFile /etc/pki/tls/certs/nodeapp_mydomain_com.key
</VirtualHost>
To redirect http://nodeapp.mydomain.com to https://nodeapp.mydomain.com, add these lines to the virtual host configuration:
<VirtualHost *:80>
ServerName nodeapp.mydomain.com
Redirect Permanent / https://nodeapp.mydomain.com/
RewriteEngine on
RewriteCond %{SERVER_NAME} =nodeapp.mydomain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
You can copy paste the same code to the conf file, and modify the corresponding domains/subdomains there.

Apache proxypass redirect to multiple unix sockets

I have a server that runs multiple flask instances using gunicorn.socket, and an apache2 server is configured to proxy the request:
<VirtualHost *:80>
ProxyPreserveHost On
ProxyPass / unix:///run/gunicorn_site0.sock|http://127.0.1.1/
ProxyPassReverse / unix:///run/gunicorn_site0.sock|http://127.0.1.1/
# RewriteEngine On
# RewriteRule ^/0/(.*)$ unix:///run/gunicorn_site0.sock|http://127.0.1.1/$1 [P,L]
# ProxyPass /1 unix:///run/gunicorn_site1.sock|http://127.0.1.1/
# ProxyPassReverse /1 unix:///run/gunicorn_site1.sock|http://127.0.1.1/
# ProxyPass /2 unix:///run/gunicorn_site2.sock|http://127.0.1.1/
# ProxyPassReverse /2 unix:///run/gunicorn_site2.sock|http://127.0.1.1/
</VirtualHost>
As shown above, if I only enable site0, everything works fine, i.e., when I type 127.0.1.1/ in the browser, it proxies to gunicorn_site0.sock with url 127.0.1.1/
However, what I really want to do is to allow the follows:
# Typed in address bar Actual request
http://127.0.1.1/0/some/path -> gunicorn_site0.sock 127.0.1.1/some/path
http://127.0.1.1/1/some/path -> gunicorn_site1.sock 127.0.1.1/some/path
http://127.0.1.1/2/some/path -> gunicorn_site2.sock 127.0.1.1/some/path
I tried multiple ways with RewriteRule but with no success.
Any ideas?
Did you try with a "first site" in /0 instead of / ? there is no reason it fail, but i think you first proxypass "eat" all sub urls:
ProxyPass /0 unix:///run/gunicorn_site0.sock|http://127.0.1.1/0
ProxyPass /1 unix:///run/gunicorn_site1.sock|http://127.0.1.1/1
ProxyPass /2 unix:///run/gunicorn_site2.sock|http://127.0.1.1/2
However you will probably need to make sure all python app are ok running in a "sub-directory". (eg by setting SCRIPT_NAME in gunicorn env)

Ubuntu 14.04 Redirect Domain to Port

I am trying to direct mydomain.com to a specific port. I have run netstat and I know that my code is listening at 0.0.0.0:3000. I have the following in /etc/apache2/sites-available
<VirtualHost *:80>
ProxyPreserveHost On
ProxyRequests Off
ServerName mydomain.com
ServerAlias www.mydomain.com
ProxyPass / http://0.0.0.0:3000/
ProxyPassReverse / http://0.0.0.0:3000/
</VirtualHost>
I was able to get other domains redirected to a subdirectory. When I test using mydomain.com from a browser, I see the default page.

How to redirect different URLs to different ports on the same domain?

I have odoo running on port 8069 and I want to run wordpress on port 80 on the same domain. I would like to redirect example.com* to port 8069 except example.com/wordpress* which I want to go to port 80.
I know this is similar to Redirecting from port 80 to different ports based on URL. I want to implement a reverse proxy solution as advised by abhi-devireddy. I tried the following reverse proxy. All requests are forwarded to port 8069 including http://example.com/wordpress/. Odoo reports error 404 page not found when browser points to http://example.com/wordpress
<VirtualHost *:80>
ServerName odoo
ErrorLog /var/log/odoo/odoo-error.log
CustomLog /var/log/odoo/odoo-access.log combined
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / http://example.com:8069/
ProxyPassReverse / http://example.com:8069/
ProxyPass http://example.com/wordpress/ http://example.com:80/wordpress
ProxyPassReverse http://example.com/wordpress/ http://example.com:80/wordpress
ProxyVia On
LogLevel warn
</VirtualHost>
ProxyPass ! excludes the URL http://example.com:80/wordpress from the reverse proxy. This allows everything except http://example.com:80/wordpress to be mapped to port 8069 while leaving http://example.com:80/wordpress to go through to port 80:
<VirtualHost *:80>
ServerName odoo
ErrorLog /var/log/odoo/odoo-error.log
CustomLog /var/log/odoo/odoo-access.log combined
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyRequests Off
ProxyPass /wordpress !
ProxyPass / http://example.com:8069/
ProxyPassReverse / http://example.com:8069/
Alias /wordpress /var/lib/wordpress/
# ProxyVia On
LogLevel warn
</VirtualHost>

Apache redirects www.domain.com to domain.com/domain

I'm preparing my ubuntu server in Amazon AWS to host a new website. It uses Apache2. I've done this before with no problem whatsoever, for example, to trick a friend I spoofed google.com and www.google.com (editing his hosts file) and everything worked fine with and without 'www.'.
Now I have bought a domain name 'domain.com' with 1&1 , I have configured a subdomain 'www.domain.com' and I have pointed it to my server. The dns record is fine, I checked. I have also edited my sites-enabled default file and added these entries:
<VirtualHost *:80 >
ServerName www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerName domain.com
DocumentRoot /var/www/domain
</VirtualHost>
When I visit domain.com in my browser it works fine, but when I visit www.domain.com I get domain.com/domain/ and obviously a 404.
I used telnet and discovered that the server sends a 301 when you request the root page of host: www.domain.com
I don't know why this is happening, I've tried lots of modifications to the configuration above like:
<VirtualHost *:80 >
ServerAlias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerAlias domain.com
DocumentRoot /var/www/domain
</VirtualHost>
.
<VirtualHost *:80 >
ServerName domain.com
DocumentRoot /var/www/domain
</VirtualHost>
<VirtualHost *:80 >
ServerName www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
.
<VirtualHost *:80 >
ServerName domain.com
Server Alias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
but it kept happening.
For the record, this is what I used for the spoof that still works fine
<VirtualHost *:80 >
ServerName google.com
DocumentRoot /var/www/google
</VirtualHost>
<VirtualHost *:80 >
ServerName www.google.com
DocumentRoot /var/www/google
</VirtualHost>
I've been searching for two days now, but all I find is the same configuration I'm using and no explanation about why this isn't working.
Thanks for your help!
PS: There are no .htaccess files in this directory or in any of the parent directories.
In case anybody enters this question.
I still don't know what was wrong but after moving these configurations around the file they've magically started working normally.
Thanks to everybody who took the time to read the question.
Its better to put
<VirtualHost *:80 >
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/domain
</VirtualHost>
You can't put a space between Server and Alias so I changed to show you
Hope It Works!