Guide
1. 安装
1.1. Windows
scoop install main/nginxnginx -p "$env:NGINX_HOME"- 修改配置文件
$env:NGINX_HOME/conf/nginx.conf中server属性中listen字段: 由80改为8080 - 鼠标双击
$env:NGINX_HOME/nginx.exe的方式来启动nginx.exe - 在
localhost:8080可以查看启动后的网页 nginx -s stop关闭
1.2. Ubuntu
sudo apt updatesudo apt install -y nginxsystemctl status nginxnginx -v
2. 新手指南
配置文件名nginx.conf
配置文件位置
/etc/nginx/usr/local/nginx/conf/usr/local/etc/nginx/
2.1. 配置文件命令相关
sudo systemctl start nginx # 启动
sudo systemctl stop nginx # 停止
sudo systemctl reload nginx # 重载
sudo systemctl restart nginx # 重启
sudo nginx -t # 测试配置文件语法
nginx
nginx -s stop
nginx -s quit
nginx -s reload
2.2. 配置文件的结构
配置文件由简单指令;和块指令{}构成. #表示注释
user www-data;
events {
worker_connections 768;
# multi_accept on;
}
结构
main # 全局上下文
|--- events # 事件驱动模块配置
|--- http # HTTP服务器配置
| |--- server # 虚拟主机
| |--- location # URI路径配置
| |--- upstream # HTTP负载无衡后端组
|--- stream # TCP/UDP代理配置(Nginx 1.9.0+)
| |--- server # 流虚拟主机
| |--- upstream # TCP/UDP负载均衡后端组
server
配置文件中定义多个服务器, 即server块, 通过端口和服务器名来区分.
location
输入的URL请求通过location按最长前缀进行匹配.
location后的路径要以/结尾.
当URL路径与文件路径有层级关系时, 使用root
/data/
├── www/
│ ├── index.html
│ └── about.html
└── images/
├── logo.png
└── bg.jpg
server {
location / {
root /data/www; # 处理根路径'/'的请求
}
location /images/ {
root /data; # 处理'/images/'路径的请求
}
}
- 访问
/index.html=>/data/www/index.html - 访问
/images/logo.png=>/data/images/logo.png
当URL路径与文件路径无层级关系时, 使用alias
location /images/ {
alias /opt/media/; # 处理 '/images/' 路径的请求
}
- 访问
/images/logo.png=>/opt/media/logo.png, 不存在则返回 404 error.
2.3. 设置一个简单的代理服务器
server {
listen 80;
root /data/upload;
location / {
proxy_pass http://localhost:8080;
}
location ~ \.(gif|jpg|png)$ {
root /data/images;
}
}