我的網址是這樣的:
http://myserver/app/inf?ConId=Obj:com.aaa.bbb:3712 # Only IP in whitelist can access
http://myserver/app/...... # all user can access
當 ConId 的引數是Obj:com.aaa.bbb:3712時,我需要限制只有特定的 IP 可以訪問我的服務器。
我嘗試了以下 Nginx 配置但無法正常作業。
location / {
if ( $arg_ConId = "Obj:com.aaa.bbb:3712" ) {
allow 192.168.1.104;
deny all;
}
proxy_pass http://192.168.234.130:80;
add_header Access-Control-Allow-Origin *;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
allow all;
}
請幫忙,謝謝!
謝謝@araisch,我最后的作業 Nginx 配置是:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
error_page 403 /403.html;
location = /403.html {
root html;
}
if ($arg_ConId = "Obj:com.aaa.bbb:3712") {
set $BLOCKING A;
}
if ($remote_addr != "192.168.3.11") {
set $BLOCKING "${BLOCKING}B";
}
if ($BLOCKING = AB) {
return 403;
break;
}
location / {
proxy_pass http://192.168.234.130:80;
add_header Access-Control-Allow-Origin *;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
allow all;
}
}
}
uj5u.com熱心網友回復:
可以使用類似的東西:
if ($arg_ConId = "Obj:com.aaa.bbb:3712") {
set $BLOCKING A;
}
if ($remote_addr != 192.168.1.104) {
set $BLOCKING "${BLOCKING}B";
}
if ($BLOCKING = AB) {
return 403;
break;
}
在server塊中。
您的代碼中的問題:
iflocation由于 nginx 的奇怪宣告規則,指令被認為是邪惡的。他們大部分時間都在做奇怪的事情,所以盡量避免。- $arg_ContainerOID 沒有捕獲名為“ConId”的引數
備注:這在橋接模式下的 dockerized nginx 中不起作用,因為真實 IP 被防火墻屏蔽了。
uj5u.com熱心網友回復:
你可以使用這樣的東西:
location / {
auth_request /auth-here;
}
location /auth-here {
internal;
proxy_pass http://192.168.234.130:80/auth.php;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}
然后在您的腳本中,您可以檢查$_SERVER['HTTP_X_ORIGINAL_URI']并回傳 HTTP 200 以允許請求或回傳 HTTP 403 以拒絕請求。
如檔案中所述,您將需要http_auth_request_module上述內容才能正常作業。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/445748.html
標籤:nginx nginx-反向代理 白名单
