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

**URL:** <https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186>\
**Category:** Product Support\
**Created:** [May 4, 2020, 11:23pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186 "2020-05-04T23:23:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtimana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jtimana/32/2352_2.png) [@jtimana](https://forum.mautic.org/u/jtimana)\
**Post date:** [May 4, 2020, 11:23pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/1 "2020-05-04T23:23:58Z")

</div>

**Software**  
My Mautic version is: [2.16.2](https://github.com/mautic/mautic/releases/tag/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](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](http://mymauticinstall.com), request: “HEAD /mtc.js HTTP/2.0”, host: “[mymauticinstall.com](http://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.

---

<div class="post-metadata">

**Author:** ![honggian](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/honggian/32/947_2.png) [@honggian](https://forum.mautic.org/u/honggian)\
**Post date:** [May 5, 2020, 3:26pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/2 "2020-05-05T15:26:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jtimana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jtimana/32/2352_2.png) [@jtimana](https://forum.mautic.org/u/jtimana)\
**Post date:** [May 5, 2020, 3:38pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/3 "2020-05-05T15:38:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jtimana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jtimana/32/2352_2.png) [@jtimana](https://forum.mautic.org/u/jtimana)\
**Post date:** [May 6, 2020, 9:57pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/4 "2020-05-06T21:57:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![honggian](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/honggian/32/947_2.png) [@honggian](https://forum.mautic.org/u/honggian)\
**Post date:** [May 7, 2020, 12:59am UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/5 "2020-05-07T00:59:23Z")

</div>

@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](https://forum.mautic.org/t/how-does-mautic-form-js-work-in-nginx-please-share-your-nginx-configuration-wordpress-subfolder-with-mautic/14137), but nobody reply for that. Do you have any idea about that?

---

<div class="post-metadata">

**Author:** ![jtimana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jtimana/32/2352_2.png) [@jtimana](https://forum.mautic.org/u/jtimana)\
**Post date:** [May 7, 2020, 1:13am UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/6 "2020-05-07T01:13:20Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![Wise-Gorilla](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/wise-gorilla/32/444_2.png) [@Wise-Gorilla](https://forum.mautic.org/u/Wise-Gorilla)\
**Post date:** [May 14, 2020, 8:42am UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/8 "2020-05-14T08:42:06Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![jtimana](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/jtimana/32/2352_2.png) [@jtimana](https://forum.mautic.org/u/jtimana)\
**Post date:** [June 11, 2020, 4:38pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/9 "2020-06-11T16:38:18Z")

</div>

Yes, I already have that.

---

<div class="post-metadata">

**Author:** ![vivekshekar](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/vivekshekar/32/1414_2.png) [@vivekshekar](https://forum.mautic.org/u/vivekshekar)\
**Post date:** [July 13, 2021, 5:49pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/10 "2021-07-13T17:49:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![humblecoder](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.mautic.org/humblecoder/32/535_2.png) [@humblecoder](https://forum.mautic.org/u/humblecoder)\
**Post date:** [August 24, 2021, 7:37pm UTC](https://forum.mautic.org/t/nginx-config-for-tracking-files-and-forms-getting-404s/14186/11 "2021-08-24T19:37:56Z")

</div>

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?
