Redirect port 80 to 8080 and make it work on local machine





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







50















I redirected traffic for port 80 to 8080 on my machine with



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


It works fine for all the world except my own machine. I am a developer and I need to redirect port 80 to 8080 for myself.



My IP is 192.168.0.111



My web server runs on port 8080



I wish to open website from http://192.168.0.111/ instead of http://192.168.0.111:8080/ from same machine where server runs.










share|improve this question

























  • Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?

    – ma11hew28
    Nov 10 '14 at 5:10






  • 1





    @mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.

    – Christian
    Mar 29 '16 at 10:14











  • Why don't you just bind the web server to port 80?

    – David Foerster
    May 23 '16 at 13:01






  • 3





    i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..

    – Pavel K.
    Oct 11 '16 at 10:35


















50















I redirected traffic for port 80 to 8080 on my machine with



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


It works fine for all the world except my own machine. I am a developer and I need to redirect port 80 to 8080 for myself.



My IP is 192.168.0.111



My web server runs on port 8080



I wish to open website from http://192.168.0.111/ instead of http://192.168.0.111:8080/ from same machine where server runs.










share|improve this question

























  • Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?

    – ma11hew28
    Nov 10 '14 at 5:10






  • 1





    @mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.

    – Christian
    Mar 29 '16 at 10:14











  • Why don't you just bind the web server to port 80?

    – David Foerster
    May 23 '16 at 13:01






  • 3





    i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..

    – Pavel K.
    Oct 11 '16 at 10:35














50












50








50


30






I redirected traffic for port 80 to 8080 on my machine with



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


It works fine for all the world except my own machine. I am a developer and I need to redirect port 80 to 8080 for myself.



My IP is 192.168.0.111



My web server runs on port 8080



I wish to open website from http://192.168.0.111/ instead of http://192.168.0.111:8080/ from same machine where server runs.










share|improve this question
















I redirected traffic for port 80 to 8080 on my machine with



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


It works fine for all the world except my own machine. I am a developer and I need to redirect port 80 to 8080 for myself.



My IP is 192.168.0.111



My web server runs on port 8080



I wish to open website from http://192.168.0.111/ instead of http://192.168.0.111:8080/ from same machine where server runs.







iptables port-forwarding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 29 '18 at 9:44









heemayl

68.3k11144215




68.3k11144215










asked Apr 7 '14 at 23:40









MaxMax

453157




453157













  • Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?

    – ma11hew28
    Nov 10 '14 at 5:10






  • 1





    @mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.

    – Christian
    Mar 29 '16 at 10:14











  • Why don't you just bind the web server to port 80?

    – David Foerster
    May 23 '16 at 13:01






  • 3





    i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..

    – Pavel K.
    Oct 11 '16 at 10:35



















  • Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?

    – ma11hew28
    Nov 10 '14 at 5:10






  • 1





    @mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.

    – Christian
    Mar 29 '16 at 10:14











  • Why don't you just bind the web server to port 80?

    – David Foerster
    May 23 '16 at 13:01






  • 3





    i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..

    – Pavel K.
    Oct 11 '16 at 10:35

















Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?

– ma11hew28
Nov 10 '14 at 5:10





Excuse me for the abberation, but what is the purpose of forwarding port 80 to 8080?

– ma11hew28
Nov 10 '14 at 5:10




1




1





@mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.

– Christian
Mar 29 '16 at 10:14





@mattdipasquale, normal users can't access port 80 so you couldn't run a web service like python flask as a normal user.

– Christian
Mar 29 '16 at 10:14













Why don't you just bind the web server to port 80?

– David Foerster
May 23 '16 at 13:01





Why don't you just bind the web server to port 80?

– David Foerster
May 23 '16 at 13:01




3




3





i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..

– Pavel K.
Oct 11 '16 at 10:35





i'd guess its because non-root user cannot bind to ports 80/443 and he doesnt want to run his web service as root..

– Pavel K.
Oct 11 '16 at 10:35










4 Answers
4






active

oldest

votes


















72














You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following should work; run as root:



iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080





