在 Windows 上安装

Nginx 在 Windows 上有预编译的二进制版本,本章节将介绍如何在 Windows 系统上安装和配置 Nginx。

系统要求

  • Windows 7 或更高版本
  • Windows Server 2008 R2 或更高版本
  • 至少 1GB 可用磁盘空间
  • 512MB 以上内存(推荐 1GB+)

下载 Nginx

  1. 访问 Nginx 官方下载页面
  2. 在 "nginx/Windows" 部分下载最新版本的 zip 压缩包
  3. 下载完成后解压到目标目录

安装步骤

1. 解压文件

将下载的 zip 文件解压到您选择的目录,例如:

C:\nginx

2. 目录结构

解压后的目录结构如下:

nginx/
├── conf/              # 配置文件目录
│   ├── fastcgi.conf
│   ├── fastcgi_params
│   ├── koi-utf
│   ├── koi-win
│   ├── mime.types
│   ├── nginx.conf
│   ├── scgi_params
│   ├── uwsgi_params
│   └── win-utf
├── contrib/           # 脚本和工具
├── docs/              # 文档
├── html/              # 默认网站根目录
│   ├── 50x.html
│   └── index.html
├── logs/              # 日志文件目录
│   ├── access.log
│   └── error.log
└── nginx.exe          # Nginx 可执行文件

3. 启动 Nginx

方法一:双击启动

直接双击 nginx.exe 文件启动 Nginx。

方法二:命令行启动

打开命令提示符(CMD)或 PowerShell,进入 Nginx 目录:

cd C:\nginx
nginx

方法三:以服务方式启动(推荐)

使用第三方工具将 Nginx 注册为 Windows 服务:

# 下载 NSSM (Non-Sucking Service Manager)
# https://nssm.cc/download

# 安装服务
nssm install nginx C:\nginx\nginx.exe

# 启动服务
nssm start nginx

# 停止服务
nssm stop nginx

# 重启服务
nssm restart nginx

# 删除服务
nssm remove nginx

验证安装

# 检查版本
nginx -v

# 测试配置
nginx -t

# 测试服务
curl http://localhost

在浏览器中访问 http://localhost,看到 "Welcome to nginx!" 页面即表示安装成功。

管理命令

# 启动 Nginx
nginx

# 停止 Nginx(快速停止)
nginx -s stop

# 优雅退出(等待当前请求完成)
nginx -s quit

# 重新加载配置
nginx -s reload

# 重新打开日志文件
nginx -s reopen

# 测试配置文件
nginx -t

# 显示版本信息
nginx -v

# 显示版本和编译信息
nginx -V

配置文件

主配置文件

C:\nginx\conf\nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

修改端口

如果 80 端口被占用,可以修改为其他端口:

server {
    listen       8080;  # 修改为 8080 端口
    server_name  localhost;

    location / {
        root   html;
        index  index.html index.htm;
    }
}

修改后重新加载配置:

nginx -t
nginx -s reload

防火墙配置

Windows 防火墙

# 允许 HTTP (80 端口)
netsh advfirewall firewall add rule name="nginx" dir=in action=allow protocol=TCP localport=80

# 允许 HTTPS (443 端口)
netsh advfirewall firewall add rule name="nginx https" dir=in action=allow protocol=TCP localport=443

# 删除规则
netsh advfirewall firewall delete rule name="nginx"

通过图形界面配置

  1. 打开 "Windows Defender 防火墙"
  2. 点击 "高级设置"
  3. 选择 "入站规则" -> "新建规则"
  4. 选择 "端口" -> "TCP" -> "特定本地端口" -> 输入 80
  5. 选择 "允许连接"
  6. 选择所有网络类型
  7. 输入规则名称(如 "nginx")

日志文件

C:\nginx\logs\
├── access.log    # 访问日志
└── error.log     # 错误日志

查看日志

# 实时查看错误日志
Get-Content C:\nginx\logs\error.log -Wait -Tail 50

# 实时查看访问日志
Get-Content C:\nginx\logs\access.log -Wait -Tail 50

# 查看 Nginx 进程
Get-Process nginx

常见问题

1. 端口被占用

# 查看端口占用
netstat -ano | findstr :80

# 结束占用端口的进程
taskkill /PID <进程ID> /F

2. 权限问题

以管理员身份运行命令提示符或 PowerShell。

3. 配置文件错误

# 测试配置文件
nginx -t

# 检查错误日志
type C:\nginx\logs\error.log

4. Nginx 无法启动

# 检查进程
tasklist | findstr nginx

# 强制结束进程
taskkill /F /IM nginx.exe

开机自启

方法一:使用任务计划程序

  1. 打开 "任务计划程序"
  2. 创建基本任务
  3. 设置触发器为 "计算机启动时"
  4. 操作为 "启动程序"
  5. 程序:C:\nginx\nginx.exe
  6. 起始于:C:\nginx

方法二:使用 NSSM(推荐)

nssm install nginx C:\nginx\nginx.exe
nssm set nginx Start SERVICE_AUTO_START

方法三:添加到启动文件夹

  1. Win + R,输入 shell:startup
  2. 创建 nginx.exe 的快捷方式
  3. 将快捷方式移动到启动文件夹

卸载 Nginx

# 停止 Nginx
nginx -s stop

# 删除服务(如果使用 NSSM)
nssm remove nginx

# 删除 Nginx 目录
rmdir /s C:\nginx

性能优化

修改 worker_processes

worker_processes  auto;  # 自动检测 CPU 核心数

修改 worker_connections

events {
    worker_connections  2048;  # 增加连接数
}

启用 gzip 压缩

http {
    gzip  on;
    gzip_min_length  1000;
    gzip_types       text/plain text/css application/json;
}

相关资源