Python Flask not forwarding files to httpd without ProxyPass / - flask

I have apache httpd configured as a proxy for flask. I have gotten flask to pass the static files through to httpd but only when proxying the root dir "/" like below
Flask:
def web():
return render_template('requestor/landingReq.html')
httpd.conf:
ProxyPass / balancer://LBFDMCLUS/ stickysession=JSESSIONID
ProxyPassReverse / balancer://LBFDMCLUS/ stickysession=JSESSIONID
ProxyPass /Web balancer://LBFDMCLUS/Web stickysession=JSESSIONID
ProxyPassReverse /Web balancer://LBFDMCLUS/Web stickysession=JSESSIONID
RequestHeader set X-Forwarded-Proto http
RequestHeader set X-Forwarded-Prefix /
While this works it does not work in my instance as I need to proxy / to a different server. However if I try to delete the proxy to / and just leave the /Web proxy I no longer get my static files forwarded.

Related

HOST_NAME is 127.0.0.1 (gunicorn via Apache)

I managed to get gunicorn running behind Apache:
<Location /foo/>
ProxyPass unix:/run/gunicorn-foo.sock|http://127.0.0.1/
ProxyPassReverse unix:/run/gunicorn-foo.sock|http://127.0.0.1/
</Location>
Everything works, except ALLOWED_HOSTS checking. The HTTP_HOST is always 127.0.0.1
How to pass the HTTP_HOST to gunicorn?
Apache/2.4.46 (Debian)
BTW: I would prefer Nginx, but that's not possible in this case.
<Location /foo/>
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
ProxyPass unix:/run/gunicorn-foo.sock|http://127.0.0.1/
ProxyPassReverse unix:/run/gunicorn-foo.sock|http://127.0.0.1/
</Location>

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.

Apache config with flask, wsgi, and reverse proxy

Below is my Apache config. What I am trying to do have a reverse proxy set up for the main part of the site. So when users go to mysite.xyz, they get what is running on port 5000 (this is working). I also want to run a flask app on mysite.xyz/page1 This is coming back with a 404 not found page. When I switch them and put the app at / and put the main site at page1, then the app works but page1 can't find any of the correct js assets so I'd like to avoid that. But the app does work and works on apache but the reverse proxy must be over-riding the WSGI deployment of the flask app or something?
<VirtualHost *:80>
ServerName mysite.xyz
ServerAdmin email#gmail.com
ProxyPass / http://127.0.0.1:5000/
ProxyPassReverse / http://127.0.0.1:5000/
RewriteCond %{HTTP_HOST} !^mysite\.xyz$ [NC]
RewriteRule ^/$ http://%{HTTP_HOST}/ [L,R=301]
WSGIScriptAlias /page1 /var/www/FlaskApp/FlaskApp.wsgi
<Directory /var/www/FlaskApp/>
Order allow,deny
Allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Instead of ProxyPass, try:
RewriteRule ^/$ http://127.0.0.1:5000/ [P]
ProxyPassReverse / http://127.0.0.1:5000/
I know this is stale but it may help someone. Flask hates reverse proxy. You need to add flask-reverse-proxy-fix to your app.
https://pypi.org/project/flask-reverse-proxy-fix/

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

Django Rest Framework Reverse Relationship Breaks When Behind Proxy

I am trying to serve a django webapp which uses gunicorn with httpd acting as a proxy which also serves static content. Everything works fine, except that the django rest framework searchable API root (the entry point for the app's API) does not provide the correct url. The url is for localhost and the port served by gunicorn rather than the ipaddress of the machine or or its host name. Here's httpd conf file for the app:
<VirtualHost *:80>
DocumentRoot /opt/example
ProxyPass /static/ !
Alias /static/ /var/www/html/static/
ProxyPass / http://localhost:8000/
ProxyPassReverse / http://localhost:8000/
</VirtualHost>
Gunicorn serves on localhost on port 8000.
Here's the view for the api:
#api_view(('GET',))
def api_root(request, format=None):
return Response({
'activity': reverse('activity-list', request=request, format=format)
'test' : reverse('test-list', request=request, format=format)
})
When hitting the api root page this is the response I get:
HTTP 200 OK
Content-Type: application/json
Vary: Accept
Allow: GET, HEAD, OPTIONS
{
"activity": "http://localhost:8000/activity/",
"tests": "http://localhost:8000/test/",
}
I have a similar setup and it seems to work just fine.
Make sure you have USE_X_FORWARDED_HOST = True in your settings.py as it defaults to False
More info here
In your httpd.conf there is a typo httpd.
The line should be:
ProxyPassReverse / http://localhost:8000/