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.

I've routed all requests from port 80 to port 8080 for my Tomcat server using iptables.

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

Now http://mydomain.com/ works fine, but people can still access http://mydomain.com:8080. I see that on other sites this is not possible. Is there anyway to disable direct access to port 8080 via the url?

share|improve this question
    
Is there a reason not to simply run your Tomcat server on port 80 natively? –  BMDan Jul 21 '10 at 13:47
    
Apparently that requires giving root access to the Tomcat user and that it's generally a bad idea. That's what I've read, at least. –  Matt H Jul 21 '10 at 16:40
add comment

2 Answers

up vote 1 down vote accepted

When you access http://mydomain.com, does it rewrite the URL to http://mydomain.com:8080? If it does, I don't see how to make it work using purely iptables (because you will need the 8080 port to be open to answer the requests). I did a quick test here and the URL was being rewritten but it may be my site doing it.

Said that, maybe it's better to put a nginx or apache with ProxyPass/ProxyPassReverse in front of your application to deal with URLs, with the plus of being able to use nginx's or apache's URL rewrite, load balancing and other capabilities.

share|improve this answer
    
Thanks, I decided to use nginx. –  Matt H Jul 22 '10 at 11:25
add comment

I haven't been using iptables for very long now so I wouldn't know how to write the rules for you but I believe if you drop or reject all requests for port 8080 in the filter table then put the statement you provided in the postrouting chain you will get your desired result.

I'm still trying to learn iptables so if this is wrong could you tell me why in the comments?

share|improve this answer
add comment

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.