配置文件结构
简单示例:
user www-data;
worker_processes auto;
worker_cpu_affinity auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
http {
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
mail {
server { }
server { }
}
使用 if 关键字
Module ngx_http_rewrite_module
NGINX not equal to – Stack Overflow
# 判断变量是否匹配字符串
if ($var != CN) {
return 444;
}
if ($var = EN) {
return 200;
}
使用变量
Alphabetical index of variables
reverse proxy – nginx – read custom header from upstream server – Stack Overflow
通过 $http_<header-field> 形式,y 以此引用请求头的字段。比如:
1)$http_user_agent:获取请求头的 User-Agent 字段;
2)$http_x_real_ip:引用请求头的 X-Real-IP 字段;
在字符串中使用环境变量:
1)set $foobar "${foo}bar";
常用工具
配置文件生成工具
digitalocean/nginxconfig.io: ⚙️ NGINX config generator on steroids 💉
演示站点:NGINX Config | DigitalOcean
nginx – Merging variable with string in config file – Server Fault
解析工具
crossplane
https://github.com/nginxinc/crossplane