Introduction To Nginx



Introduction To Nginx

0 0


introduction-to-nginx


On Github siddontang / introduction-to-nginx

Introduction To Nginx

tangliu@kingsoft.com

Agenda

  • Introduction
  • Architecture
  • Install and Use
  • Configuration
  • Development
  • Tips

Introduction

  • What?

    nginx [engine x] is an HTTP and reverse proxy server, as well as a mail proxy server.

  • Why?

    Performance, Scalability, Availability, Easy

Architecture

  • Prefork
  • Asynchronous: epoll, kqueue, select
  • Modular

Architecture

Install

  • configure with add module
  • make
  • make install

How to Use?

Configuration

Nginx configuration is based on Directives!

Use Nginx is to learn how to use Directives!

Directive Context

  • main
  • event
  • http
  • upstream
  • server
  • location
  • mail

A Simple Example

						
worker_processes  1; 

events { 
    use epoll; 
} 

http { 
	default_type text/html;
	server {
		listen 80;
		server_name localhost;

		location /hello {
			return 200 "hello world";
		}
	} 
}
						
					

Location

How Nginx handle different Request Urls?

Config Different Locations!

location /test {
	return 200 "test1";
}

location ^~ /test/ {
	return 200 "test2";
}

location /test/abc {
	return 200 "test3";
}
					

Location Match Regulation

  • Literal Strings

    location = /test {
    	return 200 "test";
    }
    								
  • Regular Expressions

    location ^~ /test1 {
    	return 200 "test1";
    }
    
    location /test2 {
    	return 200 "test2";
    }
    
    location ~* \.(gif|jpg)$ {
    	return 200 "gif,jpg";
    }
    							

Location Search Regulation

Directives with the "=" prefix that match the query exactly (literal string). If found, searching stops. All remaining directives with conventional strings. If this match used the "^~" prefix, searching stops. Regular expressions, in the order they are defined in the configuration file. If #3 yielded a match, that result is used. Otherwise, the match from #2 is used.

Question?

Inner Implementation for Location?

Directives

Tow thing to known:

  • Inheriting-Hierarchy!
  • Don't Depend on the order!

Inheriting-Hierarchy

						
server {
	add_header Kss-Hello Hello;
	location /test1 {
		return 200;
	}

	location /test2 {
		add_header Kss-Hello "HelloWorld";
		return 200;
	}
}
						
					

Derictive's Order

						
location /test {
	set $hello 'hello';
	echo $hello;

	set $hello 'hello world';
	echo $hello;
}
						
					

What the output?

Derictive's Order

						
location /test {
	deny all;
	return 200;
}
						
					

Return 200 or 403?

Request Phrase

Post Read Server Rewrite Find Config Phase Rewrite Post Rewrite PreAccess Access PostAccess TryFiles Content Log

Development

Nginx is Modular, develop a module looks simple!

But is it really the case?

A very simple nginx module

Hello Nginx!

Openresty

Use lua for nginx development!

Why lua?

  • simple, easy to learn.
  • tiny and powerful.
  • fast.
  • coroutine and synchronization.

Thank you, Agentzh!!!!!!

Now we use openresty to do many things!

Authentication

						
location /test {
	access_by_lua 'auth()';
	proxy_pass http://backend;
}
						
					

Use access_by_lua, we can authenticate whether the request is valid.

If auth successed, proxy_pass can execute.

SubRequest

						
location /test {
	content_by_lua '
		local res = ngx.location.capture("/_test1", {args = {a = 1, b = 2}})
		local body = res.body
		local res1 = ngx.location.capture("/_test2", {args = {a = body}})
		ngx.say(res1.body)
	';
}

location /_test1 {
	internal;
	return 200 "123";
}

location /_test2 {
	internal;
	return 200 "456";
}
						
					

Tips

Reverse Proxy Server

  • proxy_pass
  • fastcgi_pass
						
location /test {
	proxy_pass http://127.0.0.1:8889/;
}
						
					

Cache

  • proxy cache
  • fastcgi cache

Upstream

						
upstream backend {
	server 127.0.0.1:8888;
	server 127.0.0.1:8889;
}

location /test {
	proxy_pass http://backend;
}
						
					

Upstream Load balancer

  • round-robin
  • ip-hash
  • least-conn
  • consistent-hash
  • simple-hash

Rewrite

						
location /home {
	echo "home";
}

location /test {
	echo "test";
}

rewrite rewrite ^/home http://$host/test permanent;
						
					

Rewrite

						
location /home {
	return 301 http://$host/test;
}

location /test {
	echo "test";
}
						
					

Access Control

						
location /test {
	allow 10.20.187.118;
	deny all;
	echo "Hello World";
}

location /test1 {
	deny all;
	allow 10.20.187.118;
	echo "Hello World";
} 
						
					

Anti-Leech

  • valid-referers
  • NginxHttpAccessKeyModule

Thank You!