Переглянути джерело

feat: Add Nginx reverse proxy for MT5 compatibility

- Add Nginx configuration (nginx-1.24.0/) that proxies requests from port 80 to port 3001
- Create start_proxy.bat script for one-click setup and startup
- Create stop_proxy.bat script for clean shutdown
- Configure proxy to handle all REST methods (GET, POST, PUT, DELETE)
- Support JSON payloads and proper headers
- Add marketData to hosts file automatically
- Enable MT5 to call API using http://marketData/api/endpoint instead of port 3001

Resolves MT5 WebRequest() port restriction by proxying through allowed port 80
uzairrizwan1 9 місяців тому
батько
коміт
5632c60c78
3 змінених файлів з 269 додано та 0 видалено
  1. 127 0
      nginx-1.24.0/conf/nginx.conf
  2. 108 0
      start_proxy.bat
  3. 34 0
      stop_proxy.bat

+ 127 - 0
nginx-1.24.0/conf/nginx.conf

@@ -0,0 +1,127 @@
+events {
+    worker_connections 1024;
+}
+
+http {
+    include       mime.types;
+    default_type  application/octet-stream;
+
+    # Logging
+    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;
+
+    # Basic settings
+    sendfile        on;
+    tcp_nopush      on;
+    tcp_nodelay     on;
+    keepalive_timeout 65;
+    types_hash_max_size 2048;
+
+    # Gzip compression
+    gzip on;
+    gzip_vary on;
+    gzip_min_length 1024;
+    gzip_types
+        text/plain
+        text/css
+        text/xml
+        text/javascript
+        application/json
+        application/javascript
+        application/xml+rss
+        application/atom+xml;
+
+    # Rate limiting (optional)
+    limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
+    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
+
+    # Upstream server (your Node.js API)
+    upstream market_data_api {
+        server localhost:3001;
+        keepalive 32;
+    }
+
+    # HTTP server block for port 80
+    server {
+        listen       80;
+        server_name  marketData;
+
+        # Security headers
+        add_header X-Frame-Options DENY;
+        add_header X-Content-Type-Options nosniff;
+        add_header X-XSS-Protection "1; mode=block";
+
+        # Proxy settings for API calls
+        location /api/ {
+            # Apply rate limiting
+            limit_req zone=api burst=20 nodelay;
+
+            # Proxy to Node.js server
+            proxy_pass http://market_data_api/;
+            proxy_http_version 1.1;
+            proxy_set_header Upgrade $http_upgrade;
+            proxy_set_header Connection 'upgrade';
+            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_cache_bypass $http_upgrade;
+
+            # Timeout settings
+            proxy_connect_timeout 60s;
+            proxy_send_timeout 60s;
+            proxy_read_timeout 60s;
+
+            # Buffer settings for large payloads
+            proxy_buffering on;
+            proxy_buffer_size 4k;
+            proxy_buffers 8 4k;
+            proxy_busy_buffers_size 8k;
+            client_max_body_size 50M;
+
+            # Enable all HTTP methods
+            if ($request_method !~ ^(GET|HEAD|POST|PUT|DELETE|OPTIONS)$ ) {
+                return 405;
+            }
+        }
+
+        # Health check endpoint (optional)
+        location /health {
+            proxy_pass http://market_data_api/health;
+            proxy_http_version 1.1;
+            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;
+        }
+
+        # Root location - serve a simple status page or redirect
+        location / {
+            return 200 "Nginx is running. API endpoints available at /api/\n";
+            add_header Content-Type text/plain;
+        }
+
+        # Error pages
+        error_page   500 502 503 504  /50x.html;
+        location = /50x.html {
+            root   html;
+        }
+    }
+
+    # Optional HTTPS server block (for future use)
+    # server {
+    #     listen       443 ssl http2;
+    #     server_name  localhost;
+    #
+    #     # SSL configuration would go here
+    #
+    #     location /api/ {
+    #         proxy_pass http://market_data_api/;
+    #         # ... same proxy settings as above
+    #     }
+    # }
+}

+ 108 - 0
start_proxy.bat

