5

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?

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
  • Are you sure this is a wise idea? It means that for every request for a HTML or JS file, the PHP interpreter will be started. It might be more resource saving to use URL rewriting for this – Pekka Dec 27 '11 at 12:47
  • Can you explain the reasoning for wanting to do this? (It is a possible security risk and drag on the server). – supakeen Dec 27 '11 at 12:47
  • @ikano it's a performance problem but I can't see how it is a security risk, can you elaborate? (Edit: Ah, I suppose you mean that code may get executed in arbitrary uploaded files) – Pekka Dec 27 '11 at 12:48
  • Exactly, code execution in uploaded files :-) – supakeen Dec 27 '11 at 12:49
  • I will serve it on a subdomain. Basically I am creating an ad server for a client. And they want to serve ad codes to their sites. So http://ad.nginx.com/serve.js?client=2343&zone=2&banner=22 example – TheBlackBenzKid Dec 27 '11 at 13:12

6 Answers6

21

Passing to fastcgi didn't work for me. After a few hours of searching, I have found solution here: http://ffct.cc/solving-nginx-php-fpm-access-denied-issue/

In short:

since PHP versions > 5.3.8, to make it works, you should add directive to your php-fpm.conf:

security.limit_extensions = .php .html .js

The recognition sign is "Access denied." (notice that it's different from HTTP error 403) when accesssing .html or .js file.

Stephan
  • 41,764
  • 65
  • 238
  • 329
Tom
  • 311
  • 2
  • 3
12

Simple; just change

location ~ \.php$ {
        root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

to

location ~ \.(php|html|htm)$ {
        root           html;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
Elliott Veares
  • 121
  • 1
  • 2
3

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

TheBlackBenzKid
  • 26,324
  • 41
  • 139
  • 209
Salaros
  • 1,444
  • 1
  • 14
  • 34
1

Replying to this thread many years later, I spent three hours trying to solve this issue, I had all the right settings in my location block (php|html|htm) and in PHP-FPM (security.limit_extensions), my problem was that I already had the .html extension in a previous location which was used for some static files, so if anyone else is having issues despite using the right settings, please make sure that you're not making the same dumb mistake I did :)

Daniel
  • 2,621
  • 2
  • 18
  • 34
TheBbn
  • 11
  • 1
  • Wow! Such a novice mistake to make and we all make these. Great find though! That would have been really painful and tiresome to NOT miss. Interestingly enough you did not just try a PHP and HTML file to see if it worked regardless :) – TheBlackBenzKid Nov 18 '21 at 17:33
0

Tom's answer with the link:

http://ffct.cc/solving-nginx-php-fpm-access-denied-issue/

was really helpful. However, I was using php with php-fpm installed on mac os yosemite w/ homebrew. Changes to the php-fpm.conf file did not take effect until I added the following to my .bash_profile:

 # for homebrew php55
 export PATH="/usr/local/sbin:$PATH"

For details see:

brew info php55
reedbert
  • 51
  • 5
0

In macOS 11.5 with PHP 8.0, when I browse http://localhost/index.html, it ends with 403 Forbidden

I had to change file /usr/local/etc/php/8.0/php-fpm.d/www.conf to set

security.limit_extensions = .php .html

PHP code blocks in my .html files are properly executed, following is my server block in nginx .conf file

server {
  listen 80;
  server_name example.local;

  root "/var/www/html";
  index index.html;

  location / {
    try_files $uri $uri/ =404;
  }

  location ~ \.html$ {
    fastcgi_pass   127.0.0.1:9000;
    include        fastcgi_params;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}
AamirR
  • 11,672
  • 4
  • 59
  • 73