Reverse Proxying Google Cloud Storage public URL fails / 404s - google-cloud-platform

I'm trying to load some assets from Google Cloud Storage (GCS) onto a website, but I want to use a proxy so if I visit mysite.com/page/asset.html I want it to load the content from http://storage.googleapis.com/my-bucket/directory/asset.html without redirecting.
In my apache configs, I am utilizing mod_proxy
ProxyPass "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"
ProxyPassReverse "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"
If I visit /page/asset.html I get a 404 NoSuchBucketThe specified bucket does not exist.
I'm using HTTP as its just the development environment - I tested this by replacing the GCS URL with a URL from another development environment and that worked. I am thinking that there is some config that I've missed out for proxying GCS.
Note that the GCS URLs are public and can be visited without authentication.

For anyone else that comes across this issue
The problem is that I had ProxyPreserveHost On as it was serving some of my other proxies, but to access GCS we want to switch that off.
Now I can access the assets fine.
So my code looks like:
ProxyRequests Off
ProxyPreserveHost On
ProxyPass /first/proxy https://external.com/fproxy/
ProxyPassReverse /first/proxy https://external.com/fproxy/
ProxyPreserveHost Off
ProxyPass "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"
ProxyPassReverse "/page/asset.html" "http://storage.googleapis.com/my-bucket/directory/asset.html"

Related

Redirect http to https using Application Load Balancer

I am using an application load balancer to map certain paths to one server (Apache) and other paths to another server (Tomcat).
I made all pages on my site available via https by setting up an https listener on the load balancer.
So that requests from client to load balancer are encrypted but from load balancer to servers are not.
Now, I would also like to redirect all http requests to https.
Are there any suggestions how I can do this?
I can redirect each server separately (ie: redirect tomcat http requests as outlined here and redirect Apache http request with redirect rules). However, I was wondering if there is a simpler way to do it (ie: where I would only have 1 redirect rather than a separate redirect for each server).
Thanks.
I found this while I was looking for a solution for the same problem. This has code sample for Apache, Nginx and IIS.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule .* https://%{HTTP:Host}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
https://aws.amazon.com/premiumsupport/knowledge-center/redirect-http-https-elb/
You have to configure the following to conf/server.xml
<Connector
port="8080"
protocol="HTTP/1.1"
scheme="https"
secure="true"
connectionTimeout="20000"
URIEncoding="UTF-8"
redirectPort="8443" />
Please ensure scheme="https" is added so that no http request is being made.
Along with above add the default HSTS filters available in the conf/web.xml as defined in the tomcat documentation.
Please refer here for more info: Tomcat behind LB

WSGIPassAuthorization On is not working

I am trying to serve a django web application on apache2 server with mod_wsgi. My application has oauth2 authentication system. I send my credentials to server and get my token without any problem. But after that I send:
Authorization: Bearer HERE_TOKEN
but unfortunately I cannot get the data. I searched on the internet and find a solution. In apache's config file -named httpd.conf- there should have been:
WSGIPassAuthorization On
I added it. But nothing changed. When I check my client app, it shows the requested headers. There is no Authorization header.. How can I send the Authorization header to server ?
Connect to your Apache, go to etc/httpd/conf.d folder and open wsgi.conf file. Insert the code within VirtualHost DOM:
WSGIPassAuthorization On
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
Restart the server.
If anyone is tearing their hair out on this like I was, make sure if you have ssl configured on your site and a separate apache config file for *:443, you have it there as well.

Forcing www on all URLs without htaccess

I have a complex setup wherein am having Django 3 sub-projects kind of thing hosted on single Apache via mod wsgi.
I want to have www prepended on all addresses for one of these as some payment gateways are behaving improper without www URLs. Now, .htaccess's canonical redirect messes up the other 2. Using PREPEND_WWW from common middleware works only for foo.jp and not for foo.jp/bar/* and so on...
Similarly, I also tried to create an index.php, pointed to it from sites-enabled in apache and used header redirection to www.foo.jp for foo.jp...
Please tell me a way for all URLs i.e. anything foo.jp/, foo.jp/bar/ etc. to www.foo.jp/, www.foo.jp/bar/ and so on...
Regards !
I figured this out...
In the apache/sites-enabled/000-default
Basically, the default apache virtual host file which is read before other domain's virtual host files I added this:
<VirtualHost *:80>
ServerName playism.jp
Redirect permanent / http://www.foo.jp/
</VirtualHost>
Now, it is redirecting for everything... without the .htaccess which was messing it up !

using regex for proxypass

I'm trying to set up a proxy to my corporate environment's ActiveMQ admin pages on our standard http proxy server. I have a rule in place that allows me access to the admin landing page:
ProxyPass /foobar hostname:8161/admin
ProxyPassReverse /foobar hostname:8161/admin
However, going to the "queues" page bring me to a different page appended with a unique session ID (admin/queues.jsp;jsessionid=oq37zgvxz4zkwliwdwddyon3), and I would like for this page to be accessible through the same URL as well.
Attempting to add a wildcard (*) at the end of these proxypass rules breaks the redirect. Is there a redirect rule that I need to use in conjunction with this, and how do I get any string to this host to pass through this proxy rule?
There is ProxyPassMatch that is regex based, but ProxyPass does prefix matching (i.e. a ProxyPass for /foo should catch /foo/bar too) so your current rule should work anyway.
It may be that the redirection URL doesn't match the reverse rule for some reason. I have to admit I've never managed to get a ProxyPass to Tomcat to work correctly when the front and back end context paths differ, so my advice would be to deploy the back end app at hostname:8161/foobar instead of /admin if that's an option.

Hosting a wordpress blog parallel with a django app

I would like to host a blog at a subfolder of my domain, which is covered by a django app. I'm most of the way there, but I need some help getting over the finish line, as it were.
Currently, if I go to domain.com, the django app is served correctly. If I go to domain.com/blog/, the blog is served correctly. However, if I go to domain.com/blog (note the missing trailing slash), the urlconf returns a URL not found error.
I've tried a couple of things, including:
Reordering the Alias, Directory, and WSGIScriptAlias statements in my Apache configuration
Having the django urlconf trap the domain.com/blog condition and redirect to domain.com/blog/ (probably unsurprisingly causing an infinite loop of redirects)
What are my next steps?
Here is the relevant part of my Apache conf:
Alias /blog/ /var/www/blog/
<Directory /var/www/blog/>
AllowOverride All
Order deny,allow
Allow from all
</Directory>
I haven't used Apache in years, but try aliasing just /blog instead of /blog/. The problem currently is that Apache is not catching it, so it's being passed to Django. If that doesn't work, you might also try setting up a 301 redirect in your Apache conf to redirect to the slash version, thereby avoiding Django altogether.