@@ -0,0 +1,108 @@
+@echo off
+setlocal enabledelayedexpansion
+
+echo ========================================
+echo    Market Data Proxy Starter
+echo ========================================
+echo.
+
+REM Check if running as administrator (needed for hosts file modification)
+net session >nul 2>&1
+if %errorLevel% == 0 (
+    echo [✓] Running as administrator
+) else (
+    echo [✗] This script requires administrator privileges
+    echo    Right-click and "Run as administrator"
+    echo.
+    pause
+    exit /b 1
+)
+
+echo.
+echo [1/4] Adding marketData to hosts file...
+set "hosts_file=%windir%\System32\drivers\etc\hosts"
+set "entry=127.0.0.1 marketData"
+
+REM Check if entry already exists
+findstr /C:"marketData" "%hosts_file%" >nul 2>&1
+if !errorLevel! == 0 (
+    echo [✓] marketData already exists in hosts file
+) else (
+    echo. >> "%hosts_file%"
+    echo !entry! >> "%hosts_file%"
+    echo [✓] Added marketData to hosts file
+)
+
+echo.
+echo [2/4] Starting Node.js API server...
+echo Starting API on port 3001...
+
+REM Get current directory and store it
+set "PROJECT_DIR=%cd%"
+start "MarketDataAPI" cmd /k "cd /d "%PROJECT_DIR%" && npm start"
+
+REM Wait a moment for API to start
+timeout /t 5 /nobreak >nul
+
+echo.
+echo [3/4] Starting Nginx proxy server...
+
+REM Get current directory for nginx path
+set "NGINX_PATH=%cd%\nginx-1.24.0\nginx.exe"
+start "NginxProxy" "%NGINX_PATH%"
+
+REM Wait for Nginx to start
+timeout /t 3 /nobreak >nul
+
+echo.
+echo [4/4] Testing proxy connection...
+echo Testing basic connectivity...
+curl -s http://marketData >nul 2>&1
+if !errorLevel! == 0 (
+    echo [✓] Proxy server is responding
+) else (
+    echo [✗] Proxy server not responding
+    goto :error
+)
+
+echo.
+echo Testing API through proxy...
+curl -s http://marketData/api/health >nul 2>&1
+if !errorLevel! == 0 (
+    echo [✓] API proxy is working correctly
+) else (
+    echo [✗] API proxy not working
+    goto :error
+)
+
+echo.
+echo ========================================
+echo    SUCCESS! Proxy is ready!
+echo ========================================
+echo.
+echo Your proxy is now running:
+echo - API Server: http://localhost:3001
+echo - Proxy URL:  http://marketData
+echo - MT5 URL:   http://marketData/api/endpoint
+echo.
+echo You can now use http://marketData/api/ in your MT5 EA
+echo.
+echo Press any key to close this window...
+pause >nul
+goto :eof
+
+:error
+echo.
+echo ========================================
+echo    ERROR! Something went wrong
+echo ========================================
+echo.
+echo Please check:
+echo 1. Is Node.js installed?
+echo 2. Run 'npm install' if dependencies are missing
+echo 3. Check if port 3001 is available
+echo 4. Check Nginx error logs in nginx-1.24.0/logs/
+echo.
+echo Press any key to close this window...
+pause >nul
+exit /b 1

+ 34 - 0
stop_proxy.bat

@@ -0,0 +1,34 @@
+@echo off
+echo ========================================
+echo    Market Data Proxy Stopper
+echo ========================================
+echo.
+
+echo Stopping Nginx...
+REM Find and kill nginx process
+taskkill /f /im nginx.exe >nul 2>&1
+if !errorLevel! == 0 (
+    echo [✓] Nginx stopped
+) else (
+    echo [ℹ] Nginx was not running
+)
+
+echo.
+echo Stopping Node.js API server...
+REM Find and kill node processes (npm start creates node.exe)
+taskkill /f /im node.exe >nul 2>&1
+if !errorLevel! == 0 (
+    echo [✓] Node.js API server stopped
+) else (
+    echo [ℹ] Node.js API server was not running
+)
+
+echo.
+echo ========================================
+echo    All services stopped!
+echo ========================================
+echo.
+echo You can now safely close applications or restart services.
+echo.
+echo Press any key to close this window...
+pause >nul