tom*_*mas 3 nginx nginx-reverse-proxy nginx-config
我已经下载了版本 1.21.6 的 nginx windows 映像(https://nginx.org/en/download.html),nginx -V输出包含--with-http_sub_module:
PS C:\Utils\nginx-1.21.6> .\nginx.exe -V
nginx version: nginx/1.21.6
built by cl 16.00.40219.01 for 80x86
built with OpenSSL 1.1.1m 14 Dec 2021
TLS SNI support enabled
configure arguments: --with-cc=cl --builddir=objs.msvc8 --with-debug --prefix= --conf-path=conf/nginx.conf --pid-path=logs/nginx.pid --http-log-path=logs/access.log --error-log-path=logs/error.log --sbin-path=nginx.exe --http-client-body-temp-path=temp/client_body_temp --http-proxy-temp-path=temp/proxy_temp --http-fastcgi-temp-path=temp/fastcgi_temp --http-scgi-temp-path=temp/scgi_temp --http-uwsgi-temp-path=temp/uwsgi_temp --with-cc-opt=-DFD_SETSIZE=1024 --with-pcre=objs.msvc8/lib/pcre2-10.39 --with-zlib=objs.msvc8/lib/zlib-1.2.11 --with-http_v2_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_stub_status_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_slice_module --with-mail --with-stream --with-openssl=objs.msvc8/lib/openssl-1.1.1m --with-openssl-opt='no-asm no-tests -D_WIN32_WINNT=0x0501' --with-http_ssl_module --with-mail_ssl_module --with-stream_ssl_module
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法进行替换:(我试图在这里获得一些灵感:https ://samanbaoli.medium.com/modify-html-pages-on-the-fly-using-nginx-2e7a2d069086
这是我的配置
worker_processes 1;
worker_rlimit_nofile 8192;
pid nginx.pid;
events {
worker_connections 24;
}
http {
server {
listen 80;
server_name localhost;
location / {
proxy_pass https://example.org;
sub_filter '</head>' '<script>alert("Hi")</script></head>';
sub_filter_once on;
}
location /test {
return 200 'OKAY';
sub_filter 'OKAY' 'OK';
sub_filter_once on;
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么想法,我做错了什么吗?
http://localhost/ 不会抛出警报“Hi”,http://localhost/test 返回“OKAY”,而不是预期的“OK”。
根据这个答案,不应该需要任何参数或额外的配置:( Nginx,如何在启用 ngx_http_sub_module 的情况下启动服务
除非通过指令Content-Type显式指定default_type(在该位置或任何级别上),否则 HTTP 标头将等于text/plain默认值。该sub_filter指令仅适用于text/html MIME 类型的内容,除非使用该指令指定了其他类型sub_filter_types。因此,要使您的替换在该/test位置起作用,请使用
location /test {
default_type text/html;
return 200 'OKAY';
sub_filter 'OKAY' 'OK';
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您没有default_type指定其他类型的指令text/plain,
location /test {
return 200 'OKAY';
sub_filter_types text/plain;
sub_filter 'OKAY' 'OK';
}
Run Code Online (Sandbox Code Playgroud)
我看不出有任何理由替换在您的主要位置不起作用。检查从 HTML 标记返回的实际 MIME 类型https://example.org以及使用的大写/小写。是真的</head>吗</HEAD>?
更新
正如OP所注意到的,该sub_filter模块不适用于压缩的上游响应,因此如果上游能够压缩其响应,则Accept-Encoding标头不应传递给上游:
location / {
proxy_pass https://example.org;
proxy_set_header Accept-Encoding "";
sub_filter '</head>' '<script>alert("Hi")</script></head>';
sub_filter_once on;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7075 次 |
| 最近记录: |