Score:0

nginx rewrite for mobile site, except some extensions

gb flag

We got an IIS server with two sites: www.example.com and m.example.com (for mobile). The www site has this code in the web.config to redirect to mobile site:

<rule name="ignore png" stopProcessing="true">
      <match url="(.*)\.png" />
      <action type="None" />
</rule>
<rule name="ignore jpg" stopProcessing="true">
      <match url="(.*)\.jpg" />
      <action type="None" />
</rule>
<rule name="ignore jpeg" stopProcessing="true">
      <match url="(.*)\.jpeg" />
      <action type="None" />
</rule>
<rule name="ignore gif" stopProcessing="true">
      <match url="(.*)\.gif" />
      <action type="None" />
</rule>
<rule name="ignore pdf" stopProcessing="true">
      <match url="(.*)\.pdf" />
      <action type="None" />
</rule>
<rule name="ignore amp" stopProcessing="true">
      <match url="(.*)/amp/(.*)" />
      <action type="None" />
</rule>
<rule name="Mobile Redirect" stopProcessing="true">
     <match url="(.*)" ignoreCase="true" />
     <conditions logicalGrouping="MatchAny" trackAllCaptures="false">   
       <add input="{HTTP_USER_AGENT}" pattern="midp|mobil|phone" />
       <add input="{HTTP_X-Device-User-Agent}" pattern="midp|mobil|phone" />
       <add input="{HTTP_X-OperaMini-Phone-UA}" pattern="midp|mobil|phone" />      
     </conditions>
     <action type="Redirect" url="https://m.example.com/{R:0}" appendQueryString="True" />
</rule>

This basically do the redirect but not when is one of the extensions png, jpg, gif etc. the www site have all the images, so the mobile site have to link to www.example.com/images/image.jpg not m.example.com/images/image.jpg

I'm new to nginx and I have configured a nginx load balancer in the front, so I move all the redirection rules to the frontend nginx.

I add this conf. to nginx to do the mobile redirection:

set $mobile_rewrite do_not_perform;
  if ($http_user_agent ~* "|android|midp|mobil|phone") {
  set $mobile_rewrite perform;
  }
  if ($mobile_rewrite = perform) {
  rewrite ^ http://m.andorradifusio.ad$request_uri? redirect;
  break;
  }

The redirection works but all the images path are broken because they are pointing to m.example.com/images/image.jpg an inexistent path, the images are in the www site.

Which is the right config in nginx to avoid the redirection for those file extensions?

Thanks in advance!

Edit, this is nginx configuration:

worker_processes auto;
error_log /var/log/nginx/error.log warn;

events {
    worker_connections  1024;
}

