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

相关推荐
子兮曰1 天前
jev-ultrafast 深度解析:7 秒订机票的浏览器 Agent 是如何炼成的
前端·后端·agent
子兮曰1 天前
Jev 爆发一周:7 秒 Agent 背后的 System One 生态与三场争议
前端·后端·ai编程
未济1 天前
linux 配置环境变量
linux
傲世仙尊1 天前
目录即文件-Ext文件系统收尾篇
linux·c语言
前端小万1 天前
写公众号赚了 3000 块后,我做了一款叫 "一键成稿" 的软件
前端·微信小程序
爱勇宝1 天前
ZCode 开源 24 小时:一份没有历史的账本,回答不了"有没有偷代码"
前端·后端·chatglm (智谱)
虎头金猫1 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
三十而立洋1 天前
Cookie 详解:从产生到安全,一次讲透
前端·javascript
卡布鲁1 天前
把一个 Vite + Vue3 应用塞进 qiankun (React + Umi3) 主站:十个坑的复盘
前端·javascript·react.js
_艾伦 耶格尔.1 天前
进程间通信
linux