Reverse Proxy for Django App 404 Not Found - django

I am following this guide to try show this url www.mycompany.com/testblog instead of www.mycompany.com:8000/testblog
These are my current config files setup inside my website.comssl.conf file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName website.com
ServerAlias www.website.com
DocumentRoot /var/www/website.com
Redirect permanent / https://website.com/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
`
<VirtualHost *:443>
ServerAdmin webmaster#localhost
ServerName website.com
ServerAlias www.website.com
DocumentRoot /var/www/website.com
SSLEngine on
SSLCertificateFile /etc/ssl/certs/website.com.cer
SSLCertificateKeyFile /etc/ssl/private/website.com.key
SSLCertificateChainFile /var/www/website.com/SSLCert/SSLIntermediateCertificate.cer
ProxyPreserveHost On
ProxyPass /testblog https://website.com:8000/testblog
ProxyPassReverse /testblog https://website.com:8000/testblog
</VirtualHost>
However, when I run my server and try to access the URL www.mycompany.com/testblog I get a 404 Not Found error

Have you load proxy module
On Centos/RedHat
$> grep -R "mod_proxy" /etc/httpd/conf.modules.d/
.....
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
.....
On Ubuntu
$> a2enmod proxy
$> a2enmod proxy_http

ProxyPass /testblog https://website.com:8000/testblog
ProxyPassReverse /testblog https://website.com:8000/testblog
This is very uncommon to do for two reasons:
Normally you reverse proxy stuff that's not available via public domain name. Often it runs on the same machine or on another machine in the same network. In this case, use the IP instead (127.0.0.1 for the local machine or the IP shown in ip addr command on the other machine)
You're using https: but backend services are normally not SSL protected. Also port 8000 is very uncommon for SSL.
In other words, are you sure you want this and not ProxyPass /testblog http://127.0.0.1:8000/testblog

Related

Apache Reverse Proxy based on subdomain

I am currently trying to set up Apache2 as reverse proxy rewriting based on a subdomain's hostname.
Any request sent to *.accepted-terms.mydomain.tld should be forwarded to a local port using the Host *.mydomain.tld. Simply, the .accepted-terms in the middle should be cut away.
Eg
foo.accepted-terms.mydomain.tld → foo.mydomain.tld:6443
bar.accepted-terms.mydomain.tld → bar.mydomain.tld:6443
<VirtualHost *:443>
SSLEngine on
[...] # SSL Certificates
SSLProxyEngine On
ProxyPreserveHost On
ServerName accepted-terms.mydomain.tld
ServerAlias *.accepted-terms.mydomain.tld
ProxyPass / https://0.0.0.0:6443/
ProxyPassReverse / https://0.0.0.0:6443/
#-------- Here, the fun starts:
SetEnvIf Host ^(.*)accepted-terms\.mydomain\.tld$ new_host=$1
RequestHeader set Host %{new_host}emydomain.tld
#------- Here, the fun should be done.
RequestHeader set Cookie "disclaimer_accepted=true"
</VirtualHost>
Unfortunately, my tries based on SetEncIf and the Host were unsuccessful. Is there any way to realize this?
I found a solution.
<VirtualHost *:443>
# [...] SSL
SSLProxyEngine On
SSLProxyCheckPeerName Off
ServerName accepted-terms.mydomain.tld
ServerAlias *.accepted-terms.mydomain.tld
SetEnvIf Host ^(.*).accepted.onionroot.network$ new_host=$1
RequestHeader set Host %{new_host}e.onionroot.network
RequestHeader set Cookie "disclaimer_accepted=true"
ProxyPreserveHost On
ProxyPass / https://0.0.0.0:6443/ retry=1 acquire=3000 timeout=600 Keepalive=On
ProxyPassReverse / https://0.0.0.0:6443/
</VirtualHost>
It seems like reordering the configuration solved the issues.

What could be wrong with this virtual host file in Apache?

I am trying to route an application to a sub route on an internal server, using Gunicorn with my Django app. My virtual host file looks like this:
LoadModule proxy_module /usr/lib64/apache2/mod_proxy.so
LoadModule proxy_http_module /usr/lib64/apache2/mod_proxy_http.so
<VirtualHost *:80>
ServerName 172.16.1.81
<Location "/mycustomapp">
ProxyPreserveHost On
ProxyPass http://127.0.0.1:9090
ProxyPassReverse http://127.0.0.1:9090
</Location>
</VirtualHost>
When I navigate to 172.16.1.81/mycustomapp , I keep getting a 404 not found error when trying to navigate to the application on that route. Is there something else I am doing wrong here?
Okay, I figured it out. For anyone running into this type of problem in the future, the solution lies in using the ServerPath directive inside your VirtualHost configuration. So for example, if you wanted to have an application be served at 172.15.1.20/app1 and another application served at 172.15.1.20/app2 (via port forwarding to a process listening on a port) the virtual host configuration would like the following:
LoadModule proxy_module /usr/lib64/apache2/mod_proxy.so
LoadModule proxy_http_module /usr/lib64/apache2/mod_proxy_http.so
<VirtualHost *:80>
ServerName 172.15.1.20
ProxyPreserveHost On
ProxyPass /app1 http://127.0.0.1:9090
ProxyPassReverse /app1 http://127.0.0.1:9090
ProxyPass /app2 http://127.0.0.1:9080
ProxyPassReverse /app2 http://127.0.0.1:9080
</VirtualHost>

Setting-up localhost to other desired name on WAMP 2.4.9 server gets 404 Error

I want to rename my localhost server to other name, however I have encountered a 404 error. I have followed the steps how to set this up, But still I get error. I wonder what's wrong. I will provide the changes done.
c:\Windows\System32\Drivers\Etc\hosts.file
127.0.0.1 localhost
127.0.0.1 bluescript.com.ph
::89 localhost
::89 bluescript.com.ph
c:\wamp\bin\apache\apache2.4.9\conf\extra\httpd-vhost.conf
I added the third host on the file
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "c:/Apache24/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "c:/Apache24/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerName bluescript.com.ph
DocumentRoot "c:/wamp/www/bluescript/"
<Directory "c:/wamp/www/bluescript/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
Under httpd.conf i did changed my port to 8989, since skype will use port 80
ServerName localhost:8989
#Listen 12.34.56.78:8989
Listen 0.0.0.0:8989
Listen [::0]:8989
I've already restarted WAMP and re-open the web browser and type url: http://bluescript.com.ph and get error: HTTP Error 404. The requested resource is not found. Where did i go wrong?
1) Remove these dummy (Example) Virtual Hosts that point to non existing folders
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host.example.com
DocumentRoot "c:/Apache24/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error.log"
CustomLog "logs/dummy-host.example.com-access.log" common
</VirtualHost>
<VirtualHost *:80>
ServerAdmin webmaster#dummy-host2.example.com
DocumentRoot "c:/Apache24/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error.log"
CustomLog "logs/dummy-host2.example.com-access.log" common
</VirtualHost>
2)
If you must use a Non-Standard port number for Apache then that port number needs to be on the Virtual Host as well.
You should also add a Virtual Hosts for localhost
<VirtualHost *:8989>
ServerName localhost
DocumentRoot c:/wamp/www
<Directory "c:/wamp/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
<VirtualHost *:8989>
ServerName bluescript.com.ph
DocumentRoot "c:/wamp/www/bluescript/"
<Directory "c:/wamp/www/bluescript/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
Also remember to remove the comment from httpd.conf to activate the httpd-vhosts.conf file
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
Now restart Apache
And of course this means you must use the Non-Standard port number on all your urls. Like
http://bluescript.com.ph:8989
3)
Also this is wrong in your hosts file! Port number are not used in this file, so
::89 localhost
::89 bluescript.com.ph
Should be
::1 localhost
::1 bluescript.com.ph
After this change either reboot, or from a command window launched "As Adminitrator" do
net stop dnscache
net start dnscache

AWS Apache2 reverse proxy with godaddy not working

I have purchased a godaddy domain and AWS EC2 instance with elastic ip. In godaddy I created following entries
A-name entry ( mydomain.com ) pointing to elastic ip
And a C-name entry (app2.mydomain.com) pointing to same elastic ip
Inside amazon EC2 instance , I am running two applications
app1 - running on localhost:3000
app2 - running on localhost:4000
Now, I am trying to achieve below
mydomain.com should point to app1 running on localhost:3000
app2.mydomain.com should point to app2 running on localhost:4000
I have installed apache2 on EC2 and followed below links to configure reverse proxy
link1
I created myproxy.conf under apache2/sites-available as below
<VirtualHost mydomain.com:80>
ServerName mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
<VirtualHost app2.mydomain.com:80>
ServerName app2.mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:4000/
ProxyPassReverse / http://localhost:4000/
</VirtualHost>
But I can't get it working. But when I change the conf as below
<VirtualHost *:80>
ServerName app2.mydomain.com
ServerAdmin admin#mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:4000/
ProxyPassReverse / http://localhost:4000/
</VirtualHost>
Both mydoamin.com and app2.mydomain.com launch same app2 application.
I could not figure out what I am missing.
Edit
Soon after I post this, I tried something which seems to work. I added *.80 instead of app2.mydomain.com
<VirtualHost *:80>
ServerName app2.mydomain.com
ServerAdmin admin#mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:4000/
ProxyPassReverse / http://localhost:4000/
</VirtualHost>
<VirtualHost *:80>
ServerName mydomain.com
ServerAdmin admin#mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
Currently you are trying to run code on apache2 default port 80.
You need to run both application on diff port.Try running code on different port
<VirtualHost *:8081>
Listen 8081
ServerName mydomain.com
ServerAdmin admin#mydomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:3000/
</VirtualHost>
Don't forget to open port in listen mode by LISTEN port_no

How do I properly handle multiple VirtualHosts on one server with only one having SSL?

I have the following apache2 VirtualHost config:
<VirtualHost {my_server_ip}:443>
ServerName securesite.com
ServerAlias www.securesite.com
DocumentRoot /data/web/securesite.com/
Options -Indexes
SSLEngine On
SSLCertificateFile /etc/ssl/securesite.com/securesite.com.crt
SSLCertificateKeyFile /etc/ssl/securesite.com/server.key
SSLCertificateChainFile /etc/ssl/securesite.com/gd_bundle.crt
</VirtualHost>
<VirtualHost *:80>
DocumentRoot /data/web/unsecuresite.com/
ServerName unsecuresite.com
ServerAlias www.unsecuresite.com
</VirtualHost>
The problem is that I can access https://unsecuresite.com/ and the server returns the data for securesite.com and the browser complains.
Why does this happen? Why does ServerName for 443 matches other server names?
Can I somehow handle this? Maybe catch the request, handle it with regex and redirect it to its proper place?
EDIT
ports.conf says:
NameVirtualHost *:80
Listen 80
<IfModule mod_ssl.c>
NameVirtualHost {my_server_ip}:443
Listen 443
</IfModule>
When you connect to https://securesite.com/, then you are actually connecting to port 443. Since there is only one site listening on port 443, the first match is returned (even though the ServerName does not match.
Your configuration basically boils down to:
<VirtualHost {my_server_ip}:443>
ServerName securesite.com
# ...
</VirtualHost>
<VirtualHost *:80>
ServerName unsecuresite.com
# ...
</VirtualHost>
This means that any request to port 80 (http) will be served by unsecuresite.com and requests to port 443 (https) are served by securesite.com.
Even if you add a <VirtualHost *:443> ServerName unsecuresite.com, you can still not simply trick the user to redirect without having a valid certificate for unsecuresite.com. (That would result in a ugly certificate warning.)
If your unsecuresite.com vhost is not supposed to handle HTTPS, then just ignore it. With nginx you could reset a connection if the hostname does not match, I don't know if something similar exists for Apache.
I solved it with the following method:
<VirtualHost {my_server_ip}:443>
ServerName securesite.com
ServerAlias www.securesite.com
DocumentRoot /data/web/securesite.com/
Options -Indexes
SSLEngine On
SSLCertificateFile /etc/ssl/securesite.com/securesite.com.crt
SSLCertificateKeyFile /etc/ssl/securesite.com/server.key
SSLCertificateChainFile /etc/ssl/securesite.com/gd_bundle.crt
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?(securesite\.com){1}$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1 [R=301]
</VirtualHost>