windows下nginx的安装

nginx: download

复制代码
worker_processes  8;                # 根据 CPU 核数调整

events {
    worker_connections  2048;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    
    # 日志
    # 定义日志格式(必须放在 access_log 之前)
    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;
    error_log   logs/error.log   warn;
    
    
    # 基础优化
    sendfile        on;
    keepalive_timeout  65;
    client_max_body_size 10M;
    server_tokens off;
    gzip on;
    gzip_types text/plain application/json;
    
    # 后端组
    upstream myapp_backend {
        server 127.0.0.1:5001 max_fails=3 fail_timeout=30s;
        keepalive 32;
    }
    
    # 8080 端口静态服务
    server {
        listen 8080;
        server_name localhost;

        error_page 500 502 503 504 /50x.html;   # 移到 server 内
        location = /50x.html {
            root html;
        }

        location / {
            root html;
            index index.html index.htm;
            expires 1d;
        }
    }
    
    # 5000 端口反向代理
    server {
        listen 5000;
        server_name localhost;
        
        error_page 500 502 503 504 /50x.html;   # 移到 server 内
        location = /50x.html {
            root html;
        }
        
        location / {
            proxy_pass http://myapp_backend;
            proxy_http_version 1.1;
            proxy_set_header Connection "";
            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;
            
            proxy_connect_timeout 120s;
            proxy_send_timeout 120s;
            proxy_read_timeout 120s;
            proxy_buffering on;
        }
    }
}

🚀 启动 Nginx

以管理员身份启动(推荐)

因为配置中监听了 80 端口(需要管理员权限),请按以下步骤操作:

  1. 以管理员身份打开命令提示符

    Win 键,输入 cmd,右键点击"命令提示符",选择 "以管理员身份运行"

  2. 进入 Nginx 目录

    cd D:\zbintel\nginx-1.30.1

  3. 启动 Nginx

    start nginx

  4. 验证是否启动成功

    tasklist /fi "imagename eq nginx.exe"

    应该能看到类似输出(两个进程:一个 master,一个 worker):

    text

    复制代码
    nginx.exe                     1234 Console                    1      8,132 K
    nginx.exe                     5678 Console                    1      8,256 K

✅ 验证服务

  1. 确认 Python 后端已启动 (监听 127.0.0.1:5001

  2. 访问测试

    • 打开浏览器访问:http://192.168.11.10:5000

      应能看到您的 Python Web 应用响应。

    • 访问 http://192.168.11.10:80(如果 80 端口未被其他程序占用)

      应能看到 Nginx 默认的 html/index.html 欢迎页。

🛑 常用管理命令

操作 命令(在 Nginx 目录下执行)
平滑重启(重载配置) nginx -s reload
快速停止 nginx -s stop
优雅停止 nginx -s quit
重新打开日志文件 nginx -s reopen

⚠️ 如果访问 5000 端口出现 502 Bad Gateway

请检查:

  • Python 服务是否运行在 5001 端口(且 host='0.0.0.0'127.0.0.1

  • 执行 netstat -ano | findstr :5001 确认 Python 正在监听

  • 查看 Nginx 错误日志:D:\zbintel\nginx-1.30.1\logs\error.log

相关推荐
ℋᙚᵐⁱᒻᵉ鲸落3 小时前
移动端滑动手势冲突:overflow: auto导致外层横向滑动失效
前端·javascript·css·vue.js·html
M78佐菲3 小时前
Linux学习笔记:TCP协议
linux·笔记·学习·tcp/ip·算法
Brilliantwxx3 小时前
【Linux】 进程(9)程序与进程地址空间(基础+进阶+面试题)
linux·运维·服务器·开发语言·c++
不怕犯错,就怕不做4 小时前
git prune 自动删除本地记录中那些远程已经不存在的分支引用
linux·服务器·git
默_笙4 小时前
🎃 前端学了 Next.js,后端该学啥?NestJS 就是 Node 版的蜜雪冰城
前端·javascript
weixin_416667965 小时前
【无标题】
运维·服务器·网络
前端snow5 小时前
ai agent --- 实现 openclaw 定时间效果
前端
一池秋_5 小时前
arm低配linux设备,桌面应用冷启动提速方法
linux·运维·arm开发
惜离殇5 小时前
从零开始的敲代码生活--Linux应用软件(文件操作基础1)
linux·c语言·文件操作·标准io
码云之上5 小时前
换模型之后,Chatbot 为什么要自己做 compact?
前端·agent·前端工程化