nginx(Engine X) 高性能的web服务器
排名指路:https://w3techs.com/technologies/cross/web_server/ranking
官网指路:http://nginx.org/en/
轻量级: CPU,内存占用少(工作模式:进程池 + 单线程 => 消除了进程,线程切换的成本) One master and several worker processes; worker processes run under an unprivileged user; (在进程之上,master进程用来管理进程池,监控进程,自动恢复发生异常的worker,保持进程池的稳定和服务能力) cite: https://time.geekbang.org/column/article/117492
nginx - 作为web代理服务器 - 负载均衡,流量切换 - lua的脚本支持
1 | - 对session和cookie的处理较弱 |
- 可以作为流量网关,nginx层流量代理,负载均衡到Gateway做业务层的转发处理
nginx 怎么找
- ps -ef | grep nginx (ps:显示系统进程)
- whereis nginx 软件安装路径(whereis – locate programs;The whereis utility checks the standard binary directories for the specified programs, printing out the paths of any it finds.The path searched is the string returned by the sysctl(8) utility for the ``user.cs_path’’ string)
- which nginx 运行文件所在路径(which – locate a program file in the user’s path;The which utility takes a list of command names and searches the path for each executable file that would be run had these commands actually been invoked.)
- cd / && find - name ‘nginx.conf’
nginx.conf => 基本的
vhost => 应用的
nginx 403
1 | chmod -R 777 |
nginx root
vim nginx.conf
把 user 改成 root,即可;
firekylin nginx
1 | ln -s /root/firekylin/nginx.conf /etc/nginx/conf.d/firekylin.conf |
nginx.conf里面:
1 | ln -s是一个Linux/Unix命令,用于创建软链接(Symbolic Link或称为Symlink)。软链接是一种类似于Windows系统中快捷方式的文件,它指向另一个文件或目录。其中,源文件或目录可以是绝对路径或相对路径,也可以是一个完整的URL。软链接可以跨越不同的文件系统,而硬链接只能在同一文件系统中使用。 |
ln -s source_file link_file
1 |
|
~ #波浪线表示执行一个正则匹配,区分大小写 # location ~ /static/ { # etag on; # expires max; # }
把
# etag on; # expires max;
都注释了,还是403
把location ~ /static/ {}
也注释了,就生效了
文档指路:http://nginx.org/en/docs/beginners_guide.html
kill nginx 服务
nohup = No HangUP
1 | ps -ef|grep nginx |
前端history路由模式
1 | location / { |
正则匹配
http server优先级
线上出过一个问题,之前一直没生效的CXP的配置生效了;原因是server优先级高于http,修改了server里面的一些跨域的配置;
应用场景
本地起nginx代理解决资源跨域或者连本地服务调试(嵌入移动端的页面)问题
1 | nginx -v # 查看本机是否安装nginx, 如果没有安装homebrew安装下; |
tengine
- http://tengine.taobao.org/
旧项目,接口server
Server: Tengine/2.2.3
新项目:接口server
Server: openresty/1.15.8.3
301
1 | curl -i 'https://xxxx/qqq/thursday/xxxx' |
- 因为测试环境,前端多个项目配置了一个规则,规则这么配置的
1
2
3
4
5
6
7
8
9
10location ~* ^/(qqq|xxx|xxxe|)/(env1|env2|env3|env4)/?(\w*) {
root /neworiental/latest/;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Cache-Control' 'no-cache';
index index.html;
try_files $uri $uri/ /$1/$2/$3/index.html;
} - 这个时候,如果https://xxxx/qqq/thursday/xxxx 后面不加“/”,不是 https://xxxx/qqq/thursday/xxxx/ 的话,则会被重定向,因为nginx匹配规则有问题
配置实践
- 入口 html 文件设置 no-cache,其他资源文件设置 max-age 的缓存方式
- best-practice