Shell script forwards 80 port packets to Node.js server _linux shell

Source: Internet
Author: User
Tags iptables

Note: Do not plan to use root user directly to run Node.js service! This will lead to an unpredictable security issue!
However, using port 80 as the HTTP default port is a habit that has lasted since the age of Ms. People on the Internet talk about using Nginx as a reverse proxy, in fact, I do not think it is necessary to exaggerate, just use the root user to do a normal port and 80-port data forwarding is good, use the Iptables statement as follows:

Copy Code code as follows:
Iptables-t nat-a prerouting-p tcp--dport 80-j REDIRECT--to-port your port number

Use this statement to note the following points:
1. This statement is temporary and fails after the server restarts.
2. If you have executed such a statement and want to forward the 80-port binding to another port, you must first use the
Copy Code code as follows:

#查看目前端口转发规则
Iptables--line-numbers--list prerouting-t NAT

#从上面语句输出的列表中找到自己之前绑定转发的端口的记录的行号, and then use the following sentence to delete this record to remove forwarding, and the most painful is that you can not specify the characteristics of the forwarding rule to delete records, you can only tell it "delete the first few lines."
Iptables-t nat-d prerouting Line number

3. Children's shoes using archlinux need attention, possibly because the kernel lacks modules (iptables or NAT)? Instead of using the above statement to set the binding to the originator port, you need to recompile the kernel or module.

Above the egg pain problem, I wrote a script to implement automatic deletion of records and automatic binding:

Copy Code code as follows:
#!/bin/bash
if [$# = 1]; Then
If [$1-ge 0]; Then
If [$1-le 65535]; Then

#Delete all old bindings
For Line_num in $ (iptables--line-numbers--list prerouting-t nat|grep ' {print $} ')
Todo
lines= "$line _num $LINES"
Done
For line in $LINES
Todo
Iptables-t nat-d prerouting $line
Done
Unset LINES

#Make a new binding
Iptables-t nat-a prerouting-p TCP--dport 80-j REDIRECT $
echo "Port is bound with $!"
Exit 1
Fi
Fi
Fi
echo "Please input one PORT number!"


Just save the above code as a file (such as BIND80) and add execution permissions, and use the following instructions to clear the previous record and bind the new port forward:
Copy Code code as follows:

sudo bind80 port number

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.