enable jquery load cross domain with a proxy - proxy

I have two resources. A standard apache website on mycooldomain.com and a node website on mycooldomain.com:4337. On the apache vhost I have defined a proxy so that i can reach node using mycooldomain.com/myapp.
Now what I am trying to do is to have this code working in mycooldomain.com/index.html:
$('#somediv').load('/myapp');
but this will end with the browser blocking the request marking it as "insecure".
I assume that the proxy should be the workaround to put the two requests on the same domain but as far as I can see I am missing something.
My proxy definition in apache is the following:
ProxyPreserveHost On
SSLProxyEngine On
ProxyPass /myapp https://mycooldomain.com:4337
ProxyPassReverse /myapp https://mycooldomain.com:4337
What am I missing?
I also tried to add to this:
Header always add Strict-Transpor-Security "max-age=15552000; include subdomains"
the following line:
Header set Access-Control-Allow-Origin "https://mycooldomain.com:4337"
but this didn't solve the issue either.
Any hint?

Related

Reverse proxy for dynamic URL in apache2 server

I want to do reverse proxy for dynamic URLs. Below is my case
abc.xyz.com/p1/#/p2?data=value
to
xyz.com/p1/#/p2?data=value
I have achieved reverse proxy for static path using <Location ''> but not able to figure out how to do it for dynamic URLS
If i'm not mistaken, it doesn't matter what kind of URLs you have.
Could you send your Apache2 vHost Configuration, so I can analyse this further.
My approach would be
[...]
ServerName xyz.local
ProxyPass /p1/ http://abc.xyz.local/p1/
ProxyPassReverse /p1/ http://abc.xyz.local/p1/
[...]
We probably would need more infos. Is the /p1/ Path "dynamic"?
I don't think that I could help you with this. But I'll do my best, if you provide more information :)

Spring boot static content works fine locally but not when I push to VPS server

I have a spring boot that runs fine locally and I have no problems accessing: http://localhost:8080/staticlayouts/blah.html. Whenever I upload to my VPS running Cpanel/WHM i get the following when I try to access http://www.example.com/staticlayouts/blah.html where example.com is my website. I Have no issues accessing my main page, other than static content is not loading. Any ideas what I need to change in WHM/Cpanel configuration to get the static content of spring boot to stop getting the following error:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /staticlayouts/blah.html.
Reason: DNS lookup failure for: localhost:8080staticlayouts
Additionally, a 502 Bad Gateway error was encountered while trying to use an ErrorDocument to handle the request.
After hours of wasting my time trying worthless things, the problem was because of my apache httpd configuration. In my vhost.config file I had this:
ProxyPreserveHost on
ProxyPass / http://localhost:8080
ProxyPassReverse / http://localhost:8080
I needed this (notice the slashes on the urls):
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
This url is a great reference: https://medium.com/#codebyamir/using-apache-as-a-reverse-proxy-for-spring-boot-embedded-tomcat-f704da73e7c8

How to enable CORS on Sonatype Nexus?

I want to develop a Monitoring-WebApp for different things with AngularJS as Frontend. One of the core-elements is showing an overview of Nexus-Artifacts/Repositories.
When I request the REST-API I'm getting following error back:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost:9090' is therefore not allowed access.
To fix this error, I need to modify the response headers to enable CORS.
It would be great if anyone is familiar with that type of problem and could give me an answer!
The CORS headers are present in the response of the system you are trying to invoke. (Those are checked on the client side [aka browser this case], you can implement a call on your backend to have those calls and there you can ignore those headers, but that could become quite hard to maintain.) To change those you'll need a proxy. So your application will not call the url directly like
fetch("http://localhost:9090/api/sometest")
There are at least two ways: one to add a proxy directly before the sonar server and modify the headers for everyone. I do not really recommend this because of security reasons. :)
The other more maintaneable solution is to go through the local domain of the monitoring web app as follows:
fetch("/proxy/nexus/api/sometest")
To achieve this you need to setup a proxy where your application is running. This could map the different services which you depend on, and modify the headers if necessary.
I do not know which application http server are you going to use, but here are some proxy configuration documentations on the topic:
For Apache HTTPD mod_proxy you could use a configuration similar to this:
ProxyPass "/proxy/nexus/" "http://localhost:9090/"
ProxyPassReverse "/proxy/nexus/" "http://localhost:9090/"
It is maybe necessary to use the cookies as well so you may need to take a look at the following configurations:
ProxyPassReverseCookiePath
ProxyPassReverseCookieDomain
For Nginx location you could employ something as follows
location /proxy/nexus/ {
proxy_pass http://localhost:9090/;
}
For node.js see documentation: https://github.com/nodejitsu/node-http-proxy
module.exports = (req, res, next) => {
proxy.web(req, res, {
target: 'http://localhost:4003/',
buffer: streamify(req.rawBody)
}, next);
};

Apache and Nodejs cross domain ajax issue

