记一次阿里云服务器上使用nginx部署前端vue打包后的代码bug排查过程

博主小白,第一次部署前端vue打包后的dist项目,已知该dist使用hash模式打包,部署在阿里云centos系统上,使用docker+nginx容器部署;

问题:部署好之后容器内执行curl http://127.0.0.1:80可以获取到页面,但是在宿主机通过curl http://宿主机ip:881端口访问不到数据

一、排查可能原因1:nginx.conf文件没有写好;

一开始listen只写了80后来了解到只写80的话他就只监听本地回环地址127.0.0.1,所以改为了0.0.0.0:80,
下面是修改后的nginx.conf文件


user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;
    server {
        # listen 标识了监听的ip和端口;
        listen       0.0.0.0:80; 
        server_name localhost;
        location / {
            root /usr/share/nginx/html;
            index index.html index.htm;
        }
    }
    include /etc/nginx/conf.d/*.conf;
}

还是不行,后边觉得可能是防火墙的原因,所以执行sudo firewall-cmd --permanent --list-all
发现防火墙是关闭状态;
接着咨询AI发现还有个iptables来管理端口访问规则,AI的建议为了确保 8081 端口可以从外部访问,应该添加一条规则来允许 TCP 流量进入该端口。你可以使用以下命令来添加规则:sudo iptables -I INPUT -p tcp --dport 8081 -j ACCEPT
因为不熟悉这条命令并且之前碰到过iptables导致docker某个端口无法访问,重启一下就能解决,所以我选择了重启iptables sudo systemctl restart iptables,命令报错:没有这个服务;震惊,明明有这个进程,执行sudo iptables -L -n -v可以列出iptable的所有规则;然后接着问AI,AI给的回复是:

当你遇到 "Unit iptables.service not found." 的错误时,这通常意味着你的系统上没有使用 iptables.service 这个服务单元。这可能是因为你的系统使用的是 iptables 的替代品,比如 nftables,或者是 iptables 的服务单元名称在你的系统上有所不同。
在许多现代 Linux 发行版中,iptables 的服务单元可能被称为 iptables.service、netfilter-persistent.service 或者 nftables.service,具体取决于你的系统配置和安装的软件包。
解决方案
检查服务单元名称
首先,确认你的系统上实际使用的服务单元名称。你可以使用 systemctl list-unit-files | grep iptables 或者 systemctl list-unit-files | grep nftables 来查找相关服务。
然后确定服务名字是nftables之后执行sudo systemctl restart nftables重启这个服务,在之后我又重启了我的docker服务sudo systemctl restart docker

执行完之后发现我从宿主机阿里云服务器执行curl 命令可以访问到nginx的服务了;但是我再本地还是无法访问,不用说肯定是阿里云做了拦截,接下来去阿里云服务器的白名单里边把这个端口开放出来就好了;

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容