I want to have a haproxy in front of some iis servers.
My problem is that on the iis servers I have three applications.
app1.domain.com
app2.domain.com
app3.domain.com
On the frontend. I do not want to show the sub domains.
So I want do write:
www.domain.com/app1
www.domain.com/app2
www.domain.com/app3
And on the backend I want to change both the host and url to match each application.
I have an idea how to rewrite all this, but that will require to duplicate all real servers in multiple backed parts on the haproxy.
So my question is: Can I rewrite URL and Hosts before choosing a backend?
Or can one backend do multiple rewrites based on some conditions?
If they are different backends why wouldn't you want to create different rules for them? there's always something different to config for your backends even if the applications are quite similar. HAProxy is meant to be lightweight so if you're trying to write variables in your rewrites that is not going to work.
My config would look something like this:
acl domain hdr_dom(host) -i www.domain.com
acl app1 path_beg /app1
acl app2 path_beg /app2
acl app3 path_beg /app3
use_backend backend_app1 if app1 domain
use_backend backend_app2 if app2 domain
use_backend backend_app3 if app3 domain
backend backend_app1
reqrep ^([^\ ]*)\ /app1before \1\ /app1after
server server_app1 app1.domain.com
backend backend_app2
reqrep ^([^\ ]*)\ /app2before \1\ /app2after
server server_app2 app2.domain.com
backend backend_app3
reqrep ^([^\ ]*)\ /app3before \1\ /app3after
server server_app3 app3.domain.com
Related
I have two react frontend servers (example1 and example2) listening on two ports on MyDomain.com:
MyDomain.com:4000 accesses example1
MyDomain.com:5000 accesses example2
I have two subdomains, example1.MyDomain.com example2.MyDomain.com
I want to create a third nodejs server that will listen on port say 3000 and route requests to ports 4000 and 5000 depending on the subdomain accessed.
example1.MyDomain.com will be routed to MyDomain.com:4000
and
example2.MyDomain.com will be routed to MyDomain.com:5000
could not find an example that addresses port numbers except for ngnix
I also tried using proxy middleware but the examples show how to route according to the suffix after the .com and that is not what I need.
Thanks for any help,
Amnon
I highly recommend you to use one of the popular proxies/load balancers for that Nginx, HAProxy, Envoy, Traefik.
If you want to do it specifically with node.js you will need create proxy server on your own,but that is not a good idea because the apps mention above already have that functionality and they are built specifically for that use case
I am wondering if it is okay to serve both, client and api under one domain?
For example: index.html at my-vps.supplier.net and my-vps.supplier.net:3000 for api served by NodeJS?
Is it even okay to expose api in such way?
You can use apache reverse proxy (or another balancing technology) to expose the api on port 80 but on a different domain or on the same domain but under a different path.
For apache Reverse Proxy: https://httpd.apache.org/docs/current/mod/mod_proxy.html
For HAPropxy: http://cbonte.github.io/haproxy-dconv/2.4/intro.html
What I'm trying to do is create an access website for my own services that run on my linux server at home.
The services I'm using are accessible through <my_domain>:<respective_port_num>.
For example there's a plex instance which is listening on port X and transmission-remote (a torrenting client) listening on port Y and another custom processing service on port Z
I've created a simple website using python flask which I can access remotely which redirects paths to ports (so <my_domain>/plex turns into <my_domain>:X), is there a way to display these services on the network paths I've assigned to them so I don't need to open ports for each service? I want to be able to channel an existing service on :X to <my_domain>/plex without having to modify it, I'm sure it's possible.
I have a bit of a hard time to understand your question.
You certainly can use e.g. nginx as a reverse proxy in front of your web application, listen to any port and then redirect it to the upstream application on any port - e.g. your Flask application.
Let's say, my domain is example.com.
I then can configure e.g. nginx to listen on port 80 (and 443 for SSL), and then proxy all requests to e.g. port 8000, where Flask is running locally.
Yes, this is called using nginx as a reverse proxy. It is well documented on the internet and even the official docs. Your nginx.conf would have something like:
location /my/flask/app/ {
# Assuming your flask app is at localhost:8000
proxy_pass http://localhost:8000;
}
From user's perspective, they will be connecting to your.nginx.server.com/my/flask/app/. But behind the scenes nginx will actually forward the request to your app, and serve its response back to the user.
You can deploy nginx as a Docker container, I recommend doing this as it will keep the local files and configs separate from your own work and make it easier for you to fiddle with it as you learn. Keep in mind that nginx is only HTTP though. You can't use it to proxy things like SSH or arbitrary protocols (not without a lot of hassle anyway). If the services generate their own URLs, you might also need to configure them to anticipate the nginx redirects.
BTW, usually flask is not served directly to the internet, but instead nginx talks to something like Gunicorn to handle various network related concerns: https://vsupalov.com/what-is-gunicorn/
Im looking for a way to do the following:
Given two servers running locally on different ports:
localhost:3001
localhost:3002
I would like a third server running on port 3000 to route all traffic to localhost:3001 except for specific whitelisted paths. I would like a configuration file to specify the paths. For example.
* localhost:3001
/example localhost:3002
In this case all traffic is proxying to 3001, except for the route /example which will proxy to localhost:3002/example.
I do not want a redirect off of 3000 for any requests. I would like this intermediary server to appear to the only site. So I believe I would like the server running on port 3000 to be a proxy to the other two.
I am interested in doing this via nginx or a node.js / npm module if available. Is this possible? What is a simple way of doing it?
Nginx should suite perfectly for this job.
Usually it is used as a web tier for application servers located on other machines, but technically there's nothing different there from using the same host with different ports.
you should simply define localhost:3002 and localhost:3001 as the proxy_paths in your nginx configuration.
If you're not familiar with nginx, this can be a good place to start:
https://www.nginx.com/resources/wiki/start/
I have written some code using express.js which I'd like to put on the same HTTP port as a web application implemented with another technology (in this case, Django). I don't want to have to redirect user browsers to another port since if I do they might bookmark URLs with the other port, and then I lose the ability to reconfigure the arrangements later. So, I'd like express.js to serve HTTP on its port, fulfilling some paths I specify by making HTTP requests to a secondary web application which is being served on another port.
Is there any middleware or other technique for express.js which will serve certain paths by making HTTP requests to other servers?
(The stackoverflow question How to make web service calls in Expressjs? is relevant but only discusses GET and I will have some POST requests to forward.)
Whilst it is possible to make POST request with node, I think the pattern you're describing is better suited to using a a server like nginx or apache in front of both node.js and django, and proxying requests to whichever port is appropriate based on the request.
Typically, both django and node.js would listen on whichever ports you want them to listen on, while nginx listens on port 80. You then define a virtual host in nginx that forwards certain requests to node.js and certain requests to django.
Here are the nginx docs on using proxy_pass.
Here is an example, modified from the nginx Full Example:
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
}
# pass requests for dynamic content to django
location /djangostuff {
proxy_pass http://127.0.0.1:8080;
}
# pass requests for node
location /nodestuff {
proxy_pass http://127.0.0.1:8081;
}
}
With node-http-proxy only a single call to app.use is required to reverse proxy all unhandled requests, like this:
var app = express.createServer();
# my app.get bindings
app.use(require('http-proxy').createServer(80, 'other-server-address'));
app.listen(80);