Apache config with flask, wsgi, and reverse proxy - flask

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/

Related

Vue3 router can not refresh on production

I have Vue3 application with router. History mode is enabled so I would expect that I will be able to write vue.domain.com/path/to/view to the browser address bar and go to the required page. But as I see router create urls like expected but I can not refresh the page or go directly to another location via address bar. You can see an example here with login page https://vue.tatrytec.eu/ Try refresh and you will see 404 not found error. The whole repository is on Github.
Can somebody tell me please what is the solution of this problem?
EDIT
I found solution but I am not sure if this Apache settings are correct. This is the file from apache2/sites-available
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin xxx#xxxx.xx
ServerName vue.tatrytec.eu
ErrorLog ${APACHE_LOG_DIR}/www/vue.tatrytec.eu/error.log
CustomLog ${APACHE_LOG_DIR}/www/vue.tatrytec.eu/access.log combined
DocumentRoot /var/www/vue.tatrytec.eu/dist
<Directory "/var/www/vue.tatrytec.eu/dist">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Require all granted
RewriteEngine on
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</Directory>
SSLCertificateFile /etc/letsencrypt/live/vue.tatrytec.eu/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/vue.tatrytec.eu/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
You have to configure your Apache web server so that it routes all requests to your index.html. See example.

Amazon Linux httpd redirecting to www not working

I am actually trying to do 2 things:
1. convert site port from 80 to 3000
2. redirect site from example.com to www.example.com
My machine is amazon linux, so using http
so in httpd.conf file, below is what I've added:
<VirtualHost *:80>
ServerAdmin admin#example.com
ServerName example.com
ProxyPreserveHost On
RewriteCond "%{HTTP_HOST}" "!^www\." [NC]
RewriteCond "%{HTTP_HOST}" "!^$"
RewriteRule "^/?(.*)" "http://www.%{HTTP_HOST}/$1" [L,R,NE]
# setup the proxy
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
In above, port changing is working, but redirecting to www not working, could you please suggest me what changes I need to make, to make it to work in above.

Apache / Gunicorn and Django Issue

I have an application that runs using
client --> apache --> gunicorn --> wsgi --> django
When I run my code it starts to run and then I receive the following within the apache logs,
(20014)Internal error: proxy: error reading status line from remote server 127.0.0.1
proxy: Error reading from remote server returned by /app/view
No exceptions show in the code. However if I run the code without the gunicord/proxy layer it works. Also this only affects some requests.
Heres is my apache conf.d file.
<VirtualHost 10.0.6.1:443>
ServerName app.domain.net
Alias /static /production/pythonenv/app/lib/python2.7/site-packages/rest_framework/static/
ErrorLog /var/log/httpd/app-error.log
CustomLog /var/log/httpd/app-access.log common
LogLevel warn
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.*) http://127.0.0.1:9008/$1 [P]
ProxyPassReverse / http://127.0.0.1:9008/
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
RequestHeader unset Expect early
ErrorLog logs/app-ssl-error.log
CustomLog logs/app-ssl-access.log common
SSLEngine on
SSLCertificateFile /opt/ssl/crt/cert.crt
SSLCertificateKeyFile /opt/ssl/crt/key.key
</VirtualHost>

Redirection of domain within <VirtualHost> tag

