安装目录
一般会在 /usr/local/ 下
启动服务
systemctl daemon-reload
systemctl start nginx.service
# 开机自启动
systemctl enable nginx.service
启动命令
./nginx
./nginx -s stop # 停止
./nginx -s quit # 退出
./nginx -s reload # 重启
辅助命令
nginx -c xxx.conf # 指定配置文件启动
nginx -t # 检测配置文件是否有语法错误
nginx -v # 版本信息
主体配置
main # 全局配置,也称为Main块,对全局生效
- events # 配置影响 Nginx 服务器或与用户的网络连接
- http # 配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置
- upstream # 负载均衡
- server
- location # location uri 匹配规则,可以多个
- location
查看端口信息,排查启动信息
查看日志
# 进到 logs 目录。目录不是固定的
cd /usr/local/openresty/nginx/logs
tail -f access.log
location 匹配规则
匹配符号
= # 全字匹配
^~ # 不支持正则
~ # 区分大小写的正则匹配
~* # 不区分大小写的正则匹配
/ # 通知匹配,匹配任何请求
示例
匹配一个文件路径,去实现自动下载服务器上的文件
location ^~ /buildlogs {
alias /data/projectPath/buildlogs;
add_header Content-Disposition "attachment;";
}
匹配顺序
= > ^~ > ~, ~* (按文件中顺序) > /
当有一项匹配成功时,停止匹配,按当前匹配规则处理请求
root 与 alias
一句话总结:root 是拼接,alias 是替换
location /abc {
root /data/web/dist
# 访问 /abc 时,实际访问的资源是 ${root}/abc ---> /data/web/dist/abc
}
location /def {
alias /data/web/dist
# 访问 /def 时,实际访问的资源是 ${alias} ---> /data/web/dist
# 如果需要访问 def,则设置 alias=/data/web/dist/def
}
负载均衡
upstream mysite {
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8086 weight=1;
}
server {
listen 80;
server_name xiaoshanjie.com
error_page 500 502 503 504 /50x.html
location = /50x.html {
root html;
}
locaton / {
proxy_pass http://mysite
}
}