share|improve this answer





















  • 1





    Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

    – alper
    Jul 28 '17 at 23:39








  • 2





    This did not work, not sure why this is voted up

    – diyoda_
    Mar 5 '18 at 22:11











  • @Diyoda_ Please define did not work.

    – heemayl
    Mar 29 '18 at 9:44











  • @Alper I think you didn't read the OPs question.

    – styl3r
    Apr 23 '18 at 22:19






  • 1





    I can confirm that this doesn't work on ubuntu 18.04

    – Ahmed Hamdy
    Jun 24 '18 at 11:23



















3














Instead of the iptables, You could try:

sudo ssh -gL 80:127.0.0.1:8080 localhost






share|improve this answer



















  • 2





    That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

    – Max
    Apr 8 '14 at 10:45











  • Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

    – cgseller
    Nov 15 '16 at 20:24



















2














This worked for me.



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





share|improve this answer































    1














    Simple just use iptables allowing both port 80 and 8080 then redirect 80 to 8080 make sure you are assigning to the correct nic.. in example I use eth0



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





    share|improve this answer


























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "89"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f444729%2fredirect-port-80-to-8080-and-make-it-work-on-local-machine%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      72














      You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following should work; run as root:



      iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080





      share|improve this answer





















      • 1





        Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

        – alper
        Jul 28 '17 at 23:39








      • 2





        This did not work, not sure why this is voted up

        – diyoda_
        Mar 5 '18 at 22:11











      • @Diyoda_ Please define did not work.

        – heemayl
        Mar 29 '18 at 9:44











      • @Alper I think you didn't read the OPs question.

        – styl3r
        Apr 23 '18 at 22:19






      • 1





        I can confirm that this doesn't work on ubuntu 18.04

        – Ahmed Hamdy
        Jun 24 '18 at 11:23
















      72














      You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following should work; run as root:



      iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080





      share|improve this answer





















      • 1





        Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

        – alper
        Jul 28 '17 at 23:39








      • 2





        This did not work, not sure why this is voted up

        – diyoda_
        Mar 5 '18 at 22:11











      • @Diyoda_ Please define did not work.

        – heemayl
        Mar 29 '18 at 9:44











      • @Alper I think you didn't read the OPs question.

        – styl3r
        Apr 23 '18 at 22:19






      • 1





        I can confirm that this doesn't work on ubuntu 18.04

        – Ahmed Hamdy
        Jun 24 '18 at 11:23














      72












      72








      72







      You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following should work; run as root:



      iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080





      share|improve this answer















      You need to use the OUTPUT chain as the packets meant for the loopback interface do not pass via the PREROUTING chain. The following should work; run as root:



      iptables -t nat -A OUTPUT -o lo -p tcp --dport 80 -j REDIRECT --to-port 8080






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Jun 24 '18 at 11:50

























      answered Jan 30 '15 at 5:15









      heemaylheemayl

      68.3k11144215




      68.3k11144215








      • 1





        Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

        – alper
        Jul 28 '17 at 23:39








      • 2





        This did not work, not sure why this is voted up

        – diyoda_
        Mar 5 '18 at 22:11











      • @Diyoda_ Please define did not work.

        – heemayl
        Mar 29 '18 at 9:44











      • @Alper I think you didn't read the OPs question.

        – styl3r
        Apr 23 '18 at 22:19






      • 1





        I can confirm that this doesn't work on ubuntu 18.04

        – Ahmed Hamdy
        Jun 24 '18 at 11:23














      • 1





        Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

        – alper
        Jul 28 '17 at 23:39








      • 2





        This did not work, not sure why this is voted up

        – diyoda_
        Mar 5 '18 at 22:11











      • @Diyoda_ Please define did not work.

        – heemayl
        Mar 29 '18 at 9:44











      • @Alper I think you didn't read the OPs question.

        – styl3r
        Apr 23 '18 at 22:19






      • 1





        I can confirm that this doesn't work on ubuntu 18.04

        – Ahmed Hamdy
        Jun 24 '18 at 11:23








      1




      1





      Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

      – alper
      Jul 28 '17 at 23:39







      Does port 80 required to be gloabally accesible? I tried your solution, my port number 8080 is accessible but 80 is not hence it did not worked. @heemayl

      – alper
      Jul 28 '17 at 23:39






      2




      2





      This did not work, not sure why this is voted up

      – diyoda_
      Mar 5 '18 at 22:11





      This did not work, not sure why this is voted up

      – diyoda_
      Mar 5 '18 at 22:11













      @Diyoda_ Please define did not work.

      – heemayl
      Mar 29 '18 at 9:44





      @Diyoda_ Please define did not work.

      – heemayl
      Mar 29 '18 at 9:44













      @Alper I think you didn't read the OPs question.

      – styl3r
      Apr 23 '18 at 22:19





      @Alper I think you didn't read the OPs question.

      – styl3r
      Apr 23 '18 at 22:19




      1




      1





      I can confirm that this doesn't work on ubuntu 18.04

      – Ahmed Hamdy
      Jun 24 '18 at 11:23





      I can confirm that this doesn't work on ubuntu 18.04

      – Ahmed Hamdy
      Jun 24 '18 at 11:23













      3














      Instead of the iptables, You could try:

      sudo ssh -gL 80:127.0.0.1:8080 localhost






      share|improve this answer



















      • 2





        That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

        – Max
        Apr 8 '14 at 10:45











      • Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

        – cgseller
        Nov 15 '16 at 20:24
















      3














      Instead of the iptables, You could try:

      sudo ssh -gL 80:127.0.0.1:8080 localhost






      share|improve this answer



















      • 2





        That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

        – Max
        Apr 8 '14 at 10:45











      • Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

        – cgseller
        Nov 15 '16 at 20:24














      3












      3








      3







      Instead of the iptables, You could try:

      sudo ssh -gL 80:127.0.0.1:8080 localhost






      share|improve this answer













      Instead of the iptables, You could try:

      sudo ssh -gL 80:127.0.0.1:8080 localhost







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Apr 8 '14 at 0:06









      GregorGregor

      29417




      29417








      • 2





        That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

        – Max
        Apr 8 '14 at 10:45











      • Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

        – cgseller
        Nov 15 '16 at 20:24














      • 2





        That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

        – Max
        Apr 8 '14 at 10:45











      • Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

        – cgseller
        Nov 15 '16 at 20:24








      2




      2





      That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

      – Max
      Apr 8 '14 at 10:45





      That is an option but it is not exactly what I want because I already have web server on port 80. I will prefer to do it with iptables and keep web server on port 80 running. I guess I just have to apply rule to different step instead of PREROUTING

      – Max
      Apr 8 '14 at 10:45













      Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

      – cgseller
      Nov 15 '16 at 20:24





      Yes - this will cause a port conflict if you have something listening that you are port forwarding to as Max suggested. The above answer is the more general case.

      – cgseller
      Nov 15 '16 at 20:24











      2














      This worked for me.



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





      share|improve this answer




























        2














        This worked for me.



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





        share|improve this answer


























          2












          2








          2







          This worked for me.



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





          share|improve this answer













          This worked for me.



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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 22 at 12:25









          Sanket ChaudhariSanket Chaudhari

          3316




          3316























              1














              Simple just use iptables allowing both port 80 and 8080 then redirect 80 to 8080 make sure you are assigning to the correct nic.. in example I use eth0



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





              share|improve this answer






























                1














                Simple just use iptables allowing both port 80 and 8080 then redirect 80 to 8080 make sure you are assigning to the correct nic.. in example I use eth0



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





                share|improve this answer




























                  1












                  1








                  1







                  Simple just use iptables allowing both port 80 and 8080 then redirect 80 to 8080 make sure you are assigning to the correct nic.. in example I use eth0



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





                  share|improve this answer















                  Simple just use iptables allowing both port 80 and 8080 then redirect 80 to 8080 make sure you are assigning to the correct nic.. in example I use eth0



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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Feb 18 at 1:47









                  Kevin Bowen

                  14.9k155971




                  14.9k155971










                  answered Feb 18 at 0:53









                  tmactmac

                  111




                  111






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Ask Ubuntu!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f444729%2fredirect-port-80-to-8080-and-make-it-work-on-local-machine%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      How to change which sound is reproduced for terminal bell?

                      Can I use Tabulator js library in my java Spring + Thymeleaf project?

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents