// nginx
brew install nginx
//启动
brew services start nginx
brew services restart nginx
//四、停止
brew services stop nginx
//五、查看
cat usr/local/etc/nginx/nginx.conf
//六、编辑
vi usr/local/etc/nginx/nginx.conf
nginx 命令
下载
c10k问题
命令行
~ nginx -h
nginx version: nginx/1.19.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /usr/local/nginx/)
-c filename : set configuration file (default: /usr/local/etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file
~ nginx -t
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:2
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
vim /usr/local/etc/nginx/nginx.conf
#开启服务
brew services start nginx
➜ ~ ps -ef |grep nginx
501 452 1 0 2:57下午 ?? 0:00.02 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
501 617 452 0 2:57下午 ?? 0:00.03 nginx: worker process
501 11777 9508 0 5:39下午 ttys000 0:00.01 nginx
501 12564 9508 0 5:52下午 ttys000 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx
nginx 配置
nginx.conf 一共有三个部分组成:全局块、events块和http块。
HTTP服务器
nginx的既可以做静态代理服务器,也可以做动态代理服务器,静态代理服务器配置如下:
server{
listen 80;
server_name ip/域名;
client_max_body_size 1024M;
location / {
root /usr/local;
index index.html;
}
}
如上可以表示为,发送http请求到nginx代理服务器,默认到真实服务器的/usr/local下的index.html静态网页。
动态请求会通过nginx进行转发,示例如下:
server {
listen 80;
server_name ip/域名;
charset utf-8;
location / {
root /usr/local/efarm;
index index.html;
}
location /api {
proxy_pass http:127.0.0.1:8088 proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
如上可以表示为,当发送http请求到nginx代理服务器时,默认到/usr/local/efarm下寻找index.html网页,需要请求动态接口,则通过nginx转发给真实服务器8088端口的启动项目。
反向代理
反向代理(Reverse Proxy)是现今网络中一种非常重要的技术,它位于客户端和真正的服务器(即所谓的后端)之间,接受客户端的请求并转发给后端,然后把后端的处理结果返回给客户端。
一般上线的项目,出于安全性的考虑,是不允许外网直接访问的,这时候nginx的反向代理功能就起到了关键作用。通常表现为,在生产服务器上部署项目和代理服务器,客户端不能直接访问生产服务器,需要通过nginx接收客户端传来的请求,然后转发给生产服务器,再将服务器的回应发送给客户端。这个闭合过程nginx充当一个中转站,在此过程中,用户不需要配置任何代理ip和端口,或者说客户端根本就不知道自己访问的是真实的服务器还是代理服务器,这样能有效的保证内网的安全 。
简单配置如下:
server {
listen 80;
server_name 域名/ip;
charset utf-8;
location / {
proxy_pass http://127.0.0.1:21010;
proxy_set_header Host $host;
proxy_set_header X-real-ip $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
负载均衡
当一台服务器满足不了访问量的时候,需要用其他的服务器进行分流,nginx可以很好的充当引流人,nginx负载均衡的核心配置如下:
upstream test {
#服务器1
server ip+端口;
#服务器2
server ip+端口;
}</pre>
负载均衡—权重(weight)
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。例如:
upstream test {
#服务器1
server ip+端口 weight=3;
#服务器2
server ip+端口 weight=7;
}
以上表现为,访问服务器1的概率为30%,访问服务器2的概率为70%
负载均衡—ip绑定(ip_hash)
多台服务器协同工作,最常出现的问题是session的保持,总不能访问一次不同的服务器就重新登陆一次吧,ip_hash的出现即根据用户请求的ip来寻找其访问过的服务器,让用户一直访问同一个服务器,示例如下:
upstream test {
ip_hash;
#服务器1
server ip+端口 weight=3;
#服务器2
server ip+端口 weight=7;
}
负载均衡—优先分配(fair)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。nginx默认不支持fair算法,需要安装upstream_fair模块,示例如下:
upstream test {
fair;
ip_hash;
#服务器1
server ip+端口 weight=3;
#服务器2
server ip+端口 weight=7;
}
nginx 开发准备
目录结构
各个模块的功能
#include <ngx_config.h> # 各种配置参数,core目录
#include <ngx_core.h> # 核心数据结构定义,core目录
#include <ngx_http.h> # 开发http模块需要包含此文件
nginx config 在objs/nginx_auto_config.h
#define NGX_CONFIGURE " --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/bin/nginx --with-cc-opt='-I/usr/local/opt/pcre/include -I/usr/local/opt/openssl/include' --with-ld-opt='-L/usr/local/opt/pcre/lib -L/usr/local/opt/openssl/lib' --conf-path=/usr/local/etc/nginx/nginx.conf --pid-path=/usr/local/var/run/nginx.pid --lock-path=/usr/local/var/run/nginx.lock --http-client-body-temp-path=/usr/local/var/run/nginx/client_body_temp --http-proxy-temp-path=/usr/local/var/run/nginx/proxy_temp --http-fastcgi-temp-path=/usr/local/var/run/nginx/fastcgi_temp --http-uwsgi-temp-path=/usr/local/var/run/nginx/uwsgi_temp --http-scgi-temp-path=/usr/local/var/run/nginx/scgi_temp --http-log-path=/usr/local/var/log/nginx/access.log --error-log-path=/usr/local/var/log/nginx/error.log --with-debug --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-ipv6 --with-mail --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module"
nginx编译器
#ifndef NGX_COMPILER
#define NGX_COMPILER "clang 11.0.3 (clang-1103.0.32.62)"
#endif
nginx 状态值 ngx_core.h
#define NGX_OK 0
#define NGX_ERROR -1
#define NGX_AGAIN -2
#define NGX_BUSY -3
#define NGX_DONE -4
#define NGX_DECLINED -5
#define NGX_ABORT -6
红黑树
红黑树是一种自平衡二叉树,元素依据某种规则自动排序(通常是整数升序),不仅可以使用二分法快速查找,而且插入和删除操作的效率也很高,用途非常广泛。
红黑树性质
- 节点是红色或黑色。
- 根节点是黑色。
- 每个叶子节点都是黑色的空节点(NIL节点)。
- 每个红色节点的两个子节点都是黑色。(从每个叶子到根的所有路径上不能有两个连续的红色节点)
- 从任一节点到其每个叶子的所有路径都包含相同数目的黑色节点。
# ngx_rbtree.h
typedef struct ngx_rbtree_node_s ngx_rbtree_node_t;
struct ngx_rbtree_node_s {
ngx_rbtree_key_t key;
ngx_rbtree_node_t *left;
ngx_rbtree_node_t *right;
ngx_rbtree_node_t *parent;
u_char color;
u_char data;
};
# ngx_rbtree_t定义了红黑树:
typedef struct ngx_rbtree_s ngx_rbtree_t;
typedef void (*ngx_rbtree_insert_pt) (ngx_rbtree_node_t *root,
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
struct ngx_rbtree_s {
ngx_rbtree_node_t *root;
ngx_rbtree_node_t *sentinel;
ngx_rbtree_insert_pt insert;
};
# ngx_string.h
typedef struct {
ngx_rbtree_node_t node;
ngx_str_t str;
} ngx_str_node_t;
ngx_rbtree_t有三个成员,分别是:
■ root :红黑树的根节点;
■ sentinel :红黑树的“哨兵”节点,用于简化内部算法实现,相当于NULL;
■ insert :节点的插入方法,允许对特殊节点定制特殊插入方法。
其它参考
MEAN是一个Javascript平台的现代Web开发框架总称,它是MongoDB + Express +AngularJS + NodeJS 四个框架的第一个字母组合。它与传统LAMP一样是一种全套开发工具的简称(如下图所示)。











网友评论