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;
        }
    }

    # 11433 端口反向代理 LLM 服务
    server {
        listen 11433;
        server_name localhost;
        
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root html;
        }
        
        location / {
            # 假设你的 LLM 实际运行在本机的默认端口 11434
            proxy_pass http://127.0.0.1:11434; 
            
            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;
            
            # 关键配置:关闭缓冲以支持流式输出 (SSE/Streaming)
            proxy_buffering off;
            proxy_cache off;
            
            # 适当增加超时时间,防止大模型推理时间过长导致 Nginx 断开连接
            proxy_connect_timeout 3600s;
            proxy_send_timeout 3600s;
            proxy_read_timeout 3600s;
        }
    }
}

🚀 启动 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

--------------------------------------------------------------------

使用 nginx -t 检查语法,并使用 nginx -s reload 重载配置

nginx -t
nginx: the configuration file D:\zbintel_business_ai\nginx-1.30.1/conf/nginx.conf syntax is ok
nginx: configuration file D:\zbintel_business_ai\nginx-1.30.1/conf/nginx.conf test is successful

nginx -s reload

更多推荐