Explorar el Código

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 hace 3 meses
padre
commit
5632c60c78
Se han modificado 3 ficheros con 269 adiciones y 0 borrados
  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 @@
1
+events {
2
+    worker_connections 1024;
3
+}
4
+
5
+http {
6
+    include       mime.types;
7
+    default_type  application/octet-stream;
8
+
9
+    # Logging
10
+    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
11
+                    '$status $body_bytes_sent "$http_referer" '
12
+                    '"$http_user_agent" "$http_x_forwarded_for"';
13
+
14
+    access_log logs/access.log main;
15
+    error_log logs/error.log;
16
+
17
+    # Basic settings
18
+    sendfile        on;
19
+    tcp_nopush      on;
20
+    tcp_nodelay     on;
21
+    keepalive_timeout 65;
22
+    types_hash_max_size 2048;
23
+
24
+    # Gzip compression
25
+    gzip on;
26
+    gzip_vary on;
27
+    gzip_min_length 1024;
28
+    gzip_types
29
+        text/plain
30
+        text/css
31
+        text/xml
32
+        text/javascript
33
+        application/json
34
+        application/javascript
35
+        application/xml+rss
36
+        application/atom+xml;
37
+
38
+    # Rate limiting (optional)
39
+    limit_req_zone $binary_remote_addr zone=api:10m rate=10r/s;
40
+    limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m;
41
+
42
+    # Upstream server (your Node.js API)
43
+    upstream market_data_api {
44
+        server localhost:3001;
45
+        keepalive 32;
46
+    }
47
+
48
+    # HTTP server block for port 80
49
+    server {
50
+        listen       80;
51
+        server_name  marketData;
52
+
53
+        # Security headers
54
+        add_header X-Frame-Options DENY;
55
+        add_header X-Content-Type-Options nosniff;
56
+        add_header X-XSS-Protection "1; mode=block";
57
+
58
+        # Proxy settings for API calls
59
+        location /api/ {
60
+            # Apply rate limiting
61
+            limit_req zone=api burst=20 nodelay;
62
+
63
+            # Proxy to Node.js server
64
+            proxy_pass http://market_data_api/;
65
+            proxy_http_version 1.1;
66
+            proxy_set_header Upgrade $http_upgrade;
67
+            proxy_set_header Connection 'upgrade';
68
+            proxy_set_header Host $host;
69
+            proxy_set_header X-Real-IP $remote_addr;
70
+            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
71
+            proxy_set_header X-Forwarded-Proto $scheme;
72
+            proxy_cache_bypass $http_upgrade;
73
+
74
+            # Timeout settings
75
+            proxy_connect_timeout 60s;
76
+            proxy_send_timeout 60s;
77
+            proxy_read_timeout 60s;
78
+
79
+            # Buffer settings for large payloads
80
+            proxy_buffering on;
81
+            proxy_buffer_size 4k;
82
+            proxy_buffers 8 4k;
83
+            proxy_busy_buffers_size 8k;
84
+            client_max_body_size 50M;
85
+
86
+            # Enable all HTTP methods
87
+            if ($request_method !~ ^(GET|HEAD|POST|PUT|DELETE|OPTIONS)$ ) {
88
+                return 405;
89
+            }
90
+        }
91
+
92
+        # Health check endpoint (optional)
93
+        location /health {
94
+            proxy_pass http://market_data_api/health;
95
+            proxy_http_version 1.1;
96
+            proxy_set_header Host $host;
97
+            proxy_set_header X-Real-IP $remote_addr;
98
+            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
99
+            proxy_set_header X-Forwarded-Proto $scheme;
100
+        }
101
+
102
+        # Root location - serve a simple status page or redirect
103
+        location / {
104
+            return 200 "Nginx is running. API endpoints available at /api/\n";
105
+            add_header Content-Type text/plain;
106
+        }
107
+
108
+        # Error pages
109
+        error_page   500 502 503 504  /50x.html;
110
+        location = /50x.html {
111
+            root   html;
112
+        }
113
+    }
114
+
115
+    # Optional HTTPS server block (for future use)
116
+    # server {
117
+    #     listen       443 ssl http2;
118
+    #     server_name  localhost;
119
+    #
120
+    #     # SSL configuration would go here
121
+    #
122
+    #     location /api/ {
123
+    #         proxy_pass http://market_data_api/;
124
+    #         # ... same proxy settings as above
125
+    #     }
126
+    # }
127
+}

+ 108 - 0
start_proxy.bat

