Asrasahar commited on
Commit
139bd0d
·
verified ·
1 Parent(s): 661780d

Update nginx.conf.template

Browse files
Files changed (1) hide show
  1. nginx.conf.template +60 -79
nginx.conf.template CHANGED
@@ -2,6 +2,9 @@
2
 
3
  worker_processes auto;
4
 
 
 
 
5
  events {
6
  worker_connections 1024;
7
  }
@@ -10,118 +13,96 @@ http {
10
  include /etc/nginx/mime.types;
11
  default_type application/octet-stream;
12
 
13
- client_body_temp_path /tmp/client_body_01;
14
- proxy_temp_path /tmp/proxy_01;
15
- fastcgi_temp_path /tmp/fastcgi_01;
16
- uwsgi_temp_path /tmp/uwsgi_01;
17
- scgi_temp_path /tmp/scgi_01;
 
18
 
 
19
  access_log /dev/stdout;
20
- error_log /dev/stderr notice;
21
 
22
  sendfile on;
23
  tcp_nopush on;
24
  tcp_nodelay on;
25
  keepalive_timeout 65;
26
  types_hash_max_size 2048;
 
27
 
28
  upstream target_huggingface_space {
 
29
  server ${TARGET_HOSTNAME_NO_SCHEME}:443;
 
30
  }
31
 
32
  server {
33
  listen 7860 default_server;
34
- server_name _;
35
-
36
- proxy_hide_header X-Frame-Options;
37
- proxy_hide_header Content-Security-Policy;
38
- add_header Content-Security-Policy "frame-ancestors *; script-src 'self' 'unsafe-inline' https: data: blob:; object-src 'none'; base-uri 'self';" always;
 
 
 
 
 
 
 
 
 
 
 
39
  add_header X-Content-Type-Options "nosniff" always;
 
 
40
 
 
41
  sub_filter_types text/html;
42
- sub_filter_once on; # مهم: فقط اولین <head> را هدف قرار بده
43
-
44
- # اسکریپت کامل‌تر برای غیرفعال کردن تمام متدهای console
45
- set $console_disabler_script_full "<script id='hf-proxy-console-disabler-full'>
46
- (function() {
47
- /* برای تست می‌توانید این alert را فعال کنید: */
48
- /* alert('NGINX PROXY: FULL Console disabler script executing!'); */
49
-
50
- if (window.hfProxyConsoleFullyDisabled) return;
51
- window.hfProxyConsoleFullyDisabled = true;
52
-
53
- var preservedConsoleLogForProxy;
54
- try {
55
- if (window.console && typeof window.console.log === 'function') {
56
- preservedConsoleLogForProxy = window.console.log.bind(window.console);
57
- } else {
58
- preservedConsoleLogForProxy = function(){};
59
- }
60
- } catch (e) {
61
- preservedConsoleLogForProxy = function(){};
62
- }
63
-
64
- /* preservedConsoleLogForProxy('PROXY SCRIPT: Full console disabler is EXECUTING NOW!'); */
65
-
66
- var noop = function() {};
67
- var consoleMethods = [
68
- 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',
69
- 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline',
70
- 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'
71
- ];
72
- var newConsoleReplacement = {}; /* نام متغیر را تغییر دادم تا با newConsole احتمالی در scope دیگر تداخل نکند */
73
- for (var i = 0; i < consoleMethods.length; i++) {
74
- newConsoleReplacement[consoleMethods[i]] = noop;
75
- }
76
-
77
- try {
78
- if (typeof window.console !== 'undefined') {
79
- Object.defineProperty(window, 'console', {
80
- value: newConsoleReplacement,
81
- writable: false, /* سعی کن از بازنویسی مجدد جلوگیری کنی */
82
- configurable: false /* سعی کن از حذف یا تغییر پیکربندی جلوگیری کنی */
83
- });
84
- } else {
85
- Object.defineProperty(window, 'console', {
86
- value: newConsoleReplacement,
87
- writable: false,
88
- configurable: false
89
- });
90
- }
91
- /* preservedConsoleLogForProxy('PROXY SCRIPT: window.console should now be fully replaced by no-ops.'); */
92
- } catch (e) {
93
- /* preservedConsoleLogForProxy('PROXY SCRIPT: Error while trying to replace window.console: ' + e); */
94
- window.console = newConsoleReplacement; // Fallback to simple assignment
95
- }
96
- })();
97
- </script>";
98
-
99
- # تزریق بلافاصله بعد از تگ <head>. این بهترین شانس ماست.
100
  sub_filter '<head>' '<head>$console_disabler_script_full';
 
101
 
102
  location / {
103
- proxy_pass https://target_huggingface_space;
104
- proxy_set_header Host ${TARGET_HOSTNAME_NO_SCHEME};
 
 
105
  proxy_set_header X-Real-IP $remote_addr;
106
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
107
  proxy_set_header X-Forwarded-Proto $scheme;
108
- proxy_set_header X-Forwarded-Host $host;
109
- proxy_set_header X-Forwarded-Port $server_port;
 
110
  proxy_http_version 1.1;
111
- proxy_set_header Connection "";
112
- proxy_buffering on;
 
 
 
 
 
113
  }
114
 
115
- location = /ws {
116
- proxy_pass https://target_huggingface_space/ws;
117
  proxy_http_version 1.1;
118
  proxy_set_header Upgrade $http_upgrade;
119
  proxy_set_header Connection "upgrade";
120
- proxy_set_header Host ${TARGET_HOSTNAME_NO_SCHEME};
121
  proxy_set_header X-Real-IP $remote_addr;
122
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
123
  proxy_set_header X-Forwarded-Proto $scheme;
124
- proxy_connect_timeout 7d;
 
 
125
  proxy_send_timeout 7d;
126
  proxy_read_timeout 7d;
127
  }
 
2
 
3
  worker_processes auto;
4
 
5
+ # مسیر فایل PID در اینجا تنظیم نمی‌شود، چون از طریق -g در entrypoint.sh پاس داده می‌شود.
6
+ # pid /var/run/nginx.pid; # این خط غیرفعال است
7
+
8
  events {
9
  worker_connections 1024;
10
  }
 
13
  include /etc/nginx/mime.types;
14
  default_type application/octet-stream;
15
 
16
+ # مسیرهای موقت برای Nginx، مطمئن شوید /tmp قابل نوشتن است (که معمولا هست)
17
+ client_body_temp_path /tmp/client_body_temp 1 2;
18
+ proxy_temp_path /tmp/proxy_temp 1 2;
19
+ fastcgi_temp_path /tmp/fastcgi_temp 1 2;
20
+ uwsgi_temp_path /tmp/uwsgi_temp 1 2;
21
+ scgi_temp_path /tmp/scgi_temp 1 2;
22
 
23
+ # لاگ‌ها به stdout و stderr هدایت می‌شوند تا در لاگ‌های کانتینر نمایش داده شوند
24
  access_log /dev/stdout;
25
+ error_log /dev/stderr notice; # می‌توانید سطح لاگ را به warn یا error تغییر دهید
26
 
27
  sendfile on;
28
  tcp_nopush on;
29
  tcp_nodelay on;
30
  keepalive_timeout 65;
31
  types_hash_max_size 2048;
32
+ # server_tokens off; # هدر Server را با جزئیات نسخه Nginx مخفی می‌کند (اختیاری)
33
 
34
  upstream target_huggingface_space {
35
+ # ${TARGET_HOSTNAME_NO_SCHEME} توسط entrypoint.sh از TARGET_HF_SPACE_URL استخراج می‌شود
36
  server ${TARGET_HOSTNAME_NO_SCHEME}:443;
37
+ # keepalive 16; # برای بهبود عملکرد با استفاده مجدد از کانکشن‌ها (اختیاری)
38
  }
39
 
40
  server {
41
  listen 7860 default_server;
42
+ server_name _; # به هر هاست‌نیمی پاسخ می‌دهد
43
+
44
+ # --- مهم: این بخش هدرهای مشخصی را از پاسخ سرور هدف حذف می‌کند ---
45
+ proxy_hide_header Link; # این هدر URL کنونیکال را فاش می‌کند
46
+ proxy_hide_header X-Canonical-Url; # برای اطمینان، اگر هدر دیگری هم بود
47
+ proxy_hide_header X-Powered-By; # اطلاعات سرور بک‌اند را مخفی می‌کند
48
+ proxy_hide_header Server; # اطلاعات سرور بک‌اند را مخفی می‌کند (اگر upstream خودش تنظیم نکند)
49
+ # --- پایان بخش مهم ---
50
+
51
+ # هدرهای امنیتی که شما اضافه کرده بودید، حفظ شده‌اند
52
+ proxy_hide_header X-Frame-Options; # از پاسخ بک‌اند حذف شود تا add_header ما اعمال شود
53
+ proxy_hide_header Content-Security-Policy; # از پاسخ بک‌اند حذف شود تا add_header ما اعمال شود
54
+
55
+ # اضافه کردن هدرهای امنیتی توسط پروکسی
56
+ add_header X-Frame-Options "SAMEORIGIN" always; # یا "DENY" اگر نمی‌خواهید در هیچ iframe ای نمایش داده شود
57
+ add_header Content-Security-Policy "frame-ancestors 'self' *; script-src 'self' 'unsafe-inline' https: data: blob:; object-src 'none'; base-uri 'self';" always;
58
  add_header X-Content-Type-Options "nosniff" always;
59
+ add_header Referrer-Policy "strict-origin-when-cross-origin" always;
60
+ # add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; # اگر HTTPS همیشه فعال است
61
 
62
+ # تنظیمات sub_filter برای تزریق اسکریپت (فقط برای محتوای text/html)
63
  sub_filter_types text/html;
64
+ sub_filter_once on; # فقط اولین تطابق را جایگزین می‌کند (مهم برای <head>)
65
+
66
+ # اسکریپت کامل‌تر برای غیرفعال کردن تمام متدهای console (همانطور که شما داشتید)
67
+ set $console_disabler_script_full "<script id='hf-proxy-console-disabler-full'>\n(function() {\n if (window.hfProxyConsoleFullyDisabled) return;\n window.hfProxyConsoleFullyDisabled = true;\n var preservedConsoleLogForProxy;\n try {\n if (window.console && typeof window.console.log === 'function') {\n preservedConsoleLogForProxy = window.console.log.bind(window.console);\n } else {\n preservedConsoleLogForProxy = function(){};\n }\n } catch (e) {\n preservedConsoleLogForProxy = function(){};\n }\n var noop = function() {};\n var consoleMethods = [\n 'assert', 'clear', 'count', 'debug', 'dir', 'dirxml', 'error',\n 'group', 'groupCollapsed', 'groupEnd', 'info', 'log', 'markTimeline',\n 'profile', 'profileEnd', 'table', 'time', 'timeEnd', 'timeStamp', 'trace', 'warn'\n ];\n var newConsoleReplacement = {};\n for (var i = 0; i < consoleMethods.length; i++) {\n newConsoleReplacement[consoleMethods[i]] = noop;\n }\n try {\n if (typeof window.console !== 'undefined') {\n Object.defineProperty(window, 'console', {\n value: newConsoleReplacement,\n writable: false,\n configurable: false\n });\n } else {\n Object.defineProperty(window, 'console', {\n value: newConsoleReplacement,\n writable: false,\n configurable: false\n });\n }\n } catch (e) {\n window.console = newConsoleReplacement;\n }\n})();\n</script>";
68
+
69
+ # تزریق اسکریپت غیرفعال‌کننده کنسول بلافاصله بعد از تگ <head>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
  sub_filter '<head>' '<head>$console_disabler_script_full';
71
+ # sub_filter '</head>' '$console_disabler_script_full</head>'; # جایگزین اگر <head> صفات داشته باشد
72
 
73
  location / {
74
+ proxy_pass https://target_huggingface_space; # از upstream تعریف شده استفاده می‌کند
75
+
76
+ # تنظیم هدرهای مهم برای ارسال به سرور هدف
77
+ proxy_set_header Host ${TARGET_HOSTNAME_NO_SCHEME}; # بسیار مهم: هاست اسپیس اصلی
78
  proxy_set_header X-Real-IP $remote_addr;
79
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
80
  proxy_set_header X-Forwarded-Proto $scheme;
81
+ proxy_set_header X-Forwarded-Host $host; # هاست پروکسی فعلی (مثلا your-proxy-space.hf.space)
82
+ proxy_set_header X-Forwarded-Port $server_port; # پورت پروکسی فعلی
83
+
84
  proxy_http_version 1.1;
85
+ proxy_set_header Connection ""; # برای مدیریت بهتر کانکشن‌ها توسط Nginx
86
+ proxy_buffering on; # بافر کردن پاسخ از بک‌اند می‌تواند عملکرد را بهبود بخشد
87
+
88
+ # تایم‌اوت‌ها (اختیاری، مقادیر پیش‌فرض معمولا مناسب هستند)
89
+ # proxy_connect_timeout 60s;
90
+ # proxy_send_timeout 60s;
91
+ # proxy_read_timeout 60s;
92
  }
93
 
94
+ location = /ws { # مسیر WebSocket (اگر اسپیس شما از WebSocket استفاده می‌کند)
95
+ proxy_pass https://target_huggingface_space/ws; # مطمئن شوید مسیر /ws در اسپیس هدف صحیح است
96
  proxy_http_version 1.1;
97
  proxy_set_header Upgrade $http_upgrade;
98
  proxy_set_header Connection "upgrade";
99
+ proxy_set_header Host ${TARGET_HOSTNAME_NO_SCHEME}; # هاست اسپیس اصلی
100
  proxy_set_header X-Real-IP $remote_addr;
101
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
102
  proxy_set_header X-Forwarded-Proto $scheme;
103
+
104
+ # تایم‌اوت‌های طولانی‌تر برای WebSocket
105
+ proxy_connect_timeout 7d;
106
  proxy_send_timeout 7d;
107
  proxy_read_timeout 7d;
108
  }