nginx 命令和配置
常用命令
启动
./nginx
检查 nginx.conf 配置文件
./nginx -t
重启
./nginx -s reload
停止
./nginx -s stop
nginx 配置
基本配置
# 全局区 有一个工作子进程,一般设置为 CPU 数 * 核数
worker_processes 1;
events {
# 一般是配置 nginx 进程与连接的特性
# 如 1 个 work 能同时允许多少连接,一个子进程最大允许连接 1024 个连接
worker_connections 1024;
}
# 配置 HTTP 服务器配置段
http {
# 配置虚拟主机段
server {
# 定位,把特殊的路径或文件再次定位
location {
}
}
server {
...
}
}
基于域名的虚拟主机
# 在 http 大括号中添加如下代码段
server {
# 监听端口 80
listen 80;
# 监听域名 abc.com
server_name abc.com
location / {
# 相对路径,相对 nginx根目录。也可写成绝对路径
root abc
# 默认跳转到 index.html 页面
index index.html
}
}
基于端口的虚拟主机配置
server {
listen 2022;
server_name abc.com;
location / {
root /home;
index index.html
}
}
基于 IP 地址虚拟主机配置
server {
listen 80;
server_name 139.199.94.62;
location / {
root ip;
index index.html;
}
}