NGINX execute embedded PHP in HTML file [duplicate

2019-09-09 11:43发布

This question already has an answer here:

I have the following config:

server {
  listen 80 default_server;

  access_log /var/www/logs/access.log;                                                                                           
  error_log /var/www/logs/error.log error;     

  root /var/www/web/;

  index index.html index.php;

  server_name _;

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

  # HACK: This is temporary to work around renaming dozens of HTML links
  location ~ \.htm$ {
    root html;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.htm;

    include fastcgi_params;
  }
  # HACK: This is temporary to work around renaming dozens of HTML links

  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
      return 404;
    }

    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;

    include fastcgi_params;
  }
}

And I updated /etc/php5/fpm/pool.d/www.conf adding the line:

security.limit_extensions = .php .html 

Restarted FPM and NGINX but when I access .html files the PHP is not rendered...*.php files execute as expected...

What else am I missing???

标签: php nginx
1条回答
beautiful°
2楼-- · 2019-09-09 12:11

I would remove this:

    # HACK: This is temporary to work around renaming dozens of HTML links
  location ~ \.htm$ {
    root html;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.htm;

    include fastcgi_params;
  }

And use this instead:

location ~ \.(php|html|htm)$ {
   root           html;
   fastcgi_pass unix:/var/run/php5-fpm.sock;
   fastcgi_index  index.html;
   fastcgi_param    SCRIPT_FILENAME $document_root$fastcgi_script_name;
   include        fastcgi_params;
}

I hope this solves your issue.

查看更多
登录 后发表回答