How can I run a Spring Boot application on port 80

前端 未结 6 1706
广开言路
广开言路 2020-12-02 10:43

I can\'t start an application on port 80.

I have tried on my local computer (using my IDE, and on a local server), no luck.

I have checked other similar pos

6条回答
  •  佛祖请我去吃肉
    2020-12-02 11:04

    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:

    
    
        ProxyPreserveHost On
    
        # ...
    
        ProxyPass / http://localhost:8080/
    
    

    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
    

提交回复
热议问题