@@ -0,0 +1,108 @@
1
+@echo off
2
+setlocal enabledelayedexpansion
3
+
4
+echo ========================================
5
+echo    Market Data Proxy Starter
6
+echo ========================================
7
+echo.
8
+
9
+REM Check if running as administrator (needed for hosts file modification)
10
+net session >nul 2>&1
11
+if %errorLevel% == 0 (
12
+    echo [✓] Running as administrator
13
+) else (
14
+    echo [✗] This script requires administrator privileges
15
+    echo    Right-click and "Run as administrator"
16
+    echo.
17
+    pause
18
+    exit /b 1
19
+)
20
+
21
+echo.
22
+echo [1/4] Adding marketData to hosts file...
23
+set "hosts_file=%windir%\System32\drivers\etc\hosts"
24
+set "entry=127.0.0.1 marketData"
25
+
26
+REM Check if entry already exists
27
+findstr /C:"marketData" "%hosts_file%" >nul 2>&1
28
+if !errorLevel! == 0 (
29
+    echo [✓] marketData already exists in hosts file
30
+) else (
31
+    echo. >> "%hosts_file%"
32
+    echo !entry! >> "%hosts_file%"
33
+    echo [✓] Added marketData to hosts file
34
+)
35
+
36
+echo.
37
+echo [2/4] Starting Node.js API server...
38
+echo Starting API on port 3001...
39
+
40
+REM Get current directory and store it
41
+set "PROJECT_DIR=%cd%"
42
+start "MarketDataAPI" cmd /k "cd /d "%PROJECT_DIR%" && npm start"
43
+
44
+REM Wait a moment for API to start
45
+timeout /t 5 /nobreak >nul
46
+
47
+echo.
48
+echo [3/4] Starting Nginx proxy server...
49
+
50
+REM Get current directory for nginx path
51
+set "NGINX_PATH=%cd%\nginx-1.24.0\nginx.exe"
52
+start "NginxProxy" "%NGINX_PATH%"
53
+
54
+REM Wait for Nginx to start
55
+timeout /t 3 /nobreak >nul
56
+
57
+echo.
58
+echo [4/4] Testing proxy connection...
59
+echo Testing basic connectivity...
60
+curl -s http://marketData >nul 2>&1
61
+if !errorLevel! == 0 (
62
+    echo [✓] Proxy server is responding
63
+) else (
64
+    echo [✗] Proxy server not responding
65
+    goto :error
66
+)
67
+
68
+echo.
69
+echo Testing API through proxy...
70
+curl -s http://marketData/api/health >nul 2>&1
71
+if !errorLevel! == 0 (
72
+    echo [✓] API proxy is working correctly
73
+) else (
74
+    echo [✗] API proxy not working
75
+    goto :error
76
+)
77
+
78
+echo.
79
+echo ========================================
80
+echo    SUCCESS! Proxy is ready!
81
+echo ========================================
82
+echo.
83
+echo Your proxy is now running:
84
+echo - API Server: http://localhost:3001
85
+echo - Proxy URL:  http://marketData
86
+echo - MT5 URL:   http://marketData/api/endpoint
87
+echo.
88
+echo You can now use http://marketData/api/ in your MT5 EA
89
+echo.
90
+echo Press any key to close this window...
91
+pause >nul
92
+goto :eof
93
+
94
+:error
95
+echo.
96
+echo ========================================
97
+echo    ERROR! Something went wrong
98
+echo ========================================
99
+echo.
100
+echo Please check:
101
+echo 1. Is Node.js installed?
102
+echo 2. Run 'npm install' if dependencies are missing
103
+echo 3. Check if port 3001 is available
104
+echo 4. Check Nginx error logs in nginx-1.24.0/logs/
105
+echo.
106
+echo Press any key to close this window...
107
+pause >nul
108
+exit /b 1

+ 34 - 0
stop_proxy.bat

@@ -0,0 +1,34 @@
1
+@echo off
2
+echo ========================================
3
+echo    Market Data Proxy Stopper
4
+echo ========================================
5
+echo.
6
+
7
+echo Stopping Nginx...
8
+REM Find and kill nginx process
9
+taskkill /f /im nginx.exe >nul 2>&1
10
+if !errorLevel! == 0 (
11
+    echo [✓] Nginx stopped
12
+) else (
13
+    echo [ℹ] Nginx was not running
14
+)
15
+
16
+echo.
17
+echo Stopping Node.js API server...
18
+REM Find and kill node processes (npm start creates node.exe)
19
+taskkill /f /im node.exe >nul 2>&1
20
+if !errorLevel! == 0 (
21
+    echo [✓] Node.js API server stopped
22
+) else (
23
+    echo [ℹ] Node.js API server was not running
24
+)
25
+
26
+echo.
27
+echo ========================================
28
+echo    All services stopped!
29
+echo ========================================
30
+echo.
31
+echo You can now safely close applications or restart services.
32
+echo.
33
+echo Press any key to close this window...
34
+pause >nul