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 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
发现一只大呆瓜3 小时前
Vite凭什么这么快?3分钟带你彻底搞懂 Vite 热更新的幕后黑手
前端·面试·vite
YYRAN_ZZU3 小时前
Petalinux新建自动脚本启动
linux
Maimai108083 小时前
React如何用 @microsoft/fetch-event-source 落地 SSE:比原生 EventSource 更灵活的实时推送方案
前端·javascript·react.js·microsoft·前端框架·reactjs·webassembly
charlie1145141913 小时前
嵌入式Linux驱动开发pinctrl篇(1)——从寄存器到子系统:驱动演进之路
linux·运维·驱动开发
于小猿Sup3 小时前
VMware在Ubuntu22.04驱动Livox Mid360s
linux·c++·嵌入式硬件·自动驾驶
cen__y4 小时前
Linux12(Git01)
linux·运维·服务器·c语言·开发语言·git
kyriewen5 小时前
产品经理把PRD写成“天书”,我用AI半小时重写了一遍,他当场愣住
前端·ai编程·cursor
不仙5205 小时前
VMware Workstation 26.0.0 在 Ubuntu 24.04 (内核 6.17.0) 上的安装与内核模块编译问题
linux·ubuntu·elasticsearch