Nginx config for tracking files and forms (getting 404s)

Software
My Mautic version is: 2.16.2
My PHP version is: 7.3.17-1+ubuntu18.04.1+deb.sury.org+1

Problem
My problem is:
I have 2 problems that I think are related somehow. Tracking pixels and JS code load fine in the browser, but their http code is 404, same for the forms, but for these, they don’t even work when I try to embed them in a website.

curl -I https://mymauticinstall.com/mtc.js
HTTP/2 404
server: nginx
content-type: application/javascript; charset=utf-8
vary: Accept-Encoding
set-cookie: 7bb85555927bdb8d66d3d0ee6d04ad51=ap53f7bm8auttj8o1hqqlso3aa; path=/; secure; HttpOnly
cache-control: no-cache
date: Mon, 04 May 2020 22:41:30 GMT

This file when loaded in a browser shows the correct content, but it also shows a 404 error, same for the JS that loads the forms. But in the case of the forms as I said, it prevents them from working completely, so nothing gets rendered.

These errors are showing in the log:
2020/05/04 17:39:49 [debug] 28961#28961: *1 http filename: “/home/forge/mymauticinstall.com/public/mtc.js”
2020/05/04 17:39:49 [debug] 28961#28961: *1 add cleanup: 00005627D28D57B8
2020/05/04 17:39:49 [error] 28961#28961: *1 open() “/home/forge/mymauticinstall.com/public/mtc.js” failed (2: No such file or directory), client: 179.6.209.113, server: mymauticinstall.com, request: “HEAD /mtc.js HTTP/2.0”, host: “mymauticinstall.com
2020/05/04 17:39:49 [debug] 28961#28961: *1 http finalize request: 404, “/mtc.js?” a:1, c:1
2020/05/04 17:39:49 [debug] 28961#28961: *1 http special response: 404, “/mtc.js?”
2020/05/04 17:39:49 [debug] 28961#28961: *1 internal redirect: “/index.php?”

Steps I have tried to fix the problem:
I’ve searched the internet multiple times, tried several different configurations and tried to debug this on my own. Searched the forums and github for answers. Also tried stackoverflow and other similar sites. Nothing.

Current config:
add_header ‘Access-Control-Allow-Origin’ “*”;

# redirect index.php to root
rewrite ^/index.php/(.*) /$1  permanent;

# Allow the filemanager to work
location ~ ^/app/bundles/CoreBundle/Assets/js/libraries/ckeditor/filemanager/connectors/php/filemanager.php$ {
    allow all;
    fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
    include fastcgi_params;
}

# Deny everything else in /app folder except Assets folder in bundles
location ~ /app/bundles/.*/Assets/ {
    allow all;
    access_log off;
}
location ~ /app/ { deny all; }

# Deny everything else in /addons or /plugins folder except Assets folder in bundles
location ~ ^/(addons|plugins)/.*/Assets/ {
    allow all;
    access_log off;
}
location ~ ^/(addons|plugins)/ { deny all; }

# Deny all php files in themes folder
location ~* ^/themes/(.*)\.php {
    deny all;
}

# Deny yml, twig, markdown, init file access
location ~* /(.*)\.(?:markdown|md|twig|yaml|yml|ht|htaccess|ini)$ {
    deny all;
    access_log off;
    log_not_found off;
}

# Deny all attempts to access hidden files/folders such as .htaccess, .htpasswd, .DS_Store (Mac), etc...
# Except .well-known to allow for certbot to issue and renew certificates
location ~ /\.(?!well-known).* {
    deny all;
    access_log off;
    log_not_found off;
}

# Deny all grunt, composer files
location ~* (Gruntfile|package|composer)\.(js|json)$ {
    deny all;
    access_log off;
    log_not_found off;
}

# Deny access to any files with a .php extension in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}

location ~ ^/mtc.js {
    default_type "application/javascript";
    try_files $uri $uri/ /index.php$is_args$args;
}

