Configure redis Internet access and configure redis access

Source: Internet
Author: User

Configure redis Internet access and configure redis access
Environment Description

InLinuxInstall the redis Service

InWindowsThe xampp environment is installed in. For PHP running

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 the above code is executed, the following error is reported:

Fatal error: Uncaught exception 'redisexception' with message 'redis server went loan' in xxxx

RedisException: Redis server went away in xxxxxx

Error solution analysis and solutions

The cause of the error is very simple, that is, the redis service is not connected. Because of the Security Policy adopted by redis, only local access is allowed by default. Simple configuration is required to allow Internet access.

  • Modify the redis configuration file to shield all bind information.

    # Bind 192.168.1.100 10.0.0.1
    # Bind 192.168.1.8
    # Bind 127.0.0.1

    After the modification, You need to restart the redis service.

  • ModifyLinuxTo enable the redis service port. The default value is 6379.

    -A input-m state-state NEW-m tcp-p tcp-dport 6379-j ACCEPT
    ......
    -A input-j REJECT-reject-with icmp-host-prohibited

    Please note that you must put the redis firewall configuration inREJECT. Then executeservice iptables restart

Now, access to the code above can be linked to the redis service and displayed correctly.

About bind

After reading the articles on the Internet, it is often translated as "specify redis to only receive requests from this IP address. If this parameter is not set, all requests will be processed and it is best to set this option in the production environment ". This interpretation will confuse beginners with totally, or even make mistakes. The original English text here 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 the bind is an interface, that is, a network interface. The server can have one or more network interfaces (network cards. For example, there are two NICs on the machine: 192.168.205.5 and 192.168.205.6. If bind 192.168.205.5, only the NIC address accepts external requests. If not bound, both NICs accept requests.

OK. I don't know if it is clear. Let's take an example. During my experiment above, I commented out the bind item. In fact, I still have a solution. Because the address of my redis server is192.168.1.4. If I do not comment on the bind item, what can I do? I can make the following Configuration:

# Bind 192.168.1.4

Many may mistakenly assume that the bound ip address is the ip address of the request source. Otherwise, your redis server should be bound with the ip address that accepts the request.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.