Converting subdomain to path component without redirect?

Hello guys,

Am having trouble setting up my nginx.config to transparently proxy the
subdomains and domains to the same app, but with different “path
components” appended to the $uri

example:
mydomain.it/PATH should return ~> mydomain.com/it/PATH

using regexp:
(www.)?mydomain.(it|jp|es|de) to return my
http://app_server/$2/$request_uri

My idea is to proxy the “localised server_name” to the “default
server_name” without letting the user know ( no browser redirect ).

This is my “working” nginx.config ( without the rukes ):

This is my last unsuccessful attempt: trying multiple server blocks on nginx - Pastebin.com

any input is highly appreciated

thanks a lot,
peace

*i meant: This is my “working” nginx.config ( without the rewrite rules
):

On Mon, Apr 29, 2013 at 10:02:35PM +0100, henrique matias wrote:

Hi there,

Am having trouble setting up my nginx.config to transparently proxy the
subdomains and domains to the same app, but with different “path
components” appended to the $uri

Frequently, the main problem is that the back-end application makes it
very hard to do this.

I suggest you test first using a separate server{} block for one
server_name and demonstrate to yourself that it can work.

After that, you can worry about the details of how to auto-handle the
extra domains.

Something like (untested):

server {
server_name www.mydomain.it;
location / {
proxy_pass http://app_server/it/;
}
}

maybe with “proxy_set_header Host www.mydomain.com;”, or whatever your
application needs.

The important things to check are, do links in the returned content work
when the browser asks for “/dir/” but the app_server gets a request for
“/it/dir/”?

The above is almost the same as what you have here:

This is my last unsuccessful attempt: trying multiple server blocks on nginx - Pastebin.com

but there’s an extra “/” in the proxy_pass line; and as you’ve not said
in what way yours was unsuccessful, it’s hard to suggest a specific fix.

Compare the output of “curl -i http://www.mydomain.com/it/SOMETHING
with the output of “curl -i mydomain.it”, and with
what you expect the output to be.

f

Francis D. [email protected]

My first try was to change my location / { } to proxy pass to another
language, so i could try “the backend” as you said, but it actually
didn’t
work, i got:

[emerg] “proxy_pass” cannot have URI part in location given by regular
expression, or inside named location, or inside “if” statement, or
inside
“limit_except” block in /etc/nginx/nginx.conf:79

my nginx version is 1.3.15.

will keep trying, if someone knows how to work this around, would be
cool,
i guess this happens quite often :stuck_out_tongue:

[s]

Also i tried adding the address to the try_files:

try_files $uri $uri @app/de/; and try_files $uri $uri @app/de;

but that didn’t work either.

The way i managed to provide translated content, was using a rewrite
inside
of my location block: rewrite ^(.*)$ /my-language/$1 break;

That solves part of my problem.

The core basic of my problem is “Rewrite the URL based on the “server
name””.

So far the only options i see on nginx are:

  1. Have a configuration with one “if” and one “rewrite” in order to map
    server name to path

  2. Multiple server declarations sharing the same configuration (
    probably
    using some sort of include? )

What you reckon? Any suggestion ?

On Sun, May 05, 2013 at 02:21:58AM +0100, henrique matias wrote:

My first try was to change my location / { } to proxy pass to another
language, so i could try “the backend” as you said, but it actually didn’t
work, i got:

[emerg] “proxy_pass” cannot have URI part in location given by regular
expression, or inside named location, or inside “if” statement, or inside
“limit_except” block in /etc/nginx/nginx.conf:79

Unless I’m missing something, that configuration should not lead to that
error message.

The new test server{} block should be very small, with every line
understood.

If you show exactly what you did, what you got, and what you expected
to get, others will have a better chance of offering help.

f

Francis D. [email protected]

Hello Francis, thanks a lot for your help and words.

Sorry if at some point i didn’t make something clear.

Starting from scratch,

  1. My backend already works, http://my_ip/#{language_code}/anything will
    bring “anything” translated to the specified language.

  2. Changing the line 40 on my config: trying multiple server blocks on nginx - Pastebin.com from
    proxy_pass http://app_server;
    to
    proxy_pass http://app_server/de/;

brings me the error: Starting nginx: nginx: [emerg] “proxy_pass” cannot
have URI part in location given by regular expression, or inside named
location, or inside “if” statement, or inside “limit_except” block in
/etc/nginx/nginx.conf:121
nginx: configuration file /etc/nginx/nginx.conf test failed

Regarding: Module ngx_http_map_module :
Sounds like the way to go in order to map domian/subsomains to values (
:

Regarding: Module ngx_http_proxy_module

I tried using the “unix:socket” format, in my case:

proxy_pass http://unix:/tmp/.sock:/it/

as the example in the page you sent, and got the same error.

Thanks a lot for your tips, it definitely helped me to understand it
better.

So far the best solution i can see is using map in order to map the
addresses to “language codes”, and then execute the rewrite.

peace

On Mon, 2013-05-06 at 00:14 +0100, henrique matias wrote:
[snip]

brings me the error: Starting nginx: nginx: [emerg] “proxy_pass”
cannot have URI part in location given by regular expression, or
inside named location, or inside “if” statement, or inside
“limit_except” block in /etc/nginx/nginx.conf:121
nginx: configuration file /etc/nginx/nginx.conf test failed

…so take it out of the named location??

Steve


Steve H. BSc(Hons) MIITP

Skype: sholdowa

Hello Steve, yeah i took out ( :

Everything is working fine!

now am starting to dig the cache module, so my backend doesnt get called
too many times in a short period of time…

thanks a lot guys for all your help ( :

On Sun, May 05, 2013 at 02:38:17AM +0100, henrique matias wrote:

Hi there,

Also i tried adding the address to the try_files:

try_files $uri $uri @app/de/; and try_files $uri $uri @app/de;

but that didn’t work either.

Unless you also added new named locations like “@app/de/”, I’d expect
that to return HTTP 500.

That’s a more specific problem report than “didn’t work”.

The core basic of my problem is “Rewrite the URL based on the “server
name””.

So far the only options i see on nginx are:

  1. Have a configuration with one “if” and one “rewrite” in order to map
    server name to path

http://nginx.org/r/map

Set a (possibly empty) variable called (say) $path_prefix, and then use
that in your rewrite or proxy_pass line.

Note that using a variable in proxy_pass has other requirements too:

http://nginx.org/r/proxy_pass

  1. Multiple server declarations sharing the same configuration ( probably
    using some sort of include? )

Can work, if the shared configuration is truly the same.

What you reckon? Any suggestion ?

I still think that until you demonstrate that your back-end works with
this, the rest is not useful.

Test one thing at a time, and keep the configuration simple.

f

Francis D. [email protected]