Welcome to Centmin Mod Community
Register Now

Nginx Targeting a specific file name in Nginx configuration

Discussion in 'Nginx, PHP-FPM & MariaDB MySQL' started by deltahf, Feb 18, 2018.

  1. deltahf

    deltahf Premium Member Premium Member

    242
    113
    43
    Jun 8, 2014
    Ratings:
    +179
    Local Time:
    5:40 PM
    To make a long story short, I have a stupid ad server which keeps prompting visitors to make requests to a file called "mraid.js" on my site (you can google that file name if you're interested in learning more). It doesn't matter what the URL is, it stupidly requests that file in the current path, even though it doesn't exist.

    This means my Nginx error log is flooded with 404s to these types of URLs:

    /mraid.js
    /forum/mraid.js
    /forum/thread-title.21/mraid.js

    The ad server (Google DFP) developers seem unwilling to fix this, so I have to modify my Nginx configuration to ignore requests to this file, wherever they may be.

    I added the following to my site's configuration file, but it's not working, and the failed requests are still being logged.

    Code (Text):
    location ~ mraid\.js
            {
                    log_not_found off;
            }
    


    What did I do wrong here?
     
  2. eva2000

    eva2000 Administrator Staff Member

    34,192
    7,566
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +11,629
    Local Time:
    7:40 AM
    Nginx 1.13.x
    MariaDB 5.5
    that's because Centmin Mod Nginx's vhost has a staticfiles.conf include file with location context for matching all *.js that took priority at /usr/local/nginx/conf/staticfiles.conf see Centmin Mod Configuration Files - CentminMod.com LEMP Nginx web stack for CentOS

    easiest thing to do would be add log_not_found to existing *.js location context in /usr/local/nginx/conf/staticfiles.conf
    Code (Text):
        location ~* \.(js)$ {
      #add_header Pragma public;
      #add_header X-Frame-Options SAMEORIGIN;
      #add_header X-Xss-Protection "1; mode=block" always;
      #add_header X-Content-Type-Options "nosniff" always;
      add_header Access-Control-Allow-Origin *;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate, immutable, stale-while-revalidate=86400, stale-if-error=604800";
            access_log off;
            log_not_found off;
            expires 30d;
            break;
            }
    


    how is it referenced, make sure it's an absolute and not relative reference to the file i.e. href reference is to /path/to/mdraid.js and not just mdraid.js without beginning forward slash if it's located in top level web root.
     
    • Informative Informative x 1
..