I am working on application in which I have used two servers hosted on same machine, one is apache which will work as basic host for serving php pages and other side nodejs for communication of rest api, whole application build upon backbone/marionette/requirejs/bootstrap.
Coming to the point, my normal pages are load from apache server like,
http://192.168.20.62/project/design.php
and I have configured my model like this,
define(['backbone'],function(Backbone){
'use strict';
return Backbone.Model.extend({
url:"http://192.168.20.62:9847/page",
defaults: {
...
}
});
});
when I try to save model I am suffering from problem of ajax cross domain call and I am ended up with error in my save communication, following is the node/express server,
var express = require('/root/node_modules/express');
var app = express();
app.configure(function () {
app.use(express.json());
app.use(express.urlencoded());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://192.168.20.62:9847');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
})
app.post('/page', function(request, response){
console.log(request.body);
response.send(request.body);
});
app.listen(9847);
as you can see I have already written some patch in server code, but still the same, also I have added .htaccess at root level of both at
http://192.168.20.62
and
http://192.168.20.62:9847
with the following code,
Header add Access-Control-Allow-Origin "*"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
but things are not helping in anyways, if I run chrome by disabling web security then thing are working properly.
chrome.exe --disable-web-security
Can you guys please help me to solve this puzzle, thanks in advance.
following is the error message from chrome javascript console
OPTIONS http://192.168.20.62:9847/page Origin http://192.168.20.62 is not allowed by Access-Control-Allow-Origin. jquery-2.0.3.min.js:6
XMLHttpRequest cannot load http://192.168.20.62:9847/page. Origin http://192.168.20.62 is not allowed by Access-Control-Allow-Origin.
Oops I figure out the problem, I have setup wrong url for white listing.
res.setHeader('Access-Control-Allow-Origin', 'http://192.168.20.62');
I have to whitelist the source URL which was generating cross domain, this was the only change have to do to make thing running.
Also this has browser version impact, when I have posted this issue, I was checking with firefox version 24.0.* and when I upgrade 25.0 it has stopped generating cross domain error surprisingly. But still Chrome giving natural cross domain error, when I read error message of chrome carefully I come to know that I have whitelisted wrong url.
XMLHttpRequest cannot load http://192.168.20.62:9847/page. The 'Access-Control-Allow-Origin' whitelists only 'http://192.168.20.62:9847'. Origin 'http://192.168.20.62' is not in the list, and is therefore not allowed access.
If you don't have to deal with WebSockets, then place Node.js behind Apache via mod_proxy:
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_http_module modules/mod_proxy_http.so
LoadModule rewrite_module modules/mod_rewrite.so
# disable use as forward proxy
ProxyRequests Off
# don't handle Via: headers - we don't care about them
ProxyVia Off
# no need to transport host name - not doing virtual hosting
ProxyPreserveHost Off
ProxyPass /page/ http://192.168.20.62:9847/page/
ProxyPassReverse /page/ http://192.168.20.62:9847/page/
# If you get HTTP status code 502 (Bad Gateway), maybe this could help
SetEnv force-proxy-request-1.0 1
SetEnv proxy-nokeepalive 1
In your web-page you can access your API with the same port as your Apache and you don't have any problems with Cross-Domain / Same-Origin-Policy.
You just have to separate your URIs now, e.g /page/ exists only in node.js app.
If you're serving REST only (no HTML/CSS/Images) via node.js, using JSONP would be another option. Here is a good+short description how to handle JSONP in Express

Is this a Sinatra config issue, or an Apache config issue?

I haven't decided if this is an Apache issue, or a Sinatra issue, basically, we have a bunch of little apps running on a single server, we deploy them with just built-in webrick instances and use apache to re-route those requests by subdomain to the right port. I am using gollum which is a sinatra app with a git persistence layer, but I am getting an unexpected app routing when it redirects (after edit action). I'm not sure if I can fix this by passing a startup option to Sinatra, or by configuring in the virtual host declaration for the app a rewrite rule. Please in your answer include which option you think is best and an example of how it might be accomplished.
Thanks,
# apache virtualhost declaration
<VirtualHost *:80>
ServerName wiki.domain.com
DocumentRoot "/var/www/html"
ProxyPass / http://localhost:3006
ProxyPassReverse / http://localhost:3006
</VirtualHost>
resolves with sinatra fine for GETs, eg
wiki.domain.com/Home
but fails on sinatra redirect
# expected
wiki.domain.com/Home
# actual
wiki.domain.com:3006/Home
and here is the Sinatra action (source: https://github.com/github/gollum/blob/master/lib/gollum/frontend/app.rb )
post '/edit/*' do
name = params[:splat].first
wiki = Gollum::Wiki.new(settings.gollum_path)
page = wiki.page(name)
format = params[:format].intern
name = params[:rename] if params[:rename]
wiki.update_page(page, name, format, params[:content], commit_message)
redirect "/#{CGI.escape(Gollum::Page.cname(name))}"
end
It’s a long shot, but perhaps the URLs in the ProxyPass directives need to have a trailing /? That’s how it is in the documentation, and I got some strange entries in error.log without them.

Resources