Nginx Angular2:proxy_pass /透明内部重定向移动用户

时间:2017-09-11 10:57:19

标签: angular nginx

我有2个网站,对于桌面我们有基于php和移动设备我们有基于angular2的。我想为两者保留相同的域,并希望在内部将移动用户重定向到Angular2 vhost。

因此,桌面用户通常会登陆www.example.com,移动用户会在内部重定向到m.example.com而无需重定向浏览器。

我已经尝试过proxy_pass,我可以看到主页,但其他资源(css.js)等正在抛出404.

修改

以下是我们的主要vhost配置:

map $http_host $MAGE_RUN_CODE
{

    example.com usd_en;
    en-ae.example.com aed_en;
    ar-ae.example.com aed_ar;
    en-sa.example.com sar_en;
    ar-sa.example.com sar_ar;
    ar.example.com usd_ar;
    #en-qa.example.com qar_en;
    #ar-qa.example.com usd_en;
    en-qa.example.com usd_en;
    ar-qa.example.com usd_en;
    en-kw.example.com kwd_en;
    ar-kw.example.com kwd_ar;
    en-bh.example.com bhd_en;
    ar-bh.example.com bhd_ar;
    en-om.example.com omr_en;
    ar-om.example.com omr_ar;
}

server
{
    server_name www.example.com;
    rewrite ^(.*) http://example.com$1 permanent;
}


server
{
    server_name en-qa.example.com ar-qa.example.com;
    rewrite ^(.*) http://example.com$1;
}


server
{
    listen 80 default_server;

    #server_name example.com media.example.com js.example.com css.example.com en-ae.example.com ar-ae.example.com en-sa.example.com ar-sa.example.com test.example.com ar.example.com en-qa.example.com ar-qa.example.com en-kw.example.com ar-kw.example.com en-bh.example.com ar-bh.example.com en-om.example.com ar-om.example.com;

    server_name example.com 54.154.251.109 52.18.89.91 media.example.com js.example.com css.example.com en-ae.example.com ar-ae.example.com en-sa.example.com ar-sa.example.com test.example.com ar.example.com en-kw.example.com ar-kw.example.com en-bh.example.com ar-bh.example.com en-om.example.com ar-om.example.com;
    root /var/www/vhosts/www.example.com/public_html;


    location /apple-app-site-association
    {
        default_type application/json;
    }


    access_log /var/log/nginx/example.com-access.log main;
    error_log /var/log/nginx/example.com-error.log;


    location ~ onepage
    {
        try_files $uri $uri/ @handler;
    }

    location ~ [A-Z]
    {
        rewrite ^(.*)$ $scheme://$host$uri_lowercase permanent;
    }

    if ($request_uri ~* "index.php")
    {
        rewrite ^/(.*) $scheme://$host permanent;
    }

    if ($request_uri ~* "\/\/")
    {
        rewrite ^/(.*) $scheme://$host/$1 permanent;
    }

    location /
    {


        #Mobile Redirect
        #include /etc/nginx/vhosts/includes/mobile_redirects.conf;

        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
    }


    location @handler
    {
        ## Magento uses a common front handler
        rewrite / /index.php;
    }

    location ~ .php/
    {
        ## Forward paths like /js/index.php/x.js to relevant handler
        rewrite ^(.*.php)/ $1 last;
    }

    location ~ \.php$
    {
        ## Execute PHP scripts
        if (!-e $request_filename)
        {
            rewrite / /index.php last;
        }
        ## Catch 404s that try_files miss
        expires off; ## Do not cache dynamic content
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fastcgi_param MAGE_IS_DEVELOPER_MODE off;
        #fastcgi_param display_errors off;
        fastcgi_param MAGE_RUN_CODE $MAGE_RUN_CODE;
        fastcgi_param MAGE_RUN_TYPE store;
        include fastcgi_params;
        include proxy_params;
        fastcgi_param HTTPS $ssl;
        fastcgi_param PHP_VALUE "newrelic.appname=example.com";
    }
}

我们在寻找用户代理是否包含移动设备然后透明地将其路由到“m2-app.example.com”

赞:proxy_pass http://m2-app.example.com;

1 个答案:

答案 0 :(得分:0)

将答案用作评论,因为我需要格式化

http {

 map $http_user_agent $site_type {
    default "@handler";
    ~(iPhone|Android) "@mobile";
    ~(mobile) "@mobile";
  }
....

    location @mobile {
        proxy_pass http://m2-app.example.com;
    }

    location /
    {
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri $uri/ $site_type; ## If missing pass the URI to Magento's front handler
    }

所以上面的配置应该可以帮到你。您根据用户代理确定try_files的处理程序的位置。

编辑-1:新方法

您可以尝试以下配置

http {

 map $http_user_agent $site_type {
    default "desktop";
    ~(iPhone|Android) "mobile";
    ~(mobile) "mobile";
  }
....

    location /
    {
        root /var/www/html/$site_type;
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri /index.html; ## If missing pass the URI to Magento's front handler
    }

或者你可以尝试

    location /
    {
        root /var/www/html/;
        index index.html index.php; ## Allow a static html file to be shown first
        try_files $uri /$site_type/index.html; ## If missing pass the URI to Magento's front handler
    }