NGINX Unit介绍
472 次浏览
发表于 2021-03-07 11:21

介绍

NGINX Unit是一个多语言动态应用服务器,同样支持代理和静态文件。
是由NGINX作者Igor Sysoev亲自设计带领团队从零实现的高性能纯C服务器。
Unit的使用对开发者和运维非常友好。

关键特性:

灵活

用HTTP协议的RESTful JSON API更新任意粒度配置
极其强大和易用的路由管理请求
支持所有主流语言:GO, Python, JAVA, Perl, PHP, NodeJS, Ruby
支持负载均衡的代理
支持静态资源服务

性能

高并发:使用异步事件处理模型
低内存:使用连接和内存复用
每个应用语言的进程管理支持动态和静态分配
对Java, Perl, Python, Ruby支持原生多线程处理

安全和稳定性

处理请求的进程以非特权方式运行
进程间完全独立
不同的应用完全相互隔离
支持用容器化方式对应用程序进行namespace和file system隔离
支持SSL/TLS

社区和文档

容易上手
保持一直活跃的开发状态: https://github.com/nginx/unit/
官方一直更新使用文档:https://unit.nginx.org/

完整示例:

{
    "certificates": {
        "bundle": {
            "key": "RSA (4096 bits)",
            "chain": [
                {
                    "subject": {
                        "common_name": "example.com",
                        "alt_names": [
                            "example.com",
                            "www.example.com"
                        ],

                        "country": "US",
                        "state_or_province": "CA",
                        "organization": "Acme, Inc."
                    },

                    "issuer": {
                        "common_name": "intermediate.ca.example.com",
                        "country": "US",
                        "state_or_province": "CA",
                        "organization": "Acme Certification Authority"
                    },

                    "validity": {
                        "since": "Sep 18 19:46:19 2018 GMT",
                        "until": "Jun 15 19:46:19 2021 GMT"
                    }
                },

                {
                    "subject": {
                        "common_name": "intermediate.ca.example.com",
                        "country": "US",
                        "state_or_province": "CA",
                        "organization": "Acme Certification Authority"
                    },

                    "issuer": {
                        "common_name": "root.ca.example.com",
                        "country": "US",
                        "state_or_province": "CA",
                        "organization": "Acme Root Certification Authority"
                    },

                    "validity": {
                        "since": "Feb 22 22:45:55 2016 GMT",
                        "until": "Feb 21 22:45:55 2019 GMT"
                    }
                }
            ]
        }
    },

    "config": {
        "settings": {
            "http": {
                "header_read_timeout": 10,
                "body_read_timeout": 10,
                "send_timeout": 10,
                "idle_timeout": 120,
                "max_body_size": 6291456,
                "static": {
                    "mime_types": {
                        "text/plain": [
                             ".log",
                             "README",
                             "CHANGES"
                        ]
                    }
                },
                "discard_unsafe_fields": false
            }
        },

        "listeners": {
            "*:8000": {
                "pass": "routes",
                "tls": {
                    "certificate": "bundle"
                }
            },

            "127.0.0.1:8001": {
                "pass": "applications/drive"
            },

            "*:8080": {
                "pass": "upstreams/rr-lb"
            }
        },

        "routes": [
            {
                "match": {
                    "uri": "/admin/*",
                    "scheme": "https",
                    "arguments": {
                        "mode": "strict",
                        "access": "!raw"
                    },

                    "cookies": {
                        "user_role": "admin"
                    }
                },

                "action": {
                    "pass": "applications/cms"
                }
            },
            {
                "match": {
                    "host": "admin.emea-*.*.example.com",
                    "source": "*:8000-9000"
                },

                "action": {
                    "pass": "applications/blogs/admin"
                }
            },
            {
                "match": {
                    "host": ["blog.example.com", "blog.*.org"],
                    "source": "*:8000-9000"
                },

                "action": {
                    "pass": "applications/blogs/core"
                }
            },
            {
                "match": {
                    "host": "example.com",
                    "source": "127.0.0.0-127.0.0.255:8080-8090",
                    "uri": "/chat/*"
                },

                "action": {
                    "pass": "applications/chat"
                }
            },
            {
                "match": {
                    "host": "example.com",
                    "source": [
                        "10.0.0.0/7:1000",
                        "10.0.0.0/32:8080-8090"
                    ]
                },

                "action": {
                    "pass": "applications/store"
                }
            },
            {
                "match": {
                    "host": "wiki.example.com"
                },

                "action": {
                    "pass": "applications/wiki"
                }
            },
            {
                "match": {
                     "uri": "/legacy/*"
                },

                "action": {
                    "return": 301,
                    "location": "https://legacy.example.com"
                }
            },
            {
                "match": {
                    "scheme": "http"
                },

                "action": {
                    "proxy": "http://127.0.0.1:8080"
                }
            },
            {
                "action": {
                    "share": "/www/static/",
                    "fallback": {
                        "proxy": "http://127.0.0.1:9000"
                    }
                }
            }
        ],

        "applications": {
            "blogs": {
                "type": "php",
                "targets": {
                    "admin": {
                        "root": "/www/blogs/admin/",
                        "script": "index.php"
                    },

                    "core" : {
                        "root": "/www/blogs/scripts/"
                    }
                },

                "limits": {
                    "timeout": 10,
                    "requests": 1000
                },

                "options": {
                    "file": "/etc/php.ini",
                    "admin": {
                        "memory_limit": "256M",
                        "variables_order": "EGPCS",
                        "expose_php": "0"
                    },

                    "user": {
                        "display_errors": "0"
                    }
                },

                "processes": 4
            },

            "chat": {
                "type": "external",
                "executable": "bin/chat_app",
                "group": "www-chat",
                "user": "www-chat",
                "working_directory": "/www/chat/",
                "isolation": {
                    "namespaces": {
                        "cgroup": false,
                        "credential": true,
                        "mount": false,
                        "network": false,
                        "pid": false,
                        "uname": false
                    },

                    "uidmap": [
                        {
                            "host": 1000,
                            "container": 0,
                            "size": 1000
                        }
                    ],

                    "gidmap": [
                        {
                            "host": 1000,
                            "container": 0,
                            "size": 1000
                        }
                    ],

                    "automount": {
                        "language_deps": false,
                        "procfs": false,
                        "tmpfs": false
                    }
                }
            },

            "cms": {
                "type": "ruby",
                "script": "/www/cms/main.ru",
                "working_directory": "/www/cms/"
            },

            "drive": {
                "type": "perl",
                "script": "app.psgi",
                "threads": 2,
                "thread_stack_size": 4096,
                "working_directory": "/www/drive/",
                "processes": {
                    "max": 10,
                    "spare": 5,
                    "idle_timeout": 20
                }
            },

            "store": {
                "type": "java",
                "webapp": "/www/store/store.war",
                "classpath": ["/www/store/lib/store-2.0.0.jar"],
                "options": ["-Dlog_path=/var/log/store.log"]
            },

            "wiki": {
                "type": "python",
                "module": "asgi",
                "protocol": "asgi",
                "callable": "app",
                "environment": {
                    "DJANGO_SETTINGS_MODULE": "wiki.settings.prod",
                    "DB_ENGINE": "django.db.backends.postgresql",
                    "DB_NAME": "wiki",
                    "DB_HOST": "127.0.0.1",
                    "DB_PORT": "5432"
                },

                "path": "/www/wiki/",
                "processes": 10
            }
        },

        "upstreams": {
            "rr-lb": {
                "servers": {
                    "192.168.1.100:8080": { },
                    "192.168.1.101:8080": {
                        "weight": 2
                    }
                }
            }
        },

        "access_log": "/var/log/access.log"
    }
}
如果您觉得不错,就打赏支持一下吧〜
已有 2 人进行打赏
  • 熊平
  • SamWANG
点击标签,发现更多精彩
发表评论
发表者

洪志道

NGINX官方团队成员

  • 11

    文章

  • 1

    关注

  • 40

    粉丝

活动推荐
版权所有©F5 Networks,Inc.保留所有权利。京ICP备16013763号-5