插件窝 干货文章 Windows系统下Nginx服务器怎么配置

Windows系统下Nginx服务器怎么配置

Nginx 配置 index html 223    来源:    2025-04-27

Windows系统下Nginx服务器配置指南

一、安装Nginx

  1. 下载Nginx

    • 访问Nginx官网
    • 下载Windows版本(如nginx-1.xx.x.zip)
  2. 解压安装

    • 将下载的zip文件解压到指定目录(如C:\nginx)
    • 避免使用包含空格或中文的路径

二、基本配置

  1. 主要配置文件

    • 主配置文件位于conf/nginx.conf
    • 虚拟主机配置通常放在conf/vhosts/目录下(需手动创建)
  2. 常用配置示例

# 全局配置
worker_processes  1;  # 设置工作进程数,通常等于CPU核心数

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

三、启动与停止Nginx

  1. 启动Nginx

    • 打开命令提示符(管理员权限)
    • 进入Nginx目录: cd C:\nginx
    • 执行: start nginx 或直接运行nginx.exe
  2. 停止Nginx

    • 快速停止: nginx -s stop
    • 优雅停止: nginx -s quit
  3. 重新加载配置

    • 修改配置后无需重启: nginx -s reload

四、常见配置场景

  1. 配置虚拟主机
server {
    listen       80;
    server_name  example.com www.example.com;

    location / {
        root   C:/websites/example;
        index  index.html index.htm;
    }
}
  1. 配置PHP支持
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
  1. 配置HTTPS
server {
    listen       443 ssl;
    server_name  example.com;

    ssl_certificate      C:/nginx/conf/ssl/example.com.crt;
    ssl_certificate_key  C:/nginx/conf/ssl/example.com.key;

    ssl_session_cache    shared:SSL:1m;
    ssl_session_timeout  5m;

    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers  on;

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

五、常见问题解决

  1. 端口被占用

    • 检查80端口是否被占用: netstat -ano | findstr :80
    • 终止占用进程或修改Nginx监听端口
  2. 配置文件错误

    • 测试配置文件: nginx -t
    • 根据错误提示修正配置
  3. 访问权限问题

    • 确保Nginx有权限访问网站目录
    • 防火墙设置允许Nginx通过
  4. 性能调优

    • 根据服务器配置调整worker_processesworker_connections
    • 启用Gzip压缩减少传输量

六、将Nginx设置为Windows服务

  1. 使用第三方工具如NSSM将Nginx注册为服务
  2. 或创建批处理文件实现开机自启动

希望以上配置指南对您有所帮助。如需更详细的配置说明,可以参考Nginx官方文档或针对特定需求的配置教程。