How can I run a Spring Boot application on port 80

On linux ports below 1024 can be opened only by root, so the port 80 is restricted by default

if you want to publish your app on 80 port you need to redirect request from port 80 to the port you gonna run your springapp (e.g 8080) port

Solution 1: HTTP Proxy server

You can use Apache2 server which is allowed by default to work on port 80 and can forward requests for you to Tomcat

Example configuration for Debian

sudo apt-get install apache2

a2enmod proxy
a2enmod proxy_http   

cd /etc/apache2/sites-enabled
sudo nano 000-default.conf

Edit file:

<VIRTUALHOST *:80>

    ProxyPreserveHost On

    # ...

    ProxyPass / http://localhost:8080/
</VIRTUALHOST>

Save file: Ctrl+O, ENTER, Ctrl+X

Note: To learn more about virtual host configurations, you can check out the detailed Apache manual on the subject by clicking here.

Restart Apache2 to apply changes:

sudo service apache2 restart

or

sudo systemctl restart apache2

Solution 2: Port forwarding

Use iptables for redirects

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

if you need to use localhost also add this

iptables -t nat -I OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j REDIRECT --to-ports 8080

Leave a Comment