how to redirect apache port 80 to jetty port 8080? - apache

I have a VPS server with a domain example.com, with apache2 listening port 80
Then, I have a web app in jetty server (running with mvn jetty:run), listening in port 8080
When I type example.com in a browser, I get a apache example page "it works!"
If I write example.com:8080 I get my webapp
Question is simple, how can I redirect for when I write example.com so that I get to my webapp?
I cant change port jetty to 80, its error.
command "sudo iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080" does not anything effect
So, how can i make apache redirect 80 to 8080? in sites-enabled/default-000?
Please explain carefully, I'm a newbie with systems ;(
Thank you
EDIT
I solved it with this:
http://czetsuya-tech.blogspot.com.es/2012/07/how-to-port-forward-apaches-80-to.html#.VImt1XvyOPU
But last line:
RewriteRule ^/(.*) ......:8080/0,81 € [P,L]
Makes error at restart apache
I deleted it and works fine. Any problem with this? What is RewriteRule?

Using IPTABLES is one possible solution. I don't know much about IPTABLES - but there are a lot of tutorials out there, like this one: http://proghowto.com/iptables-redirect-port-80-to-port-8080
I don't know if that works if you have a running daemon (apache) on the destination port.
The other solution is to use a proxy module in apache to pass requests through to your webapp. On Linux only root users are allowed to use ports below 1000. On Windows you should be able to simply change the port in the jetty configuration.
There are two tutorials I often use, they apply to apache and tomcat but that makes no difference for the apache side:
https://confluence.atlassian.com/display/DOC/Using+Apache+with+virtual+hosts+and+mod_proxy
https://confluence.atlassian.com/display/STASH/Integrating+Stash+with+Apache+HTTP+Server
This is quite a common setup. Also there is almost no performance impact due to the proxying. Not that a user would notice anything.

Related

Apache not starting alongside Nginx

Have a development server with 2 static ips. Apache runs on one, Nginx on the other. Everything seems fine except after reboot. Nginx starts Apache doesn't. Can someone please tell me where to look outside log files for what the issue could be...or tell me the process on how to make this work..
Thanks in advance...
12/19/2018
Rebuilt the server using only Apache. When binding it to a single ip to one interface it still does not start after boot. When I unbind it, it boots fine. Doesn't seem to be a conflict with Nginx on the other ip, seems to do with the binding of Apache itself...still perplexed...
I went to sudo nano /etc/apache2/ports.conf
Changed Listen 80 to Listen 8080 and I was able to restart apache2 after that with the following command:
sudo systemctl restart apache2
It's probably because default port for both nginx and apache is 80. So for example when nginx starts, the port will be in use, and apache can't access to that port.
in ypur httpd.conf of apache, find the line that says Listen 80 and change that port to whatever you want. then restart apache.
If this is not the issue, then report results of these commands in your post plz:
$ systemctl status httpd // or apache2 instead of httpd
$ systemctl status nginx
$ jourcanctl -xe

redirect/listen to port 8080 if 80 is unreachable, apache to lighttpd

i am running apache and lighty on different ports and want to redirect if apache is down. Do i have to use iptables ?
mod proxy_http is enabled, anything to enable something in lighty?
Are there any suggestions?
edit:
FORWARD chain ?
You should use something like haproxy or lighttpd or nginx in front of Apache, and configure any of them to send the request to other backend hosts/server instances if Apache is down. (Apache has modules for this, too, so you could run Apache in front of Apache.)

block outside access to port so only apache proxy pass serves sites

I ran into a problem I'm not able to fix. I have a server with some applications (a ghost blog and gogs for example) that are listening on specific ports. I want apache to handle them via proxypass. So far so good, I can specify a subdomain and let the requests through to the applications. But all my applications are still reachable via the specific port they run on. I can't let apache listen to this ports because, well, the ports are in use by the applications.
I'm just wondering is there a way to let any apllications just listen on a port and be reachable from locahost (so that only apache can reach them with that port) or is there any other way to limit the access to my applications so that they are only reachable through apache? Is there a solution I can use for all applications or do I have to tweak every single app myself?
Googling it just didn't get me the rights answers (lots of port 80 to https and so on)
Thank you for every answer / tipp / nudge in the right direction you can give me.
Best regards.
Allright, the biggest problem is always in front of the computer :)
I never thought about iptables, I don't know why, because I'm quite familiar with it. For anyone else having the same stupidity problem I had:
Make a rule that allows localhost to access this port:
iptables -A INPUT -p tcp -s localhost --dport 25 -j ACCEPT
Then just block every connection on that port with iptables
iptables -A INPUT -p tcp --dport 25 -j DROP
Don't forget to change 25 to your specific port.
Best regards.

Apache server as a proxy using iptables

