node.js - nginx 访问不了非80端口
本文介绍了node.js - nginx 访问不了非80端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
在CentOS服务器上装好了nginx,直接通过公网ip可以正常显示nginx 的 index.html页面。
同时我在3000端口跑了node应用,现在我想通过 公网ip:8089的形式访问
nginx的配置代码:
server {
listen 8089;
server_name 公网ip;
location / {
root /usr/web;
proxy_pass http://127.0.0.1:3000;
index index.html;
}
}
奇怪的是当监听端口是80的时候就没有问题,其他端口都不行。
防火墙也开放了8089端口,关掉也不行:
[root@VM_102_32_centos conf]# service iptables status
Table: filter
Chain INPUT (policy ACCEPT)
num target prot opt source destination
1 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:8089
请问下大家是哪里的问题呢?不胜感激 (●゚ω゚●)
2017.1.16 更新:
问题解决了,是腾讯云服务器安全组没有开放8080端口,在腾云讯后台添加配置就可以了
解决方案
2017.1.6更新开始"
请问你是想通过nginx反向代理node的服务吗?
如果是,请将root信息删除:
root /usr/web;
2017.1.6更新结束"
首先,你nginx的配置是没问题的,但可以修改成以下格式:
server {
listen 8089;
root /usr/web;
location / {
index index.html;
proxy_pass http://127.0.0.1:3000;
}
}
然后请通过以下命令确认你的nginx正处于运行状态:
[root@centos-test ~]# ps -aux | grep nginx
root 1376 0.0 0.5 122948 5128 ? Ss 19:02 0:00 nginx: master process nginx
nginx 1406 0.0 0.3 123180 3616 ? S 19:03 0:00 nginx: worker process
nginx 1407 0.0 0.4 123180 4100 ? S 19:03 0:00 nginx: worker process
root 1410 0.0 0.0 112664 932 pts/0 R+ 19:03 0:00 grep --color=auto nginx
还可以通过以下命令确认你的nginx正监听着正确的端口:
[root@centos-test ~]# netstat -anp | grep 8089
tcp 0 0 0.0.0.0:8089 0.0.0.0:* LISTEN 1376/nginx: master
tcp 0 0 10.1.1.118:8089 10.1.1.66:54931 ESTABLISHED 1407/nginx: worker
tcp 0 0 10.1.1.118:8089 10.1.1.66:54932 ESTABLISHED 1407/nginx: worker
如果nginx确认是运行正常,那么请通过以下命令确认iptables是处于放行状态:
[root@centos-test ~]# iptables -L -vn
要注意的是,iptables在默认状态下的FORWARD链中含有一条REJECT规则:
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
你可以通过以下命令将其删除:
#先显示规则行数
[root@centos-test ~]# iptables -L -vn --line-number
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
num pkts bytes target prot opt in out source destination
1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
#然后删除该规则
[root@centos-test ~]# iptables -D FORWARD 1
#保存并重新加载
[root@centos-test ~]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ]
[root@centos-test ~]# service iptables reload
Redirecting to /bin/systemctl reload iptables.service
在这里要注意的是,你的iptables中INPUT链的默认规则是ACCEPT,并不需要手动添加放行规则。
如果nginx还不能访问,请检查proxy_pass的服务器是否运行正常。
这篇关于node.js - nginx 访问不了非80端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文