http {
        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        client_body_buffer_size 2000M;
        client_max_body_size 0M;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        gzip on;
        include /etc/nginx/conf.d/*.conf;
        proxy_buffering off;
        log_format  main_ext  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '"$host" sn="$server_name" '
                      'rt=$request_time '
                      'ua="$upstream_addr" us="$upstream_status" '
                      'ut="$upstream_response_time" ul="$upstream_response_length" '
                      'cs=$upstream_cache_status' ;
        access_log /var/log/nginx/access.log main_ext;
     }

types {
    text/html                                        html htm shtml;
    text/css                                         css;
    text/xml                                         xml;
    image/gif                                        gif;
    image/jpeg                                       jpeg jpg;
    application/javascript                           js;
    application/atom+xml                             atom;
    application/rss+xml                              rss;

    text/mathml                                      mml;
    text/plain                                       txt;
    text/vnd.sun.j2me.app-descriptor                 jad;
    text/vnd.wap.wml                                 wml;
    text/x-component                                 htc;

    image/png                                        png;
    image/svg+xml                                    svg svgz;
    image/tiff                                       tif tiff;
    image/vnd.wap.wbmp                               wbmp;
    image/webp                                       webp;
    image/x-icon                                     ico;
    image/x-jng                                      jng;
    image/x-ms-bmp                                   bmp;

    application/font-woff                            woff;
    application/java-archive                         jar war ear;
    application/json                                 json;
    application/mac-binhex40                         hqx;
    application/msword                               doc;
    application/pdf                                  pdf;
    application/postscript                           ps eps ai;
    application/rtf                                  rtf;
    application/vnd.apple.mpegurl                    m3u8;
    application/vnd.google-earth.kml+xml             kml;
    application/vnd.google-earth.kmz                 kmz;
    application/vnd.ms-excel                         xls;
    application/vnd.ms-fontobject                    eot;
    application/vnd.ms-powerpoint                    ppt;
    application/vnd.oasis.opendocument.graphics      odg;
    application/vnd.oasis.opendocument.presentation  odp;
    application/vnd.oasis.opendocument.spreadsheet   ods;
    application/vnd.oasis.opendocument.text          odt;
    application/vnd.openxmlformats-officedocument.presentationml.presentation
                                                     pptx;
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
                                                     xlsx;
    application/vnd.openxmlformats-officedocument.wordprocessingml.document
                                                     docx;
    application/vnd.wap.wmlc                         wmlc;
    application/x-7z-compressed                      7z;
    application/x-cocoa                              cco;
    application/x-java-archive-diff                  jardiff;
    application/x-java-jnlp-file                     jnlp;
    application/x-makeself                           run;
    application/x-perl                               pl pm;
    application/x-pilot                              prc pdb;
    application/x-rar-compressed                     rar;
    application/x-redhat-package-manager             rpm;
    application/x-sea                                sea;
    application/x-shockwave-flash                    swf;
    application/x-stuffit                            sit;
    application/x-tcl                                tcl tk;
    application/x-x509-ca-cert                       der pem crt;
    application/x-xpinstall                          xpi;
    application/xhtml+xml                            xhtml;
    application/xspf+xml                             xspf;
    application/zip                                  zip;

    application/octet-stream                         bin exe dll;
    application/octet-stream                         deb;
    application/octet-stream                         dmg;
    application/octet-stream                         iso img;
    application/octet-stream                         msi msp msm;

    audio/midi                                       mid midi kar;
    audio/mpeg                                       mp3;
    audio/ogg                                        ogg;
    audio/x-m4a                                      m4a;
    audio/x-realaudio                                ra;

    video/3gpp                                       3gpp 3gp;
    video/mp2t                                       ts;
    video/mp4                                        mp4;
    video/mpeg                                       mpeg mpg;
    video/quicktime                                  mov;
    video/webm                                       webm;
    video/x-flv                                      flv;
    video/x-m4v                                      m4v;
    video/x-mng                                      mng;
    video/x-ms-asf                                   asx asf;
    video/x-ms-wmv                                   wmv;
    video/x-msvideo                                  avi;
}

# configuration file /etc/nginx/conf.d/example.com.conf:
server {
    listen 80;
    server_name www.exam.com
                exam.com
                *.example1.com
                example.com;
    return 301 https://www.example.com$request_uri;

     #set $mobile_rewrite do_not_perform;
     # if ($http_user_agent ~* "|android|midp|mobil|phone") {
     #set $mobile_rewrite perform;
     # }
     # if ($mobile_rewrite = perform) {
     #rewrite ^ http://m.andorradifusio.ad$request_uri? redirect;
     #break;
      #}
}
upstream lbexample {
      server 10.50.30.21;
      server 10.50.30.22;
}

server {
    listen 443 ssl http2;
    server_name example.com;

    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    add_header Strict-Transport-Security "max-age=31536000";

    location / {
    proxy_pass http://lbexample;
        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 $scheme;
        proxy_set_header X-Client-IP $remote_addr;
        proxy_set_header X-Scheme $scheme;
        proxy_connect_timeout      3600;
        proxy_send_timeout         3600;
        proxy_read_timeout         3600;
     }

}

server {
    listen 80;
    listen [::]:80;
    server_name m.example.com;
    return 301 https://m.example.com$request_uri;
}

upstream mexample {
      server 10.50.30.21:81;
      server 10.50.30.22:81;
}

server {
    listen 443 ssl http2;
      server_name m.example.com;
    absolute_redirect off;
    ssl_certificate /etc/nginx/ssl/m.example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/m.example.com.key;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_session_cache shared:SSL:20m;
    ssl_session_timeout 10m;
    ssl_prefer_server_ciphers on;
    ssl_ciphers ECDH+AESGCM:DH+AESGCM:ECDH+AES256:DH+AES256:ECDH+AES128:DH+AES:ECDH+3DES:DH+3DES:RSA+AESGCM:RSA+AES:RSA+3DES:!aNULL:!MD5:!DSS;
    add_header Strict-Transport-Security "max-age=31536000";

    location / {
        proxy_set_header Host $host:$server_port;
        proxy_set_header X-Forwarded-Port $server_port;
        proxy_set_header X-Forwarded-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 $scheme;
        proxy_connect_timeout         3600;
        proxy_send_timeout         3600;
        proxy_read_timeout         3600;
    proxy_pass http://mexample;
    }
}
us flag
Please add the full nginx configuration as shown by `nginx -T` command to the question.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.