历尽千辛万苦,我终于将node.js的应用运行了起来,看起来貌似一切正常的样子。
可是新的问题来了,别人的网站都直接可以通过域名访问,并且一般都是80或者443(HTTPS)端口,而我这个是8080端口又是怎么回事?又该如何解决呢?
(图源 :pixabay)
直接换端口的方式肯定是行不通的啦,因为Linux下是不允许普通用户应用绑定特权端口(0至1023)的,那就要想别的方法了。
iptables转发
方法之一是使用iptables转发,以前做过类似的操作,执行下列指令即可:
sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
当然,除此之外,要确定已经配置了允许转发:
sudo vi /etc/sysctl.conf
在其中将net.ipv4.ip_forward = 0
修改为net.ipv4.ip_forward = 1
,然后执行如下指令使其生效:
sudo sysctl -p
Nginx
另外一种方法是使用Nginx。
如果还没有安装Nginx,那么可以使用如下指令安装:
sudo apt-get install nginx
安装完成之后,浏览器中直接访问IP,可以看到默认站点已经生效。
接下来我们创建一个简单的配置文件:
sudo vi /etc/nginx/sites-available/myapp
大致内容如下:
server {
listen 80;
server_name myapp.com;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:8080";
}
}
接下来在sites-enabled目录下创建指向我们配置文件的软链接
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/myapp
还有删除指向默认配置文件的软链接
sudo rm /etc/nginx/sites-enabled/default
重启Nginx,使我们的配置文件激活
sudo service nginx restart
再次在浏览器中直接访问IP,啊哈,我们的应用正常显示啦(80端口)。
其它方法
除了上述两个办法以外,还有一些其它方法可以达成此目标,我了解到的有如下可行办法:
- setcap
- authbind
- SSH转发
因为nginx很好地满足了我的需求,我就不折腾啦,感兴趣的朋友自己测试啦。
相关链接
- 每天进步一点点: Linux普通用户下程序绑定特权端口(iptables转发)
- http://nginx.org/en/docs/http/ngx_http_proxy_module.html
- https://stackoverflow.com/questions/6109089/how-do-i-run-node-js-on-port-80
https://steemit.com/~witnesses type in
oflyhigh
and click VOTE
Vote @oflyhigh via Steemconnect
Thank you!
恭喜你!您的这篇文章入选 @justyy 今日 (2019-07-10) 榜单 【优秀的文章】, 回复本条评论24小时内领赏,点赞本评论将支持 @dailychina 并增加将来您的奖赏。
@justyy 是CN区的见证人,请支持他,给他投票,或者设置justyy为见证人代理。感谢!@justyy的主要贡献:https://steemyy.com
Congratulations! This post has been selected by @justyy as today's (2019-07-10) 【Good Posts】, Steem On! Reply to this message in 24 hours to get rewards. Upvote this comment to support the @dailychina and increase your future rewards! ^_^
SteemIt 工具、API接口、机器人和教程
SteemIt Tools, Bots, APIs and Tutorial
If you believe what I am doing, please consider a spare vote voting me here, thank you very much indeed.
@justyy - the author of https://SteemYY.com and I have been a Steem Witness for more than a year now.
区块链中文第一大银行简介:你的比特币拿不了利息,但是你把比特币换成steem后就能拿利息了!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
thx
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
Thank you so much for participating in the Partiko Delegation Plan Round 1! We really appreciate your support! As part of the delegation benefits, we just gave you a 3.00% upvote! Together, let’s change the world!
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit