Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

So, I have my Glassfish server is listening on port 8080.
However, I want to have my requests at port 80 while hiding 8080,
so that people think my server really runs at 80.

How can I do it with iptables? I have tried the following rule:

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 8080

but it's not what I want, because I have to open both 80 and 8080 for it to work.

Any ideas?


Update: I've changed my rules' file to look like:

iptables -F

iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT
iptables -P FORWARD DROP

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT

iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

iptables -A INPUT -j DROP

#REDIRECTION RULES
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT  \
         --to-destination 127.0.0.1:8080

But still doesn't work. I have /proc/sys/net/ipv4/ip_forward set to 1.
Nor http://myserver:8080 neither http://myserver:80 work. The packets are dropped.

share|improve this question
    
Please help! What I'm doing wrong? –  jmendeth Aug 4 '11 at 20:03

2 Answers 2

Have you tried changing:

iptables -P FORWARD DROP

to

iptables -P FORWARD ACCEPT

?

share|improve this answer

Use DNAT.

iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination your.ip.address:8080

Something like that should work. :)

EDIT after your own edit: I think this line is too early in your rule set:

iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT

Thanks to that your server does not accept any NEW connections, right? Try to move it right before your iptables -A INPUT -j DROP line.

share|improve this answer
    
Mmmm, doesn't work. I will edit my question and add my rules file. –  jmendeth Aug 4 '11 at 9:15
    
Also tell us how it does not work. –  Janne Pikkarainen Aug 4 '11 at 9:19
    
Edited my reply. –  Janne Pikkarainen Aug 4 '11 at 9:26
    
So, what will be the correct position of the rule? I thought it was in its correct position. –  jmendeth Aug 4 '11 at 9:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.