Configure Redis for External Access
Category Programming Technology
Redis service has been installed on Linux.
XAMPP environment has been installed on Windows for PHP execution.
The PHP code is as follows:
<?php
$redis = new Redis();
$redis->connect('192.168.1.4', 6379);
$redis->set('tag', 'hello');
echo 'name:', $redis->get('tag');
?>
When executing the above code, the following error occurs:
Fatal error: Uncaught exception ‘RedisException’ with message ‘Redis server went away’ in xxxx
RedisException: Redis server went away in xxxxxx
Error Analysis and Solution
The reason for the error is simple: the connection to the Redis service could not be established. Due to Redis's security policy, it defaults to allowing only local access. Simple configuration is needed to allow external network access.
Modify the Redis configuration file to comment out all bind information.
# bind 192.168.1.100 10.0.0.1
# bind 192.168.1.8
# bind 127.0.0.1
After modification, restart the Redis service.
Modify the Linux firewall (iptables) to open your Redis service port, which defaults to 6379.
-A INPUT -m state –state NEW -m tcp -p tcp –dport 6379 -j ACCEPT
……
-A INPUT -j REJECT –reject-with icmp-host-prohibited
Ensure that the Redis firewall configuration is placed before the REJECT rule. Then execute service iptables restart
.
Now, accessing the above code will successfully connect to the Redis service and display correctly.
About bind
Reading online articles, this is often translated as "specify that Redis only receives requests from this IP address. If not set, it will handle all requests. It's best to set this in a production environment." This explanation can totally confuse beginners and is sometimes incorrect. The original English text is:
# If you want you can bind a single interface, if the bind option is not
# specified all the interfaces will listen for incoming connections.
# bind 127.0.0.1
This indicates that bind refers to the interface, meaning the network interface. A server can have one network interface (commonly referred to as a network card) or multiple. For example, if a machine has two network cards, 192.168.205.5 and 192.168.205.6, if bind 192.168.205.5, only that network card will accept external requests. If not bound, both interfaces will accept requests.
OK, I hope that's clear. Let me give another example. In my experiment, I commented out the bind option. Actually, I had another solution. Since my Redis server address is 192.168.1.4. If I didn't comment out the bind option, what else could I do? I could configure it as follows:
# bind 192.168.1.4
Many people mistakenly think that the bound IP should be the source IP of the request. Actually, it should be the IP that the Redis server itself accepts requests from.
Original article: https://blog.csdn.net/hel12he/article/details/46911159
** Click to Share Notes
-
-
-