Nginx 入门

2020-06-22  本文已影响0人  Secret_Sun

最近看了蛮多 Nginx 的书、文档等,适当的总结下一些基础扫盲知识。
刚上班就晓得有 Apache 这个东西,也维护过一段时间,后面就默默的转 DBA 了,从而其实错过了 Nginx ,最近想着扫扫盲,学学、玩玩、看看、补补 。

Nginx、OpenRestry、Kong

上面三点其实就能很明显的发现,讲到底还是 Nginx。

OpenResty

OpenResty 以 Nginx 为核心,集成打包了众多侧重于高性能 Web 开发的外围组件,它既是一个Web Server,也是一个成熟完善的开发套件。OpenResty 基于Nginx 和 Lua/LuaJIT,充分利用了两者的优势,能够无阻塞地处理海量并发连接,任意操纵 HTTP/TCP/UDP 数据流,而且功能代码不需要编译,可以就地修改脚本并运行,简化了开发流程,加快了开发和调试的速度,同时也缩短了开发周期,在如今这个快节奏的时代里弥足珍贵。

简易配置

worker_processes  auto;
worker_cpu_affinity auto;
worker_priority -10;

error_log /data/nginx/logs/error.log;
error_log /data/nginx/logs/error.log notice;
error_log /data/nginx/logs/error.log info;

pid /data/nginx/nginx.pid;

events {
    worker_connections  1024;
}

stream {
    upstream mysqld {
        server xxxxx:3306;
    }
    server {
        listen 23430;
        proxy_connect_timeout 1s;
        proxy_timeout 3s;
        proxy_pass mysqld;
    }
}

http {
    lua_shared_dict prometheus_metrics 10M;
    lua_package_path '/etc/openresty/conf.d/lua/?.lua;;';
    init_by_lua_block {
      prometheus = require("prometheus").init("prometheus_metrics")
      metric_requests = prometheus:counter(
        "nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
      metric_latency = prometheus:histogram(
        "nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
      metric_connections = prometheus:gauge(
        "nginx_http_connections", "Number of HTTP connections", {"state"})
    }
    log_by_lua_block {
      metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
      metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
    }

    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /data/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    server {
        listen 23431;
        access_log off;
        # Allow all RFC 1918 address blocks
        # allow 10.0.0.0/8;
        # allow 172.16.0.0/12;
        # allow 192.168.0.0/16;
        # deny all;
        location /nginx_status {
            stub_status;
        }
    }


    server {
        listen 23432;
        access_log off;
        # Allow all RFC 1918 address blocks
        # allow 10.0.0.0/8;
        # allow 172.16.0.0/12;
        # allow 192.168.0.0/16;
        # deny all;
        location /metrics {
            content_by_lua_block {
                metric_connections:set(ngx.var.connections_active, {"active"})
                metric_connections:set(ngx.var.connections_reading, {"reading"})
                metric_connections:set(ngx.var.connections_writing, {"writing"})
                metric_connections:set(ngx.var.connections_waiting, {"waiting"})
                prometheus:collect()
            }
        }
    }


    server {
        listen       23433;
        server_name  Nginx;

        #charset koi8-r;

        access_log  /data/nginx/logs/host.access.log  main;

       location / {
           default_type text/html;
           content_by_lua 'ngx.say("<p>hello, world</p>")';
       }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

       location /baidu {
           rewrite ^/(.*) http://www.baidu.com;
           root html;
           index index.html;
       }

}

日常覆盖场景

Location & Rewrite

优先级规则

301 & 302 跳转

基于 Lua 可以让 nginx 做更多的事情,ng 本身还是一个插件性的 web server,完全可以基于服务场景来进行二次的插件开发「前提有人会 C++」,本人的理解是普通公司的话 OpenResty 已经可以满足绝大部分场景,而且 Lua 本身也不难,运维基本上都应该适当学习很快就可以上手的一个语言。「sysbench 里面不就是有好多 Lua 」,此文档为个人总结摘录,为了一些技术知识的个人总结为主 「入门扫盲阶段」。

上一篇下一篇

猜你喜欢

热点阅读