doushi6864 2016-01-04 12:31
浏览 160
已采纳

golang-如何将数据从默认端口路由到另一个[4000]端口[关闭]

currently my go server is running on port 4000. to access web application i need to type somedomainname:4000 in browser.

I would like to only type somedomainname and it should make the connection to web server on port 4000.

  • 写回答

1条回答 默认 最新

  • duanchendu69495 2016-01-04 13:17
    关注

    There are several solutions for this:

    1. Have your Go server listen directly on port 80. However, be careful with how you implement this. Do not have your service run as root, but use Linux capabilities instead (thanks to @JimB who reminded me of this in comments). You can use setcap to grant a process the capability to bind to a privileged port:

      > setcap 'cap_net_bind_service=+ep' /path/to/your/application
      
    2. Use an HTTP reverse proxy like Nginx to forward all HTTP requests from port 80 to your Go application. Here's an example configuration file for Nginx:

      upstream yourgoapplication {
        server localhost:4000;
      }
      
      server {
        listen 80;
      
        server_name somedomainname;
      
        location / {
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Forwarded-Proto $scheme;
      
          proxy_pass http://yourgoapplication;
        }
      }
      

      When you do this, you can configure to Go application to listen on 127.0.0.1:4000 instead of 0.0.0.0:4000 to make your application accessible only by port 80.

    3. If and when you are deploying your application in a Docker container, you can simply map the container port 4000 to the host port 80. See the manual for more information.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?