本章节将指导您完成 Nginx 的基本配置和运行,让您快速上手使用 Nginx。
在开始之前,请确保:
# 直接启动
sudo nginx
# 或使用 systemd
sudo systemctl start nginx
# 设置开机自启
sudo systemctl enable nginx
# 双击 nginx.exe
cd C:\nginx
nginx
# 或使用命令提示符
start nginx
# Linux/macOS
ps aux | grep nginx
# Windows
tasklist | findstr nginx
# 使用 curl
curl http://localhost
# 或使用 wget
wget -O - http://localhost
在浏览器中打开 http://localhost,应该看到 "Welcome to nginx!" 页面。
# 启动
sudo nginx
# 停止(快速)
sudo nginx -s stop
# 优雅退出
sudo nginx -s quit
# 重新加载配置
sudo nginx -s reload
# 重新打开日志
sudo nginx -s reopen
# 测试配置
sudo nginx -t
# 查看版本
nginx -v
# 查看编译信息
nginx -V
# 查看状态
sudo systemctl status nginx
# 启动
nginx
# 停止
nginx -s stop
# 优雅退出
nginx -s quit
# 重新加载配置
nginx -s reload
# 重新打开日志
nginx -s reopen
# 测试配置
nginx -t
# 查看版本
nginx -v
# 查看编译信息
nginx -V
| 操作系统 | 主配置文件 | 额外配置 | 日志文件 |
|---|---|---|---|
| Linux | /etc/nginx/nginx.conf | /etc/nginx/conf.d/ | /var/log/nginx/ |
| macOS | /usr/local/etc/nginx/nginx.conf | /usr/local/etc/nginx/servers/ | /usr/local/var/log/nginx/ |
| Windows | conf/nginx.conf | conf/conf.d/ | logs/ |
# Linux/macOS
cat /etc/nginx/nginx.conf
# Windows
type conf\nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf;
}
# Linux/macOS
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
# Windows
copy conf\nginx.conf conf\nginx.conf.bak
# Linux/macOS
sudo vim /etc/nginx/nginx.conf
# Windows
notepad conf\nginx.conf
# Linux/macOS
sudo nginx -t
# Windows
nginx -t
# Linux/macOS
sudo nginx -s reload
# Windows
nginx -s reload
# Linux/macOS
sudo mkdir -p /var/www/mywebsite
sudo chown -R $USER:$USER /var/www/mywebsite
# Windows
mkdir C:\nginx\html\mywebsite
# Linux/macOS
echo "<h1>Hello, Nginx!</h1>" | sudo tee /var/www/mywebsite/index.html
# Windows
echo <h1>Hello, Nginx!</h1> > C:\nginx\html\mywebsite\index.html
# Linux/macOS
sudo vim /etc/nginx/conf.d/mywebsite.conf
# Windows
notepad conf\conf.d\mywebsite.conf
添加以下内容:
server {
listen 80;
server_name localhost;
location / {
root /var/www/mywebsite;
index index.html index.htm;
}
}
# Linux/macOS
sudo nginx -t
sudo nginx -s reload
# Windows
nginx -t
nginx -s reload
在浏览器中访问 http://localhost,应该看到 "Hello, Nginx!" 页面。
server {
listen 8080; # 修改端口
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
server {
listen 80;
server_name example.com;
location / {
root /var/www/example.com;
index index.html index.htm;
}
}
server {
listen 80;
server_name blog.example.com;
location / {
root /var/www/blog;
index index.html index.htm;
}
}
location / {
root /var/www/mywebsite;
autoindex on;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# Linux/macOS
sudo tail -f /var/log/nginx/access.log
# Windows
Get-Content logs\access.log -Wait -Tail 50
# Linux/macOS
sudo tail -f /var/log/nginx/error.log
# Windows
Get-Content logs\error.log -Wait -Tail 50
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
sudo ufw allow 'Nginx Full'
netsh advfirewall firewall add rule name="nginx" dir=in action=allow protocol=TCP localport=80
# 优雅退出
sudo nginx -s quit
# 快速停止
sudo nginx -s stop
# 或使用 systemd
sudo systemctl stop nginx
# 优雅退出
nginx -s quit
# 快速停止
nginx -s stop
# 或使用任务管理器
taskkill /F /IM nginx.exe
# 测试配置
nginx -t
# 查看详细错误
nginx -T
# Linux/macOS
sudo netstat -tlnp | grep :80
sudo lsof -i :80
# Windows
netstat -ano | findstr :80
# Linux/macOS
sudo chown -R nginx:nginx /var/www/mywebsite
sudo chmod -R 755 /var/www/mywebsite
# Windows
# 以管理员身份运行