I have the following in my apache2.conf file for a django project:
#ServerRoot "/etc/apache2"
<VirtualHost *:80>
Alias /static /home/ubuntu/Avails/static
# virtual host configuration
WSGIScriptAlias / home/ubuntu/Avails/wsgi.py
</VirtualHost>
Similar to a previous question I asked here (How to redirect one domain to another).
I have two domains:
www.new.example1.com/ --> IP=1.2.3.4
www.new.example2.com/ --> IP=1.2.3.4 (same IP for both domains)
I want all requests that go to www.example2.com/* to access the requested page. I want all requests that go to www.example1.com/* to redirect to www.example2.com/*. how would I redirect a domain within the VirtualHost block, instead of using the .htaccess file?
Here is what worked. Special thanks to anubhava for the help:
1) Enable module rewrite:
$ sudo a2enmod rewrite
2) Edit apache2.conf file:
<VirtualHost *:80>
Alias /static /home/ubuntu/Avails/static
# virtual host configuration
WSGIScriptAlias / home/ubuntu/Avails/wsgi.py
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new\.example1\.net [NC]
RewriteRule ^ http://new.example2.net%{REQUEST_URI} [NE,R=301,L]
</VirtualHost>
3) Restart apache:
$ sudo service apache2 restart

Django Based framework virtual host setup https on Apache

I am using a django based framework and have successfully figured Apache settings for http mode. Basically I have done the setting correctly on <VirtualHost *:80> ... </VirtualHost> and when I do, http://mysite.domain.com I get routed correctly to my site and the site pages and the skins get render correctly.
I have setup https://mysite.domain.com to work with shibboleth, shibboleth is working and when use the https I get routed to login credential page via shibboleth server, and after successful login I get redirect to https://mysite.domain.com but site doesn't get rendered correctly and skins don't show up as same as http://mysite.domain.com.
Here is my Apache settings, I am trying to understand what I am doing wrong here
<VirtualHost *:443>
ServerAdmin myname#mydomain.com
DocumentRoot /code/vEnviornment/mysite
ServerName mydomain.com
#<LocationMatch "^(?!/admin)">
#<LocationMatch "^(?!/m)">
# RewriteEngine on
# RewriteRule django.wsgi(.*)$ https://mydomain.com:443$1 [L,R=301]
#</LocationMatch>
SSLEngine on
#your SSL keys
#I have removed this wasn't comfortable putting SSL key info
#Alias /admin/media/ /usr/local/lib/python2.6/site-packages/django/contrib/admin/media/
Alias /admin/media/ /usr/local/lib/python2.7/dist-packages/django/contrib/admin/media/
WSGIScriptAlias /m/ /code/vEnviornment/mysite/django.wsgi
<Directory "/">
AuthType shibboleth
ShibRequestSetting requireSession 1
Require valid-user
</Directory>
Alias /Shibboleth.sso /tmp
# CustomLog /var/log/httpd/mysite/access_log common
# ErrorLog /var/log/httpd/mysite/error_log
CustomLog /var/log/apache2/mysite/access_log common
ErrorLog /var/log/apache2/mysite/error_log
</VirtualHost>
And here is how I have hetup http:
<VirtualHost *:80>
ServerAdmin myname#mydomain.com
DocumentRoot /code/vEnviornment/mysite
ServerName mysite.mydomain.com
#aliases to serve static media directly
#will probably need adjustment
Alias /m/ /code/vEnviornment/mysite/static/
Alias /upfiles/ /code/vEnviornment/mysite/myframework/upfiles/
<DirectoryMatch "/code/vEnviornment/mysite/myframework/skins/([^/]+)/media">
Order deny,allow
Allow from all
</DirectoryMatch>
<Directory "/code/vEnviornment/mysite/myframework/upfiles">
Order deny,allow
Allow from all
</Directory>
#must be a distinct name within your apache configuration
WSGIDaemonProcess mysite2
WSGIProcessGroup mysite2
WSGIScriptAlias / /code/vEnviornment/mysite/django.wsgi
#make all admin stuff except media go through secure connection
<LocationMatch "/admin(?!/media)">
RewriteEngine on
RewriteRule /admin(.*)$ https://128.101.35.71/admin$1 [L,R=301]
</LocationMatch>
# CustomLog /var/log/httpd/mysite/access_log common
# ErrorLog /var/log/httpd/mysite/error_log
CustomLog /var/log/apache2/mysite/access_log common
ErrorLog /var/log/apache2/mysite/error_log
LogLevel debug
</VirtualHost>
What am I doing wrong here to render the site incorrectly via https?
Alias /m/ /code/vEnviornment/mysite/static/
Alias /upfiles/ /code/vEnviornment/mysite/myframework/upfiles/
These two lines are missing in https virual host
and
your WSGIScriptAlias should point to / not /m/