A
A
Alexander Pashchenko2013-05-31 16:03:42
Nginx
Alexander Pashchenko, 2013-05-31 16:03:42

m.domain.com -> domain.ru/m on nginx (but with a twist)

Given:
nginx on the frontend as a balancer.
jetty on the backend.

Users should come to sub.domain.ru.
And sub.domain.ru should remain everywhere in the browser line.
However, requests like domain.ru/sub/ should already be coming to jetty

. So far, I have reached such a configuration.
For domain.ru, we send a request directly to the backend
. For m.domain.ru, we rewrite the Host header field in domain.ru, and we send the request to the backend by rewriting the URI as /m/{request}

And everything seems to be working. But the fact is that in the application all URLs to the statics are registered as /public/mobile. Accordingly, when entering m.domain.ru, they are converted to mobile.ru/m/public/mobile, and the server does not find them. Wrote a workaround, but something doesn't work. I don't understand what I'm doing wrong.

Maybe there is a more elegant solution to this problem?

    server {
        listen       80;
        server_name  m.domain.ru;
        
        root           /path/to/jetty/webapps/;

        # все запросы к m.domain.ru переписываем отправляем к jetty как localhost:8080/m/, а поле заголовка Host переписываем на domain.ru 
        location / {
             proxy_pass              http://127.0.0.1:8080/m$request_uri;
             proxy_set_header        Host domain.ru;
             proxy_set_header        X-Real-IP $remote_addr;
             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_cache off;
             proxy_redirect off;
        
        }

        #но делаем исключение для статики
        location /public/mobile {
             proxy_pass              http://localhost:8080/;
             proxy_set_header        X-Real-IP $remote_addr;
             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header        Host domain.ru;
             proxy_cache off;
             proxy_redirect off;

        }

    }

    server {
        listen       80;
        server_name  domain.ru;
        root           /path/to/jetty/webapps/;
        location / {
             proxy_pass                    http://localhost:8080/;
             proxy_set_header        X-Real-IP $remote_addr;
             proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header        Host $http_host;
             proxy_cache off;
             proxy_redirect off;
        }
    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
_
_ _, 2013-05-31
@AMar4enko

It is not entirely clear - at what address does this statics lie correctly? And is it right that you are proxying requests to statics? Can directly give enginiksom?

P
Power, 2013-06-02
@Power

It seems to be correct like this (this is for m.domain.ru):

location / {
  proxy_pass http://localhost:8080/m/;
  ...
}

location /public/mobile/ {
  proxy_pass http://localhost:8080;
  ...
}

_
_ _, 2013-05-31
@AMar4enko

You need to write in the second server something like

location /m/public/mobile {
             proxy_pass http://localhost:8080/public/mobile/$uri;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
             proxy_set_header Host $http_host;
             proxy_cache off;
             proxy_redirect off;
}

True, I don’t remember offhand about $uri and /$uri - in one of these options, it will throw it out of $uri location and as a result, the necessary request will go to your backend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question