Home Backend Development PHP Tutorial nginx_lua case analysis: dynamic routing implementation

nginx_lua case analysis: dynamic routing implementation

Jul 30, 2016 pm 01:31 PM
host proxy quot redis source

    这里的路由指的是在web开发中,访问路径以及具体实现内容的映射。比如,/a映射到某个具体的页面,这个就称之为一个路由。而动态路由,顾名思义就是动态添加这种路由映射关系。

    在nginx中,通过rewrite和proxy_pass来实现路由映射或者说反向代理,但是这种关系按照传统的配置必须写死在配置文件中,然后通过快速"无缝"重启nginx。虽说是无缝,但是其繁琐的配置和枯燥的重启操作还是无法避免。

    最近,在github上看到个项目ceryx,是nginx结合lua进行动态路由的映射的,也就是上面所说的映射关系,用lua来管理,虽然是比较简单的实现,但是可以分析学习下。该项目通过用redis的结构来保存这种映射关系,这样在nginx中可以快速获得这种关系,以便做出处理,同时,采用HTTP的形式暴露对redis这种路由关系进行管理的接口。

from ceryx.db import RedisRouter


resource_fields = {
    'source': fields.String,
    'target': fields.String,
}

parser = reqparse.RequestParser()
parser.add_argument(
    'source', type=str, required=True, help='Source is required'
)
parser.add_argument(
    'target', type=str, required=True, help='Target is required'
)

router = RedisRouter.from_config()


def lookup_or_abort(source):
    """
    Returns the target for the given source, or aborts raising a 404
    """
    try:
        return {'source': source, 'target': router.lookup(source)}
    except RedisRouter.LookupNotFound:
        abort(
            404, message='Route with source {} doesn\'t exist'.format(source)
        )


class Route(Resource):
    """
    Resource describing a single Route. Supports GET, DELETE and PUT. The
    format is the following:
    ```
    {
        "source": "[source]",
        "target": "[target]"
    }
    ```
    """

    @marshal_with(resource_fields)
    def get(self, source):
        """
        Fetches the route with the given source
        """
        route = lookup_or_abort(source)
        return route

    @marshal_with(resource_fields)
    def delete(self, source):
        """
        Deletes the route with the given source
        """
        route = lookup_or_abort(source)
        router.delete(source)
        return route, 204

    @marshal_with(resource_fields)
    def put(self, source):
        """
        Creates or updates the route with the given source, pointing to the
        given target
        """
        args = parser.parse_args()
        router.insert(args['source'], args['target'])
        return args, 201
Copy after login

    上述的代码,是进行路由管理的http api,当然,这个不是我们分析的重点。先来看一下,在这个项目里面,对nginx的配置是怎样的

upstream fallback {
    server www.something.com;
}

