LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 12-19-2016, 10:54 AM   #1
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Rep: Reputation: Disabled
Redirect port 443 requests to port 3000 on hostmonster (Centos 6.8) for Node Express Application listening on port 3000


I have a secure (https) node express application that I have listening on port 3000 => https://mywebsite.net:3000 running on hostmonster (Centos 6.8).

I want requests to port 443 (i.e. https://mywebsite.net / https://mywebsite.net:443) to be forwarded to my node express server application listening on port 3000 => https://mywebsite.net:3000

This is due to not be able to directly listen on port 443.

I logged into hostmonster as root.

I added the following rule to the iptable:

iptables -t nat -A PREROUTING -s 123.123.123.123 -p tcp --dport 443 -j DNAT --to-destination
service iptables save

I see the following when I type: iptables -t nat -L

Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- server.mywebsite anywhere tcp dpt:https to: 123.123.123.123:3000
DNAT tcp -- server.mywebsite anywhere tcp dpt:https to: 123.123.123.123:3000

The issue is if I type https://mywebsite.net or https://mywebsite.net:443 (same thing) in a browser it does not go to my main node express application page listening on port 3000.

I also used Google's postman to do a request of https://mywebsite.net:443 and it does not return information from my main node express application page listening on port 3000.

So it does not appear the redirect from port 443 to port 3000 is working?

What am I missing?
 
Old 12-20-2016, 02:22 AM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,173
Blog Entries: 1

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Hi,

Apart from iptables, you need to enable IP forwarding:
Code:
sysctl -w net.ipv4.ip_forward=1
For a complete example, have a look here

Regards
 
Old 12-20-2016, 09:18 AM   #3
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Great thank you for your help!
 
Old 12-20-2016, 09:40 AM   #4
Coffee!!!
LQ Newbie
 
Registered: Dec 2016
Distribution: RHEL
Posts: 18

Rep: Reputation: Disabled
You're just trying to forward port traffic? You don't need to enable IP forwarding on your server and its as easy as adding these lines to your current Apache SSL.conf:


ProxyPreserveHost On
ProxyPass / https://localhost:3000/
ProxyPassReverse / https://hostname.com/
</VirtualHost>
 
Old 12-20-2016, 10:09 AM   #5
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
Yes I am just trying to forward port traffic. And thank you, I will try this out.
 
Old 12-20-2016, 10:22 AM   #6
Coffee!!!
LQ Newbie
 
Registered: Dec 2016
Distribution: RHEL
Posts: 18

Rep: Reputation: Disabled
No problem. I use this setup on all of my web servers and it works flawlessly with Apache Tomcat (both 8080 and 8443). Depending on what kind of traffic is passed in the headers, you may have to fiddle with mods but Apache has a ton of documentation about which ones you might need.
 
Old 12-20-2016, 11:18 AM   #7
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
Quote:
Originally Posted by brentw View Post
iptables -t nat -A PREROUTING -s 123.123.123.123 -p tcp --dport 443 -j DNAT --to-destination
service iptables save

I see the following when I type: iptables -t nat -L

Chain PREROUTING (policy ACCEPT)
target prot opt source destination
DNAT tcp -- server.mywebsite anywhere tcp dpt:https to: 123.123.123.123:3000
DNAT tcp -- server.mywebsite anywhere tcp dpt:https to: 123.123.123.123:3000
For redirecting to a port on the same system you want to use REDIRECT not DNAT.
You rule should looks something like this:
Code:
iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 3000
The other problem is you are NAT'ing on the Source address which is not needed unless you only want the connection from that IP to be natted.
 
1 members found this post helpful.
Old 12-20-2016, 11:25 AM   #8
brentw
LQ Newbie
 
Registered: Aug 2014
Posts: 29

Original Poster
Rep: Reputation: Disabled
This worked, thank you!
 
Old 12-20-2016, 11:38 AM   #9
lazydog
Senior Member
 
Registered: Dec 2003
Location: The Key Stone State
Distribution: CentOS Sabayon and now Gentoo
Posts: 1,249
Blog Entries: 3

Rep: Reputation: 194Reputation: 194
If any of the suggestion above fixed your issue it would be nice if you stated what fixed your problem and marked the thread as SOLVED.

Maybe even click that "Did you find this post helpful? Yes" to add to the persons rep so people will know this person helped others in the past.
 
  


Reply

Tags
centos, iptables, node, port forwarding



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Making request at port 443, but strangely my requests are redirected to port 80. anjo236 Linux - Newbie 3 03-01-2016 01:16 PM
Tomcat6 stops listening on port 80 when i change from port 8080 to port 80 trongthect Linux - Server 1 07-27-2012 05:41 PM
Port redirect 8443 to 443 roshan.s Linux - Networking 4 07-15-2011 01:40 AM
Apache/2.2.4 openSUSE10.3 not listening on port 443 even ssl_module is present farhan Linux - Server 6 10-27-2008 11:42 AM
redirect some http requests to port 443. FMH Linux - Software 5 09-17-2007 08:19 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 03:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration