Search code examples
phpnginxfastcgi

How to route .html and .js files as PHP on Nginx FastCGI server?


For web servers using PHP as apache module:

AddType application/x-httpd-php .html .htm

For web servers running PHP as CGI:

AddHandler application/x-httpd-php .html .htm 

I have an Nginx server and I want to run .js files and and .htm files as PHP, so I will have full PHP code inside them. Anyone know how to configure the Nginx to do this?


Solution

  • Example for .htm, .html files

      location ~ \.htm$ {
                root           html;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.htm;
                include        fastcgi.conf;
      }
    

    Example for .js files

    location ~ \.js$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
    

    just change the extension and port settings if needed