大网数据广告活动
服务器运维问答

NGINX proxy_pass with URI modification

· 52CE 我爱测

I need to pass some requests to proxy (running GlassFish) with removing one section of url. For example: https://xxx.net/jazz/MobileApi?id=2&make_id=4 Should be passed to proxy as: http://X.X.X.X:8080/MobileApi?id=2&make_id=4 I h…

问题原文

原帖链接 · Andrey · CC BY-SA 3.0

I need to pass some requests to proxy (running GlassFish) with removing one section of url. For example:

https://xxx.net/jazz/MobileApi?id=2&make_id=4

Should be passed to proxy as:

http://X.X.X.X:8080/MobileApi?id=2&make_id=4

I have following Nginx configuration:

upstream vito_api {
    server 178.63.X.X:8080;
}

server {
    listen 80;
    listen 443 ssl;
    ....

    location ~ /jazz/(?<section>.*) {
       proxy_pass http://vito_api/$section/;
       proxy_set_header X-Real-IP $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

But, unfortunately, request passing without parameters. So, in GlassFish access logs, I can see only:

"148.251.X.X" "NULL-AUTH-USER" "05/Jan/2015:15:18:40 +0100" "GET /MobileApi/ HTTP/1.0" 200 21

What I did wrong? How to pass URL parameters too?

Thank you.

已采纳解答

原帖链接 · Xavier Lucas · CC BY-SA 3.0

From nginx's documentation (context : prefixed location)

If the proxy_pass directive is specified with a URI, then when a request is passed to the server, the part of a normalized request URI matching the location is replaced by a URI specified in the directive.

So it can be simplified with the following :

location /jazz/ {
    proxy_pass http://vito_api/;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2015-01-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。