开始使用

本章节将指导您完成 Nginx 的基本配置和运行,让您快速上手使用 Nginx。

前提条件

在开始之前,请确保:

  • 已安装 Nginx
  • 具有管理员权限(Linux/macOS 需要 sudo)
  • 了解基本的命令行操作

启动 Nginx

Linux/macOS

# 直接启动
sudo nginx

# 或使用 systemd
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

Windows

# 双击 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!" 页面。

基本命令

Linux/macOS

# 启动
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

Windows

# 启动
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/
Windowsconf/nginx.confconf/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;
}

修改配置

1. 备份配置

# Linux/macOS
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak

# Windows
copy conf\nginx.conf conf\nginx.conf.bak

2. 编辑配置

# Linux/macOS
sudo vim /etc/nginx/nginx.conf

# Windows
notepad conf\nginx.conf

3. 测试配置

# Linux/macOS
sudo nginx -t

# Windows
nginx -t

4. 重新加载配置

# Linux/macOS
sudo nginx -s reload

# Windows
nginx -s reload

创建第一个网站

1. 创建网站目录

# Linux/macOS
sudo mkdir -p /var/www/mywebsite
sudo chown -R $USER:$USER /var/www/mywebsite

# Windows
mkdir C:\nginx\html\mywebsite

2. 创建首页文件

# 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

3. 创建服务器配置

# 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;
    }
}

4. 测试并重新加载

# Linux/macOS
sudo nginx -t
sudo nginx -s reload

# Windows
nginx -t
nginx -s reload

5. 访问网站

在浏览器中访问 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

防火墙配置

Linux (firewalld)

sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

Linux (ufw)

sudo ufw allow 'Nginx Full'

Windows Defender 防火墙

netsh advfirewall firewall add rule name="nginx" dir=in action=allow protocol=TCP localport=80

停止 Nginx

Linux/macOS

# 优雅退出
sudo nginx -s quit

# 快速停止
sudo nginx -s stop

# 或使用 systemd
sudo systemctl stop nginx

Windows

# 优雅退出
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
# 以管理员身份运行