php x accel redirect,Nginx-xaccel重定向命名位置uri

在Python应用中,通过nginxx-accel-redirect进行外部资源的身份验证,遇到HTTP方法被转发为GET的问题。当前配置下,所有x-accel-redirect请求都被当作GET处理。尝试利用nginx的命名位置来转发原始HTTP方法,但未能成功,重定向始终指向“@resource/”而非期望的“@resource/name”。nginx配置中包括了对/getresource/、/resource/和@resource/的location定义,但问题仍未解决。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我使用nginx x-accel-redirect作为外部资源的身份验证前端。在

在python代码中,我将执行以下操作:

/getresource/def view(self, req, resp):

name = get_name(req.user.id) # authenticates request.

resp.set_header('X-Accel-Redirect', '/resource/%s/' %name )

这也会将HTTP方法转发到nginx1.10。

由于nginx1.10,所有x-accel-redirect都作为GET方法转发。在

我知道转发HTTP方法的正确方法是使用命名位置。

我找不到关于该如何做的文件。

我尝试了以下方法:

^{pr2}$

但这会重定向到“@resource/”。在

我想重定向到“@resource/name”。在

但还没有回应。在

编辑:

发布nginx的配置location /getresource {

proxy_pass http://127.0.0.1:8000;

}

location /resource {

internal;

proxy_pass http://127.0.0.1:8888;

}

location @resource {

internal;

proxy_pass http://127.0.0.1:8888;

}

Internal Server Error The server encountered an internal error and was unable to complete your request. Please contact the server administrator if this error reappears multiple times, please include the technical details below in your report. More details can be found in the server log. error.log监控内容如下: 2025/06/09 15:42:11 [emerg] 8200#8200: unknown directive "..." in /etc/nginx/sites-enabled/nextcloud.conf:5 2025/06/09 15:43:43 [emerg] 8242#8242: unknown directive "..." in /etc/nginx/sites-enabled/nextcloud.conf:5 nginx -t 检查结果如下: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful nextcloud.conf配置文件内容如下: server { listen 192.168.60.200:80; # 改为你的域名或 IP # 静态资源缓存 location ~* \.(?:ico|css|js|svg|png|jpg)$ { expires 30d; add_header Cache-Control "public"; } # Nextcloud 根目录 root /var/www/nextcloud; index index.php index.html; # 基础安全规则 location ~ /\. { deny all; access_log off; log_not_found off; } # PHP 处理 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.2-fpm.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # Nextcloud 路由规则 location / { try_files $uri $uri/ /index.php$request_uri; } # 强制重定向到 /.well-known 路径 location = /.well-known/carddav { return 301 /remote.php/dav; } location = /.well-known/caldav { return 301 /remote.php/dav; } }
06-11
upstream largemodel_gateway{ server 10.73.171.46:30444; # server 188.108.11.91:30443; # server 188.108.11.89:30443; } upstream keyclock{ server 10.73.171.46:31080; # server 188.108.11.81:31080; # server 188.108.11.89:31080; } server { listen 30110; # 定义允许跳转的白名单域名(根据实际业务需求修改) set $valid_redirects "^(https?://(10\.73\.171\.46)(:\d+)?)"; location /docs/ { alias /usr/share/nginx/assets/; } location /auth { # if ($uri ~* "/auth/admin/?$") { # return 404; # } # if ($uri ~* "/auth/realms/master") { # return 404; # } # if ($uri ~* "/auth/admin/master") { # return 404; # } # 重定向参数检查 - 使用 map 变量 if ($arg_redirect_uri) { # 检查是否在白名单中 if ($is_valid_redirect = 0) { return 403 "Invalid redirect_uri detected. Only trusted domains are allowed."; } # 额外安全:阻止IP地址形式的外部跳转 if ($arg_redirect_uri ~* "^https?://\d+\.\d+\.\d+\.\d+") { return 403 "IP-based redirects are not allowed for security reasons."; } } proxy_pass http://keyclock; # 下面这几条是常用的,在反向代理时传递真实访问ip和信息的配置。 proxy_redirect off; proxy_intercept_errors off; 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_set_header X-Forwarded-Host 10.73.171.46; proxy_set_header X-Forwarded-Port 30110; # 传大文件失败 proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_max_temp_file_size 128m; proxy_hide_header Access-Control-Allow-Origin; add_header 'Access-Control-Allow-Origin' http://10.73.171.46:30110; if ($request_method = OPTIONS){ add_header 'Access-Control-Allow-Origin' http://10.73.171.46:30110 always; add_header 'Access-Control-Allow-Credentials' 'true' always; #是否可以带cookie add_header 'Access-Control-Max-Age' 1728000; #预检可用有效期 add_header 'Access-Control-Allow-Headers' 'Authorization, x-xsrftoken, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Range, poolId, projectId' always; #后续可用的header add_header 'Access-Control-Allow-Methods' 'POST, GET, PUT, DELETE'; #后续可以使用的http方法 return 204; } } # location = /auth/ { # return 404; # } location ~ /edu/auth { return 404; } location /largemodel { proxy_pass http://largemodel_gateway; # 不缓存,支持流式输出 add_header X-Accel-Buffering no; proxy_cache off; proxy_buffering off; #关闭代理缓冲 chunked_transfer_encoding on; #开启分块传输编码 tcp_nopush on; #开启TCP NOPUSH选项,禁止Nagle算法 tcp_nodelay on; #开启TCP NODELAY选项,禁止延迟ACK算法 keepalive_timeout 300; #设定keep-alive超时时间为65秒 proxy_intercept_errors off; proxy_connect_timeout 1800; proxy_send_timeout 1800; proxy_read_timeout 1800; 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-Port 30110; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_http_version 1.1; proxy_set_header X-Scheme $scheme; proxy_redirect off; # 传大文件失败 proxy_buffer_size 256k; proxy_buffers 4 256k; proxy_busy_buffers_size 256k; proxy_temp_file_write_size 256k; proxy_max_temp_file_size 128m; } } 报错:root@k8s-master01:/home/admin1/Downloads/backup_file# /usr/local/nginx/sbin/nginx -s reload nginx: [emerg] "if" directive is not allowed here in /usr/local/nginx/conf/conf.d/largemodel.conf:38这里38行是if ($is_valid_redirect = 0) {如何修改?
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值