I want to accomplish the following: I want to show a task bar on top of all websites I request from my notebook. On this bar I display various information.
I have a Raspberry Pi in my network which I configured as a gateway on my notebook. So all the traffic from my notebook passes the raspberry. On the raspberry I installed an Apache server with two VirtualHosts. One is a local webserver listening on port 80. For the other one, listening on port 8126, I have the following rules:
If no special parameter is set in the request, I redirect the request to a local page (with the original requested URI as a parameter). On this page I have an iframe in which I show the originally requested page, using the URI I read out from the parameters. To avoid having an endless loop I add the special parameter to the link in the iframe. This part works fine.
If the special parameter is not set (since the page should be display in the iframe), I redirect the request to the originally requested page using *mod_rewrite* again. This redirecting seems to cause the problems.
So here is some code of what I am doing:
I redirect all traffic on port 80 to port 8126 using iptables. 192.168.1.1 is the IP address of my raspberry which I use since I can not redirect to the loopback interface in the PREROUTING phase. I do this with the following iptables-rule:
iptables -t nat -A PREROUTING -i eth0 -p tcp 80 -j DNAT 192.168.1.1:8126
I boiled down my problem to the following VirtualHost configuration for the Apache server, where I simply redirect all requests to an external website:
<VirtualHost *:8126>
RewriteEngine On
RewriteRule ^/?(.*) http://example.com/ [P]
ProxyPassReverse / http://example.com/
</VirtualHost>
On my notebook I set the gateway to 192.168.1.1, open my Chrome browser and send an HTTP request to e.g. test.com. I would expect to get redirected to example.com. But I receive an error message saying
ERR_TOO_MANY_REDIRECTS
Does anybody have an idea how I could solve this? I am free for completely different approaches solving my problem!
Your iptables rule creates a loop so requests FROM the proxy are sent again to himself.
You should avoid that by excluding Proxy source IP from rule:
iptables -t nat -A PREROUTING -i eth0 -s ! <Your proxy IP> -p tcp 80 -j DNAT 192.168.1.1:8126

how to put nodejs and apache in the same port 80

I have to put nodejs in port 80, but apache is already using it. How can I put both (nodejs and apache) on the same port 80? I need it because in my university all the ports are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application).
Thanks a lot
I do this via node.js proxy..
Install http-proxy with npm or official page
Example:
var http = require('http'),
httpProxy = require('http-proxy'),
proxyServer = httpProxy.createServer ({
hostnameOnly: true,
router: {
'domain.com': '127.0.0.1:81',
'domain.co.uk': '127.0.0.1:82',
'127.0.0.1': '127.0.0.1:83'
}
});
proxyServer.listen(80);
This creates a node process listening to port 80, and forwarding requests for domains which go to :81,82,83 etc. I recommend running this with forever and adding an entry to init.d so your proxy is up in case system shuts down.
You can also use Apache 2's mod_proxy and mod_proxy_http, which might be more reliable or perform better depending on your system.
Here's an example:
Firstly run below command to proxy to allow
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
# Use Apache for requests to http://example.com/
# but use Node.js for requests to http://example.com/node/
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example/
<Location /node>
ProxyPass http://127.0.0.1:8124/
ProxyPassReverse http://127.0.0.1:8124/
</Location>
</VirtualHost>
And of course you can modify the directives to your needs, such as using a different port for your virtual host (e.g., 443), different port for Node.js, or set up the proxy under a different block, such as for a subdomain (e.g., node.example.com).
I've personally done this the other way round from #liammclennan. Some suggest that proxying through Apache defeats some of the performance and scalability advantages of Node (don't have experience myself as my server doesn't get that much traffic, but from #liammclennan's link: "Every request that comes in through Apache will cause an Apache thread to wait/block until the response is returned from your Node.js process.", which obviously doesn't mesh well with Node's architecture.)
I used node-http-proxy to set up a Node proxy server roughly as described in the first link (my Node proxy runs on port 80; Apache and my other Node services don't). Seems to be working well so far, though I have had occasional stability problems that I've 'solved' through checking the proxy's still running with a cron job (edit: it seems a lot more stable these days). The proxy's pretty lightweight, taking up about 30MB memory.
You can't. You have to run node.js on another port and then proxy requests through apache. You can do this using mod_proxy
http://davybrion.com/blog/2012/01/hosting-a-node-js-site-through-apache/
I usually use haproxy as the front-end in situations like that and have that proxy to the appropriate backend server. (Though making your node.js process a proxy server is a valid approach too depending on your needs).
for httpd.conf
activiate the module , proxy_module and proxy_http
if you are using virtual host
<virtualhost ...>
ServerName api.domain.com
........
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</virtualhost>
assume you are running nodejs server at 8080 , you don't need to take care ssl in nodejs , all should be done in apache
then try https://api.domain.com/
I found a cool gist Run apache and nodejs on port 80. did not try it yet but will do of course
Step 1
Get a VPS that offers 2 or more IP addresses.
Step 2
From the WHM cPanel, find the menu item Service Configuration,
select Apache Configuration and then click on Reserved IPs Editor.
Step 3
Tick the IP address you DON'T WANT Apache to listen to, and write
it down so you can use it in the next step. Click Save.
Step 4
Install Node.js, and create a server like this:
var http = require('http');
var server = http.createServer(function(req, res) {
res.writeHead(200);
res.end('Hello, world!');
});
server.listen(80, '111.111.111.111');
Replacing 111.111.111.111 with the IP address you previously
reserved from the WHM cPanel.
Step 5
Stop wasting your time and never listen to those telling you to use
mod_rewrite to proxy Node.js again.
Update:
We can solve a problem in many different ways and IMHO, we should at least know each possible way 😉. We can do it without buying a new IP of course putting a proxy in front of both Apache and NodeJS server each running other ports except 80.