Home  >  Q&A  >  body text

LAN - How to make nginx accessible to other devices under Linux?

How to make the nginx server running under Linux accessible to other devices in the LAN or even the external network?

I found on the Internet that it is related to iptables, but how to adjust this thing?

天蓬老师天蓬老师2712 days ago692

reply all(1)I'll reply

  • ringa_lee

    ringa_lee2017-05-16 17:23:56

    This is how to turn on and off the firewall ufw on Ubuntu:

    # 开启防火墙(开放80端口):
    sudo ufw enable && sudo ufw default deny && sudo ufw allow 80/tcp && sudo ufw status
    # 关闭防火墙:
    sudo ufw disable && sudo ufw default allow
    sudo iptables -F && sudo iptables -X && sudo iptables -Z && sudo iptables -L

    If you don’t want to use ufw provided by Ubuntu, you can also use iptables directly:

    sudo ufw disable && sudo ufw default allow #用于确保INPUT/FORWARD/OUTPUT几条链都是ACCEPT状态,否则网络访问中断,包括ssh.
    sudo iptables -F && sudo iptables -X && sudo iptables -Z && sudo iptables -L
    sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
    sudo iptables -A INPUT -i lo -j ACCEPT
    sudo iptables -A INPUT -p tcp -i eth0 --dport 22 -j ACCEPT
    sudo iptables -A INPUT -p tcp -i eth0 --dport 80 -j ACCEPT
    sudo iptables -A INPUT -j DROP
    iptables-save > /etc/iptables.up.rules #切换到root用户执行,sudo会提示无权限
    sudo nano /etc/network/interfaces #在末尾添加一行,在网络启动时应用防火墙规则: 
    pre-up iptables-restore < /etc/iptables.up.rules
    #查看设置的规则:
    sudo iptables -nvL --line-numbers
    #插入一条规则到INPUT链第6的位置:
    sudo iptables -I INPUT 6 -j DROP
    #修改INPUT链的第6条规则:
    sudo iptables -R INPUT 6 -j ACCEPT
    #删除INPUT链第6条规则:
    sudo iptables -D INPUT 6

    reply
    0
  • Cancelreply