server {
    listen 80;
    default_type text/html;

    location / {
        set $container_url "fallback";
        resolver 8.8.8.8;

        # Lua files
        access_by_lua_file lualib/router.lua;//切入点

        # Proxy configuration
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect ~^(http://[^:]+):\d+(/.+)$ $2;
        proxy_redirect / /;

        # Upgrade headers
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_pass http://$container_url$request_uri;
    }
    ...
}
Copy after login
可以简单的看到,这个配置相当的常见,跟普通的反向代理并没有什么不同,真正的切入点在于access_by_lua_file里面的router.lua代码。

local container_url = ngx.var.container_url//拿到配置文件中的container_url
local host = ngx.var.host  //拿到请求的时候的host,比如我们请求http://www.xxx.com/a.html 那这里的host就是www.xxx.com

-- Check if key exists in local cache
local cache = ngx.shared.ceryx
local res, flags = cache:get(host)   //直接在nginx cache中拿host对应的路由映射,如果存在则直接返回结果
if res then
    ngx.var.container_url = res
    return
end

local redis = require "resty.redis"  // redis的连接代码  每次都会连接redis,
local red = redis:new()<span style="white-space:pre">			</span>//<span style="font-family: Arial, Helvetica, sans-serif;">这个操作比较相对比较耗时 所以接下来的操作才会在本地cache中存对应的关系</span>
red:set_timeout(100) -- 100 ms
local redis_host = os.getenv("CERYX_REDIS_HOST")
if not redis_host then redis_host = "127.0.0.1" end
local redis_port = os.getenv("CERYX_REDIS_PORT")
if not redis_port then redis_port = 6379 end
local res, err = red:connect(redis_host, redis_port)

-- Return if could not connect to Redis
if not res then
    return
end

-- Construct Redis key
local prefix = os.getenv("CERYX_REDIS_PREFIX")
if not prefix then prefix = "ceryx" end
local key = prefix .. ":routes:" .. host

-- Try to get target for host
res, err = red:get(key)
if not res or res == ngx.null then
    -- Construct Redis key for $wildcard
    key = prefix .. ":routes:$wildcard"
    res, err = red:get(key)
    if not res or res == ngx.null then
        return
    end
    ngx.var.container_url = res
    return
end

-- Save found key to local cache for 5 seconds
cache:set(host, res, 5)  // 在redis取出的映射关系存到redis的cache中避免下次继续连redis操作

ngx.var.container_url = res
Copy after login
可以看出,这个项目分享的内容,并不尽人意,只是简单的提供了一种思路,如何去实现动态的proxy_pass,在这个基础上我们可以进行对url rewrite的扩展。另外,这里的host对应的routeHost 如果只是IP,那样的话,会造成proxy_pass的时候后端的单点,也就是没有应用到upstream,没办法进行负载均衡。但是如果routeHost的值是upstream的话,则,在配置文件中,依然要写死,所以,没有办法做到完全意义上的动态路由。

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了nginx_lua案例分析:动态路由实现,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to 0x80242008 error when installing Windows 11 10.0.22000.100 Solution to 0x80242008 error when installing Windows 11 10.0.22000.100 May 08, 2024 pm 03:50 PM

1. Start the [Start] menu, enter [cmd], right-click [Command Prompt], and select Run as [Administrator]. 2. Enter the following commands in sequence (copy and paste carefully): SCconfigwuauservstart=auto, press Enter SCconfigbitsstart=auto, press Enter SCconfigcryptsvcstart=auto, press Enter SCconfigtrustedinstallerstart=auto, press Enter SCconfigwuauservtype=share, press Enter netstopwuauserv , press enter netstopcryptS

Analyze PHP function bottlenecks and improve execution efficiency Analyze PHP function bottlenecks and improve execution efficiency Apr 23, 2024 pm 03:42 PM

PHP function bottlenecks lead to low performance, which can be solved through the following steps: locate the bottleneck function and use performance analysis tools. Caching results to reduce recalculations. Process tasks in parallel to improve execution efficiency. Optimize string concatenation, use built-in functions instead. Use built-in functions instead of custom functions.

Golang API caching strategy and optimization Golang API caching strategy and optimization May 07, 2024 pm 02:12 PM

The caching strategy in GolangAPI can improve performance and reduce server load. Commonly used strategies are: LRU, LFU, FIFO and TTL. Optimization techniques include selecting appropriate cache storage, hierarchical caching, invalidation management, and monitoring and tuning. In the practical case, the LRU cache is used to optimize the API for obtaining user information from the database. The data can be quickly retrieved from the cache. Otherwise, the cache can be updated after obtaining it from the database.

Caching mechanism and application practice in PHP development Caching mechanism and application practice in PHP development May 09, 2024 pm 01:30 PM

In PHP development, the caching mechanism improves performance by temporarily storing frequently accessed data in memory or disk, thereby reducing the number of database accesses. Cache types mainly include memory, file and database cache. Caching can be implemented in PHP using built-in functions or third-party libraries, such as cache_get() and Memcache. Common practical applications include caching database query results to optimize query performance and caching page output to speed up rendering. The caching mechanism effectively improves website response speed, enhances user experience and reduces server load.

How to use Redis cache in PHP array pagination? How to use Redis cache in PHP array pagination? May 01, 2024 am 10:48 AM

Using Redis cache can greatly optimize the performance of PHP array paging. This can be achieved through the following steps: Install the Redis client. Connect to the Redis server. Create cache data and store each page of data into a Redis hash with the key "page:{page_number}". Get data from cache and avoid expensive operations on large arrays.

How to upgrade Win11 English 21996 to Simplified Chinese 22000_How to upgrade Win11 English 21996 to Simplified Chinese 22000 How to upgrade Win11 English 21996 to Simplified Chinese 22000_How to upgrade Win11 English 21996 to Simplified Chinese 22000 May 08, 2024 pm 05:10 PM

First you need to set the system language to Simplified Chinese display and restart. Of course, if you have changed the display language to Simplified Chinese before, you can just skip this step. Next, start operating the registry, regedit.exe, directly navigate to HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlNlsLanguage in the left navigation bar or the upper address bar, and then modify the InstallLanguage key value and Default key value to 0804 (if you want to change it to English en-us, you need First set the system display language to en-us, restart the system and then change everything to 0409) You must restart the system at this point.

Can navicat connect to redis? Can navicat connect to redis? Apr 23, 2024 pm 05:12 PM

Yes, Navicat can connect to Redis, which allows users to manage keys, view values, execute commands, monitor activity, and diagnose problems. To connect to Redis, select the "Redis" connection type in Navicat and enter the server details.

How to find the update file downloaded by Win11_Share the location of the update file downloaded by Win11 How to find the update file downloaded by Win11_Share the location of the update file downloaded by Win11 May 08, 2024 am 10:34 AM

1. First, double-click the [This PC] icon on the desktop to open it. 2. Then double-click the left mouse button to enter [C drive]. System files will generally be automatically stored in C drive. 3. Then find the [windows] folder in the C drive and double-click to enter. 4. After entering the [windows] folder, find the [SoftwareDistribution] folder. 5. After entering, find the [download] folder, which contains all win11 download and update files. 6. If we want to delete these files, just delete them directly in this folder.

See all articles