Windows Nginx 完整安装 + 启动教程
一、下载 Nginx(官方第一个稳定版链接)
官网地址:https://nginx.org/en/download.html 第一个稳定版本下载链接(当前稳定版 1.26.2 Windows 包): https://nginx.org/download/nginx-1.26.2.zip
下载步骤
- 复制上面链接浏览器打开,自动下载 zip 压缩包
- 重要:解压路径不能有中文、空格 推荐路径:
D:\server\nginx-1.26.2禁止:D:\软件\nginx、D:\nginx 服务
二、目录结构简单说明
nginx-1.26.2
├─conf # 配置文件目录(核心 nginx.conf)
├─html # 默认网站页面
├─logs # 日志
└─nginx.exe # 主程序
三、CMD 启动 / 操作命令
-
打开 CMD,cd 进入 nginx 解压目录
D:
cd D:\server\nginx-1.26.2 -
常用指令
1. 启动nginx(后台运行)
start nginx
2. 检查配置是否写错(改完配置必执行)
nginx -t
3. 平滑重载配置(不中断访问)
nginx -s reload
4. 快速强制停止
nginx -s stop
5. 优雅停止(等待现有请求完成再关闭)
nginx -s quit
四、验证是否运行成功
启动后浏览器访问:http://127.0.0.1 看到灰色 Nginx 欢迎页面 = 运行正常
五、常见问题解决
-
80 端口被占用启动失败 查看占用进程:
netstat -ano | findstr :80
记下最后一列 PID,任务管理器结束对应进程,或修改 nginx 监听端口。
-
关闭所有 nginx 进程
taskkill /f /im nginx.exe
-
修改网站根目录 / 反向代理 编辑
conf/nginx.conf,改完一定要nginx -t校验再 reload。
六、开机自启(可选)
Windows 原生绿色版不能直接开机启动,两种方案:
- 用 WinSW 工具把 nginx 注册成 Windows 服务;
- 创建 bat 启动脚本放到开机启动文件夹。
GeoJSON 文件存放位置 + Nginx 访问配置(Windows Nginx)
一、推荐存放目录(两种方案任选)
方案 1:放默认网页目录(最简单,开箱即用)
路径:nginx/html/
- 直接新建文件夹存 geojson,比如
nginx/html/geo/data/ - 文件:
china.geojson、city.geojson
访问地址:
http://127.0.0.1/geo/data/china.geojson
方案 2:单独新建静态资源目录(规范项目)
在 nginx 根目录新建 static/geo/ 完整路径:D:\server\nginx-1.26.2\static\geo\ 后续需要改配置映射访问路径。
二、关键配置:Nginx 允许读取 geojson MIME 类型
默认 nginx 不识别 .geojson,前端请求会出现跨域 / 下载乱码 / 无法解析 ,必须修改 conf/nginx.conf
-
打开
conf/mime.types找到types { }块,添加一行:application/geo+json geojson;
-
或者直接在
nginx.confhttp 块内添加:http {
include mime.types;
default_type application/octet-stream;# 新增geojson类型 types { application/geo+json geojson; }}
三、如果用方案 2(自定义 static 文件夹),配置映射
在 server {} 内部添加 location:
server {
listen 80;
server_name localhost;
# 访问 /geo/ 映射到 static/geo 文件夹
location /geo/ {
root static;
index none;
# 允许跨域(前端地图常用)
add_header Access-Control-Allow-Origin *;
}
location / {
root html;
index index.html index.htm;
}
}
文件放在 static/geo/country.geojson 访问地址:http://127.0.0.1/geo/country.geojson
四、完整操作步骤
- 把 geojson 文件放入对应文件夹
- 修改 mime.types 添加 geojson 类型
- 校验配置:
nginx -t - 重载配置:
nginx -s reload - 浏览器 / 前端地图直接 url 请求即可加载
五、前端地图常见配套配置(CORS 跨域)
如果 OpenLayers/DeckGL 本地前端访问 geojson 报跨域,在 location 里加上:
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods GET;
六、快速示例(最简直接用 html 目录)
文件位置:nginx/html/data/bound.geojson 无需额外 location 配置,直接访问: http://127.0.0.1/data/bound.geojson
ngix 如何放 net 服务
搜索 3 个关键词,参考 17 篇资料
Windows Nginx 代理 .NET(ASP.NET Core)完整教程
核心原理
.NET 程序(Kestrel)单独跑一个端口(如 http://127.0.0.1:5000),Nginx 做反向代理 对外暴露 80 端口,统一接收请求转发给.NET 服务; GeoJSON 静态文件放 Nginx html 目录,接口走代理,前后端分离一套 Nginx 搞定。
一、先启动你的.NET 服务
-
发布.NET 项目,CMD 进入发布目录启动:
默认监听5000/5001
dotnet YourApp.dll
指定端口启动(推荐固定端口)
dotnet YourApp.dll --urls=http://127.0.0.1:5000
-
验证:浏览器访问
http://127.0.0.1:5000能正常打开接口 / 页面。
二、修改 Nginx 配置 conf/nginx.conf
完整可用配置(同时兼容 GeoJSON 静态文件 + .NET 接口 + SignalR 长连接)
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# 识别geojson文件(之前地图需要)
types {
application/geo+json geojson;
}
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 logs/access.log main;
sendfile on;
keepalive_timeout 65;
# 适配SignalR/WebSocket必须配置
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name localhost;
# 1. 静态GeoJSON/前端页面(nginx/html)
location / {
root html;
index index.html index.htm;
}
# 2. 所有/api接口转发到.NET服务 5000端口
location /api/ {
proxy_pass http://127.0.0.1:5000/api/;
# 代理标准头(.NET获取真实客户端IP、域名)
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# SignalR/Blazor Server 长连接必备
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_cache_bypass $http_upgrade;
# 跨域(前端地图/axios调用接口必备)
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS always;
add_header Access-Control-Allow-Headers Content-Type,Authorization always;
if ($request_method = 'OPTIONS') {
return 204;
}
}
# 3. 若.NET是MVC/Razor页面,把所有路径转发给.NET(覆盖静态)
# location / {
# proxy_pass http://127.0.0.1:5000/;
# # 复制上面proxy_set_header全套配置
# }
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
三、关键配置说明
- proxy_pass 结尾斜杠区别
http://127.0.0.1:5000/api/:访问/api/xxx→ 转发5000/api/xxxhttp://127.0.0.1:5000:访问/api/xxx→ 转发5000/api/xxx
- WebSocket/SignalR 必须加
map和 Upgrade、Connection 头,否则实时通讯断开。 - 跨域 前端 OpenLayers/axios 请求 /api 不会报跨域,OPTIONS 预检直接返回 204。
- GeoJSON 共存 静态文件
/geo/data.geojson由 Nginx 直接返回,不走.NET,性能更高。
四、生效操作(Windows CMD 进入 nginx 目录)
# 1. 校验配置是否写错(必执行)
nginx -t
# 2. 重载配置,不中断服务
nginx -s reload
# 重启nginx(端口冲突时用)
taskkill /f /im nginx.exe
start nginx
五、访问测试
- 静态 GeoJSON:
http://127.0.0.1/geo/china.geojson - .NET 接口:
http://127.0.0.1/api/WeatherForecast
六、常见踩坑解决
1. 80 端口被占用
netstat -ano | findstr :80
杀掉占用进程,或修改 nginx listen 8080;
2. .NET 拿不到客户端真实 IP
缺少 X-Real-IP、X-Forwarded-For 两行代理头,复制完整配置即可。
3. SignalR 连接频繁断开
漏配 map $http_upgrade 和 Upgrade/Connection 头,必须完整复制。
4. 上传大文件报错
在 server{} 内添加限制:
client_max_body_size 50m;
5. 同时跑多个.NET 服务(多端口)
# 接口1:/api/user → 5000
location /api/user/ {
proxy_pass http://127.0.0.1:5000/api/user/;
# 全套proxy头
}
# 接口2:/api/map → 5001
location /api/map/ {
proxy_pass http://127.0.0.1:5001/api/map/;
# 全套proxy头
}
七、开机自启(可选)
- .NET:用 WinSW 封装成 Windows 服务后台常驻;
- Nginx:创建启动 bat 脚本放入开机文件夹,或同样封装服务。
需要我给你一份只代理.NET、不处理静态文件的极简 nginx.conf 模板吗?