Form won't show via javascrip - 404 not found

I’m trying to add a mautic form to my site via javascript. But the form won’t show.

Code:
When I click to the link https://mt.fususu.com/form/generate.js?id=5 it just show a 404 file not found.
It also happen to mt.fususu.com/mtc.js and the tracking code won't work as well.
I've search around and tried these fixes below, but not work.
1. Checking install base url
2. Add some code in the nginx config.

I installed Mautic on a VPS, nginx 1.12/ php 7.1. Below is my current virtualhost
Code:
server { listen 80; server_name mt.fususu.com; rewrite ^(.*) https://mt.fususu.com$1 permanent; }

server {
listen 443 ssl http2;

# access_log off;
access_log /home/mt.fususu.com/logs/access.log;
# error_log off;
	error_log /home/mt.fususu.com/logs/error.log;

   # some ssl certificate code here 

	root /home/mt.fususu.com/public_html;
index index.php index.html index.htm;
	server_name mt.fususu.com;

location / {
	try_files $uri $uri/ /index.php?$args;

	
}



# Custom configuration
include /home/mt.fususu.com/public_html/*.conf;

	location ~ .php$ {
	fastcgi_split_path_info ^(.+.php)(/.+)$;
    	include /etc/nginx/fastcgi_params;
    	fastcgi_pass 127.0.0.1:9000;
    	fastcgi_index index.php;
	fastcgi_connect_timeout 1000;
	fastcgi_send_timeout 1000;
	fastcgi_read_timeout 1000;
	fastcgi_buffer_size 256k;
	fastcgi_buffers 4 256k;
	fastcgi_busy_buffers_size 256k;
	fastcgi_temp_file_write_size 256k;
	fastcgi_intercept_errors on;
    	fastcgi_param SCRIPT_FILENAME /home/mt.fususu.com/public_html$fastcgi_script_name;
	}
	
location /nginx_status {
	stub_status on;
	access_log   off;
	allow 127.0.0.1;
	deny all;
}
location /php_status {
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	fastcgi_param SCRIPT_FILENAME  /home/mt.fususu.com/public_html$fastcgi_script_name;
	include /etc/nginx/fastcgi_params;
	allow 127.0.0.1;
	deny all;
	}
location ~ /. {
	deny all;
}
    location = /favicon.ico {
            log_not_found off;
            access_log off;
    }
   location = /robots.txt {
	allow all;
	log_not_found off;
	access_log off;
   }
location ~* .(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|woff2|ttf|woff)$ {
        gzip_static off;
	add_header Pragma public;
	add_header Cache-Control "public, must-revalidate, proxy-revalidate";
	access_log off;
	expires 30d;
	break;
    }

    location ~* .(txt|js|css)$ {
        add_header Pragma public;
	add_header Cache-Control "public, must-revalidate, proxy-revalidate";
	access_log off;
	expires 30d;
	break;
    }
	
	
		
location ~ mtc.js{
  try_files $uri @phpstream;
}

location ~ generate.js{
  try_files $uri @phpstream;
}

location @phpstream {
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}	

}

I’m trying to add a mautic form to my site via javascript. But the form won’t show.

<script type="text/javascript" src="//mt.fususu.com/form/generate.js?id=5"></script>

When I click to the link https://mt.fususu.com/form/generate.js?id=5 it just show a 404 file not found.
It also happen to mt.fususu.com/mtc.js and the tracking code won’t work as well.
I’ve search around and tried these fixes below, but not work.

  1. Checking install base url
  2. Add some code in the nginx config.

I installed Mautic on a VPS, nginx 1.12/ php 7.1. Below is my current virtualhost

server {
	listen   80;
	server_name mt.fususu.com;
	rewrite ^(.*) https://mt.fususu.com$1 permanent;
    	}
		
server {
	listen   443 ssl http2;
		
	# access_log off;
	access_log /home/mt.fususu.com/logs/access.log;
	# error_log off;
    	error_log /home/mt.fususu.com/logs/error.log;
	
       # some ssl certificate code here 

    	root /home/mt.fususu.com/public_html;
	index index.php index.html index.htm;
    	server_name mt.fususu.com;
 
   	location / {
		try_files $uri $uri/ /index.php?$args;

		
	}
	


	# Custom configuration
	include /home/mt.fususu.com/public_html/*.conf;
 
    	location ~ .php$ {
		fastcgi_split_path_info ^(.+.php)(/.+)$;
        	include /etc/nginx/fastcgi_params;
        	fastcgi_pass 127.0.0.1:9000;
        	fastcgi_index index.php;
		fastcgi_connect_timeout 1000;
		fastcgi_send_timeout 1000;
		fastcgi_read_timeout 1000;
		fastcgi_buffer_size 256k;
		fastcgi_buffers 4 256k;
		fastcgi_busy_buffers_size 256k;
		fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;
        	fastcgi_param SCRIPT_FILENAME /home/mt.fususu.com/public_html$fastcgi_script_name;
    	}
		
	location /nginx_status {
  		stub_status on;
  		access_log   off;
		allow 127.0.0.1;
		deny all;
	}
	location /php_status {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME  /home/mt.fususu.com/public_html$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;
		allow 127.0.0.1;
		deny all;
    	}
	location ~ /. {
		deny all;
	}
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
       location = /robots.txt {
		allow all;
		log_not_found off;
		access_log off;
       }
	location ~* .(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|woff2|ttf|woff)$ {
	        gzip_static off;
		add_header Pragma public;
		add_header Cache-Control "public, must-revalidate, proxy-revalidate";
		access_log off;
		expires 30d;
		break;
        }

        location ~* .(txt|js|css)$ {
	        add_header Pragma public;
		add_header Cache-Control "public, must-revalidate, proxy-revalidate";
		access_log off;
		expires 30d;
		break;
        }
		
		
			
	location ~ mtc.js{
      try_files $uri @phpstream;
    }
	
	location ~ generate.js{
      try_files $uri @phpstream;
    }
	
	location @phpstream {
    fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
	}	
	
	}

Up, anyone can fix this before?

Hi there?

hi! @fususu

I am facing the same issue.
I am using the following extra setup for Mautic.

My issue is that CSS and JS files are not beeing load.
I am new with NGINX, I have no idea in which direction should I go.

Did you solve it?

Thanks.


START - Mautic Configuration
# redirect index.php to root
	rewrite ^/index.php/(.*) /$1 permanent;
	
# redirect some entire folders
	rewrite ^/(vendor|translations|build)/.* /index.php break;


# ---- START - Mautic Protections ---- #
	
  #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;
	}

  #Don't log favicon
	# It is not necessary because it is already in /usr/local/nginx/conf/drop.conf
	# I am using centminmod.com
	#location = /favicon.ico {
	#	log_not_found off;
	#	access_log off;
	#}

  #Don't log robots
	# It is not necessary because it is already in /usr/local/nginx/conf/drop.conf
	# I am using centminmod.com
	#location = /robots.txt  {
	#	access_log off;
	#	log_not_found off;
	#}

  #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...
	location ~ /. {
		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;
	}
	
  #Allowing the file manager to execute
	location ~ /app/bundles/CoreBundle/Assets/js/libraries/ckeditor/filemanager/connectors/php/filemanager.php {
		allow all;
	}

# ---- END - Mautic Protections ---- #

location / {
# First attempt to serve request as file, then as directory, then fall back to index
try_files $uri $uri/ /index.php$is_args$args;
}

END - Mautic Configuration

Hi @Alcides
Unfortunately I haven’t solved it yet. And this caused the email tracking function not working, too.
But I know for sure it’s the config nginx problem. Due to some restrict config, that nginx looking for the particular file mtc.js, and if not found it give the 404 error, while Mautic using the dynamic content, it will call that file via PHP script.
So I tried to pass the mtc.js file to the php via 127.0.0.1:9000 by these code, but no luck :frowning:

location ~ mtc.js{
      try_files $uri @phpstream;
    }
location @phpstream {
    fastcgi_pass fastcgi_pass 127.0.0.1:9000;
	}

It may work if we pass the php to listen to sock, instead of the ip like this https://stackoverflow.com/questions/44757189/where-is-php7-0-fpm-sock-located but I’m not trying yet.

@fususu

I got what you have said.
Today I rebuild my server completelly to exclude any past mistake but the issue is still there.

I tryied HTTP and HTTPS and the results are the same.

I have no idea for while how to solve it. If you have, please, let me know.
Thanks a lot.

@alcides Not any luck. I’m hiring some guys here to help https://www.freelancer.com/projects/linux/fix-issue-when-using-mautic/#/proposals , minimum is 21$ :)) can we share? maybe he will fix for both of us.

hi! @fususu

Sure If the person really find the reason knowing what they are doing I can share it.

Please tell me about your server environment setup.

Mine is very simple:
1 - CentOS 7
2 -LEMP install script (https://centminmod.com) version 123.09beta01

OBS: I decided to use this version instead of the 123.08stable just because the new one has the hability to auto add a Let’s Encrypt SSL to a virtualhost setup.

Have you tested it?

Thanks,

Hi! @fususu

Based on this post: https://www.mautic.org/community/index.php/5944-e-mail-open-tracking-404/0#p16733

I believe I could solve it my adding the lines bellow:
The 404 Error is not anymore but I sill have some CSS load errors.


Solve e-Mail Open Tracking 404 Error

location ~ email/(.*).gif {
	try_files $uri /index.php?$args;
	}

Solve JS Loading 404 Error

location ~ (.*).js {
	try_files $uri /index.php?$args;
	}

Please, have a look if it can solve also your issue.

Best regards!

Alcides

[quote=26216:@Alcides]Hi! @fususu


Solve e-Mail Open Tracking 404 Error

location ~ email/(.*).gif {
	try_files $uri /index.php?$args;
	}

Solve JS Loading 404 Error

location ~ (.*).js {
	try_files $uri /index.php?$args;
	}

[/quote]

I tried this before but not work. I’m using Centos 7 and LEMP just like you.
Still waiting for the miracle happens…

Hey, after one hour messing around with the nginx config I found the root cause of my problem and solved it!
Here is what I did:

  1. I notice that when visit mydomain.com/whatever --> it show a mautic 404 page. but when visit mydomain.com/mtc.js it show just a 404 of Nginx ==> it must be some pre-configs related to that the file js causes this.
  2. Then I search in my nginx config and see that there is another block that config the css & js file:
 location ~* .(txt|js|css)$ {
	      add_header Pragma public;
	     add_header Cache-Control "public, must-revalidate, proxy-revalidate";
		access_log off;
		expires 30d;
		break;
        }
  1. I just comment all the code. Then the miracle appeared! 404 not found of mydomain/mtc.js gone!

P.s. I also add these code below to config the js & gif file

              # Solve email tracking pixel not found
		location ~ email/(.*).gif {
		try_files $uri /index.php?$args;
		}
		# Solve JS Loading 404 Error
		location ~ (.*).js {
		try_files $uri /index.php?$args;
		}

P.p.s. this solution also help me solve open mail tracking issues. I just delete |gif from the code in my pre-config

location ~* .(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|woff2|ttf|woff)$ { gzip_static off; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; access_log off; expires 30d; break; }

@fususu Can you please publish the full mautic config for nginx?

Here is my full nginx that works well with mautic now, also using SSL.

server {
	listen   80;
	
	server_name subdomain.mydomain.com;
	rewrite ^(.*) https://subdomain.mydomain.com$1 permanent;
    	}
		
server {
	listen   443 ssl http2;
		
	# access_log off;
	access_log /home/subdomain.mydomain.com/logs/access.log;
	# error_log off;
    	error_log /home/subdomain.mydomain.com/logs/error.log;
	
    	root /home/subdomain.mydomain.com/public_html;
	index index.php index.html index.htm;
    	server_name subdomain.mydomain.com;

#( some SSL related was removed )
 
    	location / {
		try_files $uri $uri/ /index.php?$args;
		
	}
	
	# Custom configuration
	include /home/subdomain.mydomain.com/public_html/*.conf;
 
    	location ~ .php$ {
		fastcgi_split_path_info ^(.+.php)(/.+)$;
        	include /etc/nginx/fastcgi_params;
        	fastcgi_pass 127.0.0.1:9000;
        	fastcgi_index index.php;
		fastcgi_connect_timeout 1000;
		fastcgi_send_timeout 1000;
		fastcgi_read_timeout 1000;
		fastcgi_buffer_size 256k;
		fastcgi_buffers 4 256k;
		fastcgi_busy_buffers_size 256k;
		fastcgi_temp_file_write_size 256k;
		fastcgi_intercept_errors on;
        	fastcgi_param SCRIPT_FILENAME /home/subdomain.mydomain.com/public_html$fastcgi_script_name;
    	}
		
	location /nginx_status {
  		stub_status on;
  		access_log   off;
		allow 127.0.0.1;
		deny all;
	}
	location /php_status {
		fastcgi_pass 127.0.0.1:9000;
		fastcgi_index index.php;
		fastcgi_param SCRIPT_FILENAME  /home/subdomain.mydomain.com/public_html$fastcgi_script_name;
		include /etc/nginx/fastcgi_params;
		allow 127.0.0.1;
		deny all;
    	}
	location ~ /. {
		deny all;
	}
        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }
       location = /robots.txt {
		allow all;
		log_not_found off;
		access_log off;
       }
	location ~* .(3gp|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|eot|svg|woff2|ttf|woff)$ {
	        gzip_static off;
		add_header Pragma public;
		add_header Cache-Control "public, must-revalidate, proxy-revalidate";
		access_log off;
		expires 30d;
		break;
        }

        #location ~* .(txt|js|css)$ {
	    #add_header Pragma public;
		#add_header Cache-Control "public, must-revalidate, proxy-revalidate";
		#access_log off;
		#expires 30d;
		#break;
        #}
		
		
		location ~* .(eot|otf|ttf|woff|woff2)$ {
			add_header Access-Control-Allow-Origin *;
		}
		
		# Solve email tracking mautic
		location ~ email/(.*).gif {
		try_files $uri /index.php?$args;
		}
		# Solve JS Loading 404 Error
		location ~ (.*).js {
		try_files $uri /index.php?$args;
		add_header Access-Control-Allow-Origin *;
		}
	
	}

@fususu what does this line have include /home/subdomain.mydomain.com/public_html/*.conf;

I get error 405 for /mtc/event and 404 for mtracking.gif

Fixed error 404 for mtracking.gif. Edited

location ~ email/(.*).gif { try_files $uri /index.php?$args; }
to

location ~ (.*).gif { try_files $uri /index.php?$args; }

Some wordpress plugins may create custom nginx file in the public_html folder, this line will include that custom conf file. Anyway, it’s not related to Mautic ^^!


Nice to be here with you guys, let’s make a great community.
My blog: https://fususu.com/en/ My fb: Facebook

[quote=26769:@fususu]Some wordpress plugins may create custom nginx file in the public_html folder, this line will include that custom conf file. Anyway, it’s not related to Mautic ^^!


Nice to be here with you guys, let’s make a great community.
My blog: https://fususu.com/en/ My fb: https://fb.com/fususuvn[/quote]

Why did you put “/home/subdomain.mydomain.com/public_html” at PHP location?

It’s related to some optimized configs for the VPS that was pre-installed before. I used the script here to setup VPS in 15 minutes with all things I need Cài đặt và Quản lý server dễ dàng với HocVPS Script - Học VPS