location ~* ^/media/js/$ {
    add_header 'Cache-Control' public;
    expires 7d;
}

location ~ \.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt)$ {
    allow all;
    try_files $uri $uri/ /index.php$is_args$args;
}

# Set cache expiry time
location ~* \.(jpg|jpeg|png|ico|css)$ {
    expires 365d;
}

location ~* \.(pdf)$ {
    expires 30d;
}

# Add X-Robots-Tag header to login page to prevent bots from indexing or following
add_header X-Robots-Tag "noindex, nofollow";

Any help is appreciated, thanks.

1 Like

Did you choose Restrict Domains “NO” from CORS Settings?

Yes of course. One of the first things I did.

So, I was able to fix the 404 on mtc.js mtracking.gif and forms.js those work fine now. This is the new config:

# Mautic tracking fallback
location = /mtracking.gif {
     expires off;
     gzip off;
     default_type "image/gif";
     add_header 'Access-Control-Allow-Origin' *;
     try_files $uri /index.php?$args;
}

# Mautic tracking code
location = /mtc.js {
    expires off;
    default_type "application/javascript";
    try_files $uri $uri/ /index.php$is_args$args;
}

# Embedded forms
location = /form/generate.js {
    expires off;
    default_type "application/javascript";
    try_files $uri $uri/ /index.php$is_args$args;
}

Now I just need to get the email tracking pixel working. As before I’m getting a 404 even though the file loads just fine and even tracks emails correctly.

This is what I get with curl:

curl -I https://mymauticinstall.com/email/5eb1d62a797b6697395943.gif
HTTP/2 404
server: nginx
content-type: text/html; charset=UTF-8
set-cookie: 7bb85555927bdb8d66d3d0ee6d04ad51=l1ujdnsd9r6eo1ol376aq1f4su; path=/; secure; HttpOnly
set-cookie: mautic_session_id=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/; secure; SameSite=None
set-cookie: mautic_device_id=m34stxwawfljlrkbdj4gukj; expires=Thu, 06-May-2021 21:49:01 GMT; Max-Age=31536000; path=/; secure; SameSite=None
set-cookie: mtc_id=23412; path=/; secure; SameSite=None
set-cookie: mtc_sid=m34stxwawfljlrkbdj4gukj; path=/; secure; SameSite=None
set-cookie: mautic_session_id=m34stxwawfljlrkbdj4gukj; expires=Thu, 06-May-2021 21:49:01 GMT; Max-Age=31536000; path=/; secure; SameSite=None
set-cookie: m34stxwawfljlrkbdj4gukj=23412; expires=Thu, 06-May-2021 21:49:01 GMT; Max-Age=31536000; path=/; secure; SameSite=None
cache-control: no-cache
date: Wed, 06 May 2020 21:49:01 GMT
content-encoding: none

Anyone knows why’s this happening?

1 Like

@jtimana Congratulation for your Mautic working.

I insert the form to collect e-mail using official Mautic Wordpress plugin from Wordpress site, i got the 404 error for mautic-form.js.

I already post it to here, but nobody reply for that. Do you have any idea about that?

No, I’m sorry but I haven’t figured out the email tracking pixel yet as I mention above. That’s the only thing left to have Mautic run flawlessly under Nginx.

Also, please I’d appreciate a lot if you don’t hijack my thread with your own questions, thank you :slight_smile:

1 Like

Why not replace all that with just this code which works perfectly for me!

location / {
  try_files $uri /index.php$is_args$args;
 }

Yes, I already have that.

Hi everyone,
Is there similar solution for apache2?
I am getting 50 error for mtc.js.

All this does for me is results in 301 Moved Permanently. Per the config, generate.js is routed through MauticFormBundle:Public:generate. Not sure exactly why switching to HTTPS would make any difference once we hit the controller, unless the app itself is fiddling with headers.

Any update on this at all?