6

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?

5
  • 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 Commented Dec 27, 2011 at 12:47
  • Can you explain the reasoning for wanting to do this? (It is a possible security risk and drag on the server). Commented Dec 27, 2011 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) Commented Dec 27, 2011 at 12:48
  • Exactly, code execution in uploaded files :-) Commented Dec 27, 2011 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 ad.nginx.com/serve.js?client=2343&zone=2&banner=22 example Commented Dec 27, 2011 at 13:12

6 Answers 6

22

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.

Sign up to request clarification or add additional context in comments.

Comments

13

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;
    }

2 Comments

location ~ \.php|\.html|\.htm$ { ... would also work.
Another +1 for simplicity.
4

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

2 Comments

Can it be limited to subdomains. Only .JS files I want to do this in a select folder area.
hmmm, getting a 502 now on all my js files.
2

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 :)

1 Comment

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 :)
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

Comments

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;
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.