Please help with proxy_pass and trailing slash!

3 views
Skip to first unread message

Roddick

unread,
Mar 14, 2012, 1:05:55 PM3/14/12
to ng...@nginx.org
I have a php site with location /app served by Java app server on same
server port 9001

What is in /app/ should become / (root) for Java app server

I have this in config:

server {
...
index index.html index.htm index.php;

location /app {

proxy_pass http://localhost:9001;
proxy_next_upstream error timeout invalid_header
http_500 http_502 http_503;

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 https;

proxy_redirect off;
}

location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.php?$args;
}

location ~ index.php$ {

try_files $uri =404;

include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}

with this configuration, Java server receives requests like "GET /app/"
while it should receive "GET /"

If I add trailing slash to proxy_pass:

proxy_pass http://localhost:9001/;

Then java server receives "GET //" and unable to serve that either.

Do I need to involve rewrite here ? And how? or extra trailing slash is
caused by other rewrite rules somehow?

Thank you!

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,223839,223839#msg-223839

_______________________________________________
nginx mailing list
ng...@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx

Valentin V. Bartenev

unread,
Mar 14, 2012, 2:06:01 PM3/14/12
to ng...@nginx.org
On Wednesday 14 March 2012 21:05:55 Roddick wrote:
> I have a php site with location /app served by Java app server on same
> server port 9001
>
> What is in /app/ should become / (root) for Java app server
>
> I have this in config:
>
> server {
> ...
> index index.html index.htm index.php;
>
> location /app {
>
> proxy_pass http://localhost:9001;
> proxy_next_upstream error timeout invalid_header
> http_500 http_502 http_503;
>
> 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 https;
>
> proxy_redirect off;
> }
>
> location / {
> # First attempt to serve request as file, then
> # as directory, then fall back to index.html
> try_files $uri $uri/ /index.php?$args;
> }

Just a side note, you don't need explicit set $args here.

try_files $uri $uri/ /index.php;


> location ~ index.php$ {
>
> try_files $uri =404;
>
> include fastcgi_params;
> fastcgi_index index.php;
> fastcgi_param SCRIPT_FILENAME
> $document_root$fastcgi_script_name;
> fastcgi_pass 127.0.0.1:9000;
> }
> }
>
> with this configuration, Java server receives requests like "GET /app/"
> while it should receive "GET /"
>
> If I add trailing slash to proxy_pass:
>
> proxy_pass http://localhost:9001/;
>
> Then java server receives "GET //" and unable to serve that either.
>
> Do I need to involve rewrite here ? And how? or extra trailing slash is
> caused by other rewrite rules somehow?
>

Documentation already has answers to your questions:
http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

E.g., in your case:

location /app/ {
...
proxy_pass http://localhost:9001/;
}

wbr, Valentin V. Bartenev

Roddick

unread,
Mar 15, 2012, 12:08:10 AM3/15/12
to ng...@nginx.org
Valentin V. Bartenev Wrote:
-------------------------------------------------------

Thanks Valentin.

I get it now! I had
location /app {

and it should be
location /app/ {

with trailing slash. Weird that it's sensitive for that slash there.

Thanks.

Posted at Nginx Forum: http://forum.nginx.org/read.php?2,223839,223859#msg-223859

Edho Arief

unread,
Mar 15, 2012, 12:18:45 AM3/15/12
to ng...@nginx.org

Trivia: "location /app { ... }" will match url "http://domain.com/appwhatever"

Reply all
Reply to author
Forward
0 new messages