此篇文章用于记录Nginx使用方面的说明
安装Nginx
源码安装
安装编译环境
到Nginx官网下载源码
wget -O nginx.tar.gz https://nginx.org/download/nginx-1.23.4.tar.gz && tar -xzf nginx.tar.gz && rm nginx.tar.gz && mv nginx-* nginx && cd nginx
设置编译参数
输入以下命令可以查看具体的编译参数
常用的编译参数
使用systemctl管理nginx
nginx.service文件编写
[Unit]
Description=nginx - high performance web server.
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/usr/local/nginx/sbin/nginx -s reopen
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
通过Apt安装Nginx
通过该方案安装的Nginx默认目录为 /etc/nginx
通过LNMP管理Nginx
LNMP的具体介绍参考官网: https://lnmp.org/
以下命令表示只安装Nginx
LNMP可以通过修改 /path/to/lnmp1.9/lnmp.conf 的nginx相关字段来修改编译参数
使用寄巧(技巧)
本人目前的nginx的编译参数如下
./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-http_gzip_static_module --with-http_sub_module --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_ssl_preread_module --with-http_realip_module
nginx.conf
conf.d/template.conf
Nginx Location 规则
Location的格式如下
location [modifier] [URI] {
...
...
}
Modifier
Nginx对不同的Modifier有不同的优先级,该表按照匹配的优先级排序
Modifier | 名称 | 说明 | Example |
---|---|---|---|
= | 精确匹配 | 最高优先级,路径必须严格等于时才匹配到该语法块 | location = /mod { ... } |
^~ | 路径前缀匹配 | 正则匹配路径前缀且命中后不再进行进一步匹配 | location ^~ /mod { ... } |
~* | 不区分大小写的正则匹配 | 不区分大小写的正则匹配 | location ~* /mod { ... } |
~ | 正则匹配 | 正则匹配 | location ~ /mod { ... } |
none | 普通匹配 | 普通匹配 | location /mod { ... } |