SoFunction
Updated on 2025-04-24

The entire process of deploying Flask applications using Nginx in Windows Server environment

1. Environmental preparation

  1. operating system: Windows Server 2016/2019/2022
  2. Python: Install the latest stable version (such as Python 3.10+), make sure to check itAdd Python to PATH
  3. Nginx for Windows: fromOfficial websiteDownload the Windows version (such as nginx/Windows-1.25.3).
  4. Dependency tools: Git (optional, used for code pull), text editor (such as VS Code).

2. Create a Flask app

2.1 Project Structure

C:\flask-app\
   ├──           # Flask main program   ├── 
   ├── static\         # Static files   └── venv\           # Python Virtual Environment

2.2 Example

from flask import Flask

app = Flask(__name__)

@('/')
def home():
    return "Hello, World! Flask on Windows Server with Nginx!"

if __name__ == '__main__':
    (host='0.0.0.0', port=5000)

2.3 Installation dependencies

# Create a virtual environmentpython -m venv venv
venv\Scripts\activate

# Install Flask and Waitress (production-grade WSGI server)pip install flask waitress

3. Start Flask using Waitress

3.1 Start command

waitress-serve --port=5000 --threads=4 app:app
  • Parameter description:
    • --port: Listen to the port
    • --threads: Number of threads (adjusted according to CPU core)

3.2 Create a startup script start_server.bat

@echo off
call venv\Scripts\activate
waitress-serve --port=5000 --threads=4 app:app

4. Configure Nginx reverse proxy

4.1 Modify the Nginx configuration file

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       ;
    default_type  application/octet-stream;

    # Reverse proxy configuration    upstream flask_app {
        server 127.0.0.1:5000;  # Waitress Listening Address    }

    server {
        listen       80;
        server_name  your_domain.com;  # Replace with domain name or server IP
        # Static file processing (directly proxyed by Nginx)        location /static {
            alias C:/flask-app/static;  # Replace with the actual path            expires 30d;
        }

        # Dynamic request proxy to Flask        location / {
            proxy_pass http://flask_app;
            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;
        }
    }
}

4.2 Start Nginx

# Check configuration syntaxnginx -t

# Start Nginxstart nginx

# Restart (after modifying the configuration)nginx -s reload

5. Configure Windows Services (Self-Start on)

5.1 Using NSSM Tools (recommended)

  • download
  • Register for Nginx Services:
nssm install Nginx "C:\nginx\"  # Replace with Nginx actual pathnssm start Nginx
  • Sign up for Waitress Services:
nssm install FlaskApp "C:\flask-app\venv\Scripts\" "C:\flask-app\venv\Scripts\" --port=5000 app:app
nssm start FlaskApp

6. FAQs and Solutions

6.1 Port conflict

  • mistakebind() to 0.0.0.0:5000 failed: Only one socket address is allowed
  • solve: Check whether other programs occupy ports and usenetstat -ano | findstr :5000and terminate the process.

6.2 Static File 404

  • mistake: The browser cannot load/static/
  • solve: Ensure that the Nginx configuration is inaliasThe path uses forward slashes (C:/flask-app/static), and the directory exists.

6.3 Nginx 502 Bad Gateway

  • reason: Nginx cannot connect to the backend Flask service.
  • Troubleshooting:
    1. Check if Waitress is running (tasklist | findstr waitress)。
    2. Check whether the firewall allows port 5000.

7. Extend knowledge

7.1 Enable HTTPS

  1. Use Let’s Encrypt to get a free certificate (domain name required).
  2. Modify Nginx configuration:
server {
    listen 443 ssl;
    ssl_certificate     C:/ssl/your_domain.crt;
    ssl_certificate_key C:/ssl/your_domain.key;
    # ...Other configurations}

7.2 Load balancing

upstream flask_app {
    server 127.0.0.1:5000;
    server 127.0.0.1:5001;  # Start the second instance    least_conn;  # Use the least connection algorithm}

8. Things to note

  1. Debug mode is disabled in production environment: Make sure Flask's(debug=False)
  2. Log Management: Configure the log paths of Nginx and Flask and archive them regularly.
  3. Backup configuration: Backup before modifying key files (such as)。

Through the above steps, the Flask application will run in a high-reliability manner on Windows Server, and efficient reverse proxy and static resource acceleration will be achieved through Nginx.

The above is the detailed content of the entire process of using Nginx to deploy Flask applications in Windows Server environment. For more information about Nginx to deploy Flask applications, please follow my other related articles!