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

相关推荐
禅思院23 分钟前
AI对话前端从入门到崩溃:一个长对话引发的五层优化战争【引子】
前端·面试·架构
TrisighT1 小时前
Electron 鸿蒙 PC 上点外链唤醒应用,我试了 6 种写法只有 1 种能跑
前端·electron·harmonyos
天才熊猫君2 小时前
配置与数据分离:一种可视化搭建的属性编辑方案
前端·javascript
林希_Rachel_傻希希2 小时前
web性能之相关路径——AI总结
前端·javascript·面试
竹林8182 小时前
用 wagmi v2 踩坑两天,我终于搞懂了多链钱包切换在 DeFi 前端中的正确姿势
前端·javascript
用户2136610035722 小时前
Vue项目搜索功能与面包屑导航
前端·javascript
星栈2 小时前
LiveView 的实时通信,爽是爽,但 PubSub 和广播也最容易把自己绕晕
前端·前端框架·elixir
用户2930750976692 小时前
告别关键词匹配,拥抱向量语义 —— RAG 搜索从零到一
前端
独孤留白2 小时前
从C到Rust:告别 C 的"指针 + 长度"手动模式
前端·rust
掘金安东尼3 小时前
中小厂前端候选人简历面试拆解:从 HR 面、技术面到主管面的双赢提问法
前端·面试