Skip to content

Nginx Configuration

Hunter edited this page Sep 2, 2021 · 9 revisions

vod

cd /tmp
git clone https://github.com/nytimes/nginx-vod-module-docker.git
mv examples /data/vod

vim example
docker pull nytimes/nginx-vod-module

cd /data/vod
docker run -d -p 8030:80 \
    -v $PWD/videos:/opt/static/videos \
    -v $PWD/nginx.conf:/usr/local/nginx/conf/nginx.conf \
    nytimes/nginx-vod-module

nginx reverse proxy

# /etc/nginx/sites-enabled/https.conf
upstream vod {
  server 127.0.0.1:8030;
}

server {
    ...
    location /hls/ {
        proxy_connect_timeout 20s;
        proxy_send_timeout    20s;
        proxy_read_timeout    20s;
        proxy_pass   http://vod/hls/;
        proxy_redirect off;
    }
    location /dash/ {
        proxy_connect_timeout 20s;
        proxy_send_timeout    20s;
        proxy_read_timeout    20s;
        proxy_pass   http://vod/dash/;
        proxy_redirect off;
    }
    location /thumb/ {
        proxy_connect_timeout 20s;
        proxy_send_timeout    20s;
        proxy_read_timeout    20s;
        proxy_pass   http://vod/thumb/;
        proxy_redirect off;
    }
    ...
}

with CORS

location /pp {
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Headers' '*';
        add_header 'Access-Control-Allow-Methods' 'GET';
        add_header 'Access-Control-Max-Age' 86400;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
    }
    # add_header "X-DEBUG" $args;
    # return 200;

    resolver 8.8.8.8;
    proxy_ssl_session_reuse off;
    proxy_set_header Origin $args;
    proxy_pass $args;
    proxy_hide_header Access-Control-Allow-Origin;
    add_header Access-Control-Allow-Origin *;
}

log response body

sudo apt-get install libnginx-mod-http-lua
    log_format bodylog escape=json '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent '
        '"$http_referer" "$http_user_agent" $request_time '
        '\n\n"$req_headers" \n"$req_body" \n>"$resp_body"';

server {
    access_log  /data/log/nginx/vapi_access.log bodylog;

    lua_need_request_body on;

    set $resp_body "";
    set $req_body "";
    set $req_headers "";

    client_body_buffer_size 16k;
    client_max_body_size 16k;

    rewrite_by_lua_block {
        local req_headers = "Headers: ";
        ngx.var.req_body = ngx.req.get_body_data();
        local h, err = ngx.req.get_headers()
        for k, v in pairs(h) do
            req_headers = req_headers .. k .. ": " .. v .. "\n";
        end
        ngx.var.req_headers = req_headers;
    }

    body_filter_by_lua '
        local resp_body = string.sub(ngx.arg[1], 1, 1000)
        ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
        if ngx.arg[2] then
            ngx.var.resp_body = ngx.ctx.buffered
        end
    ';
}

build

sudo apt-get update
sudo apt-get install libpcre3 libpcre3-dev
sudo apt-get install zlib1g-dev

php7.2-fpm

location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    set $path_info $fastcgi_path_info;
    fastcgi_param PATH_INFO $path_info;
    fastcgi_param XBTW_RUNTIME_ENVIRONMENT dev;
    fastcgi_param RUNTIME_ENVIRONMENT dev;

    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}

Proxy

location /ccapi/ {
    rewrite ^/ccapi/(.*) /$1 break;
    proxy_pass https://min-api.cryptocompare.com;
}
  • zimg
location ~ "^/([0-9a-f]{32})_(80|150|200)x(80|150|200)(.jpg|.png|.gif)?" {
    proxy_pass http://127.0.0.1:9869/$1?w=$2&h=$3;
}

location ~ "^/([0-9a-f]{32})(\??.*)" {
    proxy_pass http://127.0.0.1:9869/$1;
}

location ~ "^/upload" {
    proxy_pass http://127.0.0.1:9869;
}

Direct Response

location ~* ./some/path/end$ {
   default_type application/json;
   return 200 '{"Status":0}';
}

location ~* ./another/path/.*$ {
   default_type application/json;
   return 200 '{"Status":0}';
}

Multiple conditions

location ^~ /mycdn {
     if ($remote_addr = 127.0.0.1) { set $stat OK;}
     if ($remote_addr = 8.8.8.8) { set $stat OK;}
     if ($remote_addr = 8.8.4.4) { set $stat OK;}
     if ($stat != OK) {
         set $stat OUT;
     }
     if ($request_uri ~ ^/mycdn/myfunc/themes-resource/(.*)/banner.png$) {
         set $stat "${stat}1";
         set $theme_id $1;
     }
     if ($stat = OUT1) {
         rewrite ^ http://mycdn.example.com/myfunc/themes/$theme_id/banner.png;
     }
     if ($request_uri ~ ^/mycdn/myfunc/themes-beta/(.*)/(.*)$) {
         set $stat "${stat}2";
         set $theme_id $1;
         set $theme_file $2;
     }
     if ($stat = OUT2) {
         rewrite ^ http://mycdn.example.com/myfunc/themes/$theme_id/$theme_file;
     }
     if ($stat = OUT) {
         return 404;
     }
   alias /data/mycdn;
}
Clone this wiki locally