A
A
Alexey2015-06-23 04:12:50
Nginx
Alexey, 2015-06-23 04:12:50

Nginx How to make a mobile version of the site?

Tell me, please, what is wrong with the config?
I'm trying to make a mobile version of the site with the "m" subdomain.
All the difference is in directories with statics: main site - application, mobile - mobile
Newbie in the back, so don't judge strictly and if there is something else to fix - let me know)

server {
        listen          80;
        server_name     site.ru www.site.ru;

        if ($http_user_agent ~* (iphone|аndroid|blackberry)){
                rewrite ^/(.*)$ m.site.ru/$1 permanent;
        }

        location / {
                auth_basic              "Enter pass:";
                auth_basic_user_file    /var/www/nginx/passwords;

                root    /var/www/site.ru/html/application;
                index   index.html;
                charset utf-8;

                proxy_pass              http://localhost:1337/;

                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header        Host                    $host;
        }
}


server {
        listen          80;
        server_name     m.site.ru www.m.site.ru;

        location / {
                auth_basic              "Enter pass:";
                auth_basic_user_file    /var/www/nginx/passwords;

                root    /var/www/site.ru/html/mobile;
                index   index.html;
                charset utf-8;

                proxy_pass              http://localhost:1337/;

                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header        Host                    $host;
        }
}

UPD:
Part of it worked - on site.ru and m.site.ru it gives different statics, but does not automatically switch to the mobile version of the site + the mobile version of the site does not work with the server.
server {
        listen          80;
        server_name     site.ru www.site.ru;

        if ($http_user_agent ~* (iphone|аndroid|blackberry)){
               rewrite ^/(.*)$ http://m.site.ru/$1 permanent;
        }

        location / {
                auth_basic              "Enter pass:";
                auth_basic_user_file    /var/www/nginx/passwords;

                root    /var/www/site.ru/html/application;
                index   index.html;
                charset utf-8;

                proxy_pass              http://localhost:1337/;

                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header        Host                    $host;
        }
}

server {
        listen 80;
        server_name m.site.ru;
        root    /var/www/site.ru/html/mobile;
        index   index.html;
}

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey, 2015-06-25
@Murmurianez

This is the solution I ended up with. Needs a little tweaking, but the redirect performs

server {
        listen          80;
        server_name     site.ru;

        if ($http_user_agent ~* "(iphone|android|blackberry)") {
                rewrite ^ http://m.site.ru redirect;
                break;
        }

        location / {
                root /var/www/site.ru/html/application; #статика
                index index.html;
                charset utf-8;

                proxy_pass http://localhost:1337; #здесь у нас висит Node.js - отправляем все запросы сюда
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header        Host                    $host;
        }
}

server {
        listen          80;
        server_name m.site.ru;

        location / {
                root    /var/www/site.ru/html/mobile; #статика мобильного сайта
                index   index.html;
                charset utf-8;
        }

        location /api { #перенаправляем все ajax запросы m.site.ru к api
                proxy_pass              http://localhost:1337;
                proxy_set_header        X-Real-IP               $remote_addr;
                proxy_set_header        X-Forwarded-For         $proxy_add_x_forwarded_for;
                proxy_set_header        Host                    $host;
        }
}

C
criminalist, 2015-06-24
@criminalist

Yeah, I listened to an adviser like you, now I'm making the third template because the adaptive layout turned out to be very difficult and the search engines refused to accept it, but this is a separate conversation.
I realized that we need separate versions for mobile and for PC.

E
Egor Kazantsev, 2015-06-23
@saintbyte

Drop these subdomains and other separate domains - use adaptive layout.

O
Oleg Batalov, 2015-06-25
@badmilkman

As an option: give one html and different css depending on the "mobility" of the client.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question