你好,游客 登录 注册 搜索
背景:
阅读新闻

给 Nginx 增加 OAuth 支持(nginx-lua)

[日期:2014-01-08] 来源:oschina.net  作者:Garfielt, LeoXu, DrZ, BoydWang, 学习者8, FreeZ [字体: ]

**白话不表**我们使用Nginx的Lua中间件建立了OAuth2认证和授权层。如果你也有此打算,阅读下面的文档,实现自动化并获得收益。

SeatGeek在过去几年中取得了发展,我们已经积累了不少针对各种任务的不同管理接口。我们通常为新的展示需求创建新模块,比如我们自己的博客、图表等。我们还定期开发内部工具来处理诸如部署、可视化操作及事件处理等事务。在处理这些事务中,我们使用了几个不同的接口来认证:

  • Github/Google Oauth

  • 我们SeatGeek内部的用户系统

  • 基本认证

  • 硬编码登录

显然,实际应用中很不规范。多个认证系统使得难以对用于访问级别和通用许可的各种数据库进行抽象。

单系统认证

我们也做了一些关于如何设置将解决我们问题的研究。这促使了Odin的出现,它在验证谷歌应用的用户方面工作的很好。不幸的是它需要使用Apache,而我们已和Nginx结为连理并把它作为我们的后端应用的前端。

幸运的是,我看了mixlr的博客并引用了他们Lua在Nginx上的应用:

  • 修改响应头

  • 重写内部请求

  • 选择性地基于IP拒绝主机访问

最后一条看起来很有趣。它开启了软件包管理的地狱之旅。

构建支持Lua的Nginx

Lua for Nginx没有被包含在Nginx的核心中,我们经常要为OSX构建Nginx用于开发测试,为Linux构建用于部署。

为OSX定制Nginx

对于OSX系统,我推荐使用Homebrew进行包管理。它初始的Nginx安装包启用的模块不多,这有非常好的理由:

关键在于NGINX有着如此之多的选项,如果把它们都加入初始包那一定是疯了,如果我们只把其中一些加入其中就会迫使我们把所有都加入,这会让我们疯掉的。
- Charlie Sharpsteen, @sharpie

所以我们需要自己构建。合理地构建Nginx可以方便我们以后继续扩展。幸运的是,使用Homebrew进行包管理十分方便快捷。

我们首先需要一个工作空间:

cd ~
mkdir -p src
cd src

 

之后,我们需要找到初始安装信息包。你可以通过下面任何一种方式得到它:

  • 找到HOMEBREW_PREFIX目录,通常在/usr/local下,在其中找到nginx.rb文件

  • 从下列地址取得https://raw.github.com/mxcl/homebrew/master/Library/Formula/nginx.rb

  • 使用如下命令 brew cat nginx > nginx.rb

此时如果我们执行brew install ./nginx.rb命令, 它会依据其中的信息安装Nginx。既然现在我们要完全定制Nginx,我们要重命名信息包,这样之后通过brew update命令进行更新的时候就不会覆盖我们自定义的了:

mv nginx.rb nginx-custom.rb
cat nginx-custom.rb | sed 's/class Nginx/class NginxCustom/' >> tmp
rm nginx-custom.rb
mv tmp nginx-custom.rb

 

我们现在可以将我们需要的模块加入安装信息包中并开始编译了。这很简单,我们只要将所有我们需要的模块以参数形式传给brew install命令,代码如下:

# Collects arguments from ARGV
def collect_modules regex=nil
    ARGV.select { |arg| arg.match(regex) != nil }.collect { |arg| arg.gsub(regex, '') }
end

# Get nginx modules that are not compiled in by default specified in ARGV
def nginx_modules; collect_modules(/^--include-module-/); end

# Get nginx modules that are available on github specified in ARGV
def add_from_github; collect_modules(/^--add-github-module=/); end

# Get nginx modules from mdounin's hg repository specified in ARGV
def add_from_mdounin; collect_modules(/^--add-mdounin-module=/); end

# Retrieve a repository from github
def fetch_from_github name
    name, repository = name.split('/')
    raise "You must specify a repository name for github modules" if repository.nil?

    puts "- adding #{repository} from github..."
    `git clone -q git://github.com/#{name}/#{repository} modules/#{name}/#{repository}`
    path = Dir.pwd + '/modules/' + name + '/' + repository
end

# Retrieve a tar of a package from mdounin
def fetch_from_mdounin name
    name, hash = name.split('#')
    raise "You must specify a commit sha for mdounin modules" if hash.nil?

    puts "- adding #{name} from mdounin..."
    `mkdir -p modules/mdounin && cd $_ ; curl -s -O http://mdounin.ru/hg/#{name}/archive/#{hash}.tar.gz; tar -zxf #{hash}.tar.gz`
    path = Dir.pwd + '/modules/mdounin/' + name + '-' + hash
end

 

上面这个辅助模块可以让我们指定想要的模块并检索模块的地址。现在,我们需要修改nginx-custom.rb文件,使之包含这些模块的名字并在包中检索它们,在58行附近:

nginx_modules.each { |name| args << "--with-#{name}"; puts "- adding #{name} module" }
add_from_github.each { |name| args <<  "--add-module=#{fetch_from_github(name)}" }
add_from_mdounin.each { |name| args <<  "--add-module=#{fetch_from_mdounin(name)}" }

 

现在我们可以编译我们重新定制的nginx了:

brew install ./nginx-custom.rb \
    --add-github-module=agentzh/chunkin-nginx-module \
    --include-module-http_gzip_static_module \
    --add-mdounin-module=ngx_http_auth_request_module#a29d74804ff1

 

你可以方便地在seatgeek/homebrew-formulae找到以上信息包。

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

推荐阅读

 

Nginx实现反向代理和负载均衡的配置及优化 http://www.linuxidc.com/Linux/2013-11/92909.htm

 

Nginx做负载均衡报:nginx: [emerg] could not build the types_hash http://www.linuxidc.com/Linux/2013-10/92063.htm

 

Nginx 负载均衡模块 ngx_http_upstream_module 详述 http://www.linuxidc.com/Linux/2013-10/91907.htm

 

Nginx+Firebug 让浏览器告诉你负载均衡将请求分到了哪台服务器 http://www.linuxidc.com/Linux/2013-10/91824.htm

 

Ubuntu安装Nginx php5-fpm MySQL(LNMP环境搭建) http://www.linuxidc.com/Linux/2012-10/72458.htm

 

linux
相关资讯       OAuth  Nginx Lua 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款