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

How can I port-forward port 80 internally to port 8080?

My goal is to have a web app server (Glassfish) running on port 8080, but for the outside world to access it normally on port 80. This is being done so that I don't have to run Glassfish as root.

I tried adding the following rule to my /etc/sysconfig/iptables:

-A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

but this results in the following error:

Applying iptables firewall rules: iptables-restore v1.3.5: Line 21 seems to have a -t table option.
share|improve this question

2 Answers 2

up vote 4 down vote accepted

You cannot specify the table like that in/etc/sysconfig/iptables. Each table is set with an asterisk then the table name. Here is a skeleton of what you'd do:

*nat
:PREROUTING ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
:POSTROUTING ACCEPT [0:0]
-A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination :8080
COMMIT
*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
COMMIT

Instead of editing the file you could also manually set up the rules you like using the iptables command and then execute iptables-save > /etc/sysconfig/iptables or service iptables save.

share|improve this answer
    
That did the trick. Brilliant, thank you! –  Caffeine Coma Feb 4 '11 at 22:46

you were close

iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j DNAT --to-destination :8080

It has to do NAT so that when the reply is sent back to the client, it appears to come from port 80, not 8080.

share|improve this answer
    
Hmm, while this works on the command line, I still get the above error when I add the rule to /etc/sysconfig/iptables. –  Caffeine Coma Feb 4 '11 at 21:16
    
Once its added, just do /etc/init.d/iptables save –  Patrick Feb 4 '11 at 22:45

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.