Learn about Centmin Mod LEMP Stack today
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

    Jun 8, 2014
    Local Time:
    10:51 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:


    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

    May 24, 2014
    Brisbane, Australia
    Local Time:
    1:51 PM
    Nginx 1.21.x
    MariaDB 10.x
    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;

    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.