tangliu@kingsoft.com
What?
nginx [engine x] is an HTTP and reverse proxy server, as well as a mail proxy server.
Why?
Performance, Scalability, Availability, Easy
How to Use?
Nginx configuration is based on Directives!
Use Nginx is to learn how to use Directives!
worker_processes 1;
events {
use epoll;
}
http {
default_type text/html;
server {
listen 80;
server_name localhost;
location /hello {
return 200 "hello world";
}
}
}
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";
}
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";
}
Tow thing to known:
server {
add_header Kss-Hello Hello;
location /test1 {
return 200;
}
location /test2 {
add_header Kss-Hello "HelloWorld";
return 200;
}
}
location /test {
set $hello 'hello';
echo $hello;
set $hello 'hello world';
echo $hello;
}
What the output?
location /test {
deny all;
return 200;
}
Return 200 or 403?
Nginx is Modular, develop a module looks simple!
But is it really the case?
Use lua for nginx development!
Why lua?
Thank you, Agentzh!!!!!!
Now we use openresty to do many things!
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.
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";
}
location /test {
proxy_pass http://127.0.0.1:8889/;
}
upstream backend {
server 127.0.0.1:8888;
server 127.0.0.1:8889;
}
location /test {
proxy_pass http://backend;
}
location /home {
echo "home";
}
location /test {
echo "test";
}
rewrite rewrite ^/home http://$host/test permanent;
location /home {
return 301 http://$host/test;
}
location /test {
echo "test";
}
location /test {
allow 10.20.187.118;
deny all;
echo "Hello World";
}
location /test1 {
deny all;
allow 10.20.187.118;
echo "Hello World";
}