【问题标题】:NGINX execute embedded PHP in HTML file [duplicate]NGINX 在 HTML 文件中执行嵌入的 PHP [重复]
【发布时间】:2016-08-16 12:03:13
【问题描述】:

我有以下配置:

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

我更新了 /etc/php5/fpm/pool.d/www.conf 添加以下行:

security.limit_extensions = .php .html 

重新启动 FPM 和 NGINX,但是当我访问 .html 文件时,PHP 未呈现...*.php 文件按预期执行...

我还缺少什么???

【问题讨论】:

  • 我尝试将 PHP 注释掉,所以它只是 html,但也没有用?!?
  • 旁注:你的正则表达式看起来很奇怪:.+?。这意味着“至少一个令牌,这是可选的”。使用.+(至少一个)或.*(零个或多个)。在这种特定情况下,您当然应该使用.+,因为任何文件都不应该以.php 开头。

标签: php nginx


【解决方案1】:

我会删除这个:

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

改用这个:

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

希望这能解决您的问题。

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-03-13
    • 2021-06-06
    • 1970-01-01
    • 2012-12-31
    • 2019-07-02
    • 2019-02-11
    相关资源
    最近更新 更多