Add OAuth support for Nginx (nginx-lua)

Source: Internet
Author: User

** In plain text **, we used the Nginx Lua middleware to establish the oau2's authentication and authorization layer. If you want to do the same, read the following documents for automation and benefits.

With the development of SeatGeek over the past few years, we have accumulated many different management interfaces for various tasks. We usually create new modules for new display requirements, such as our own blogs and charts. We also regularly develop internal tools to handle transactions such as deployment, visualization operations, and event processing. In processing these transactions, we use several different interfaces for authentication:

  • Github/Google Oauth

  • Our user system in SeatGeek

  • Basic Authentication

  • Hard-coded Logon

Obviously, it is not standardized in actual application. Multiple authentication systems make it difficult to abstract various databases for access level and general license.

Single System Authentication

We also did some research on how to set up to solve our problem. This prompted the emergence of Odin, which worked well in verifying Google app users. Unfortunately, it requires Apache, and we have joined Nginx and used it as the front-end of Our backend application.

Fortunately, I read the mixlr blog and referenced their Lua application on Nginx:

  • Modify Response Header

  • Rewrite internal request

  • Host access is selectively rejected Based on IP addresses.

The last one looks interesting. It enables a journey to hell in software package management.

Build Nginx that supports Lua

Lua for Nginx is not included in the Nginx core. We often need to build Nginx for OSX for development and testing, and build Nginx for Linux for deployment.

Customize Nginx for OSX

For OSX systems, Homebrew is recommended for package management. There are not many modules enabled in its initial Nginx installation package, which has a good reason:

The key is that NGINX has so many options. If we add them to the initial package, it will be crazy. If we only add some of them, it will force us to add all of them, this will drive us crazy.
-Charlie Sharpsteen, @ sharpie

So we need to build it ourselves. Reasonable Nginx construction can facilitate further expansion. Fortunately, it is very convenient to use Homebrew for package management.

First, we need a workspace:

cd ~mkdir -p srccd src

 

Then, we need to find the initial installation information package. You can get it in any of the following ways:

  • Find the HOMEBREW_PREFIX directory, usually in/usr/local, where find the nginx. rb File

  • Get https://raw.github.com/mxcl/homebrew/master/Library/Formula/nginx.rb from the following address

  • Run the following command: brew cat nginx> nginx. rb

If we execute the brew install./nginx. rb command, it will install Nginx based on the information. Now that we want to completely customize Nginx, We need to rename the information package so that we will not overwrite our custom information when updating through the brew update command:

mv nginx.rb nginx-custom.rbcat nginx-custom.rb | sed 's/class Nginx/class NginxCustom/' >> tmprm nginx-custom.rbmv tmp nginx-custom.rb

 

Now we can add the required modules to the installation information package and start compiling. This is simple. We only need to pass all the modules we need to the brew install command in the form of parameters. The Code is as follows:

# Collects arguments from ARGVdef 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 ARGVdef nginx_modules; collect_modules(/^--include-module-/); end# Get nginx modules that are available on github specified in ARGVdef add_from_github; collect_modules(/^--add-github-module=/); end# Get nginx modules from mdounin's hg repository specified in ARGVdef add_from_mdounin; collect_modules(/^--add-mdounin-module=/); end# Retrieve a repository from githubdef 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 + '/' + repositoryend# Retrieve a tar of a package from mdounindef 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 + '-' + hashend

 

The preceding auxiliary module allows us to specify the desired module and retrieve the module address. Now we need to modify the nginx-custom.rb file so that it contains the names of these modules and retrieve them in the package, near line 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)}" }

 

Now we can compile the customized 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

 

You can easily find the above information package in seatgeek/homebrew-formulae.

Nginx details: click here
Nginx: click here

Recommended reading:

 

Configure and optimize reverse proxy and load balancing in Nginx

 

Nginx load balancing: nginx: [emerg] cocould not build the types_hash

 

Nginx Load Balancing module ngx_http_upstream_module details

 

Nginx + Firebug allows the browser to tell you which Server Load balancer distributes requests

 

Ubuntu install Nginx php5-fpm MySQL (LNMP environment setup)

 

  • 1
  • 2
  • 3
  • 4
  • Next Page

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.