0

I am trying to migrate my website from LAMP to NGINX FastCGI on Linux on AWS and I am having problems trying to parse PHP in some legacy .htm files on my site.

I have tried the solutions listed here:

Specifically, I am using:

location ~ \.(php|html|htm)$ {

and

security.limit_extensions = .php .htm .html

in my /etc/nginx/sites-available/mybrokensite.com and /etc/php-fpm.d/www.conf files.

When I open the .htm files in my browser, I just get a blank page. When I view source, I see the entire raw php and html in the file. If I rename the file with a .php extension it interprets the php and I get the formatted html file that I expect in my browser.

I used the following steps to setup my NGINX FastCGI Wordpress server:

https://gist.github.com/ericandrewlewis/95239573dc97c0e86714

Here is my config:

# Define the microcache path.
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=microcache:100m inactive=60m;

# Redirect http traffic to https
server {
  listen [::]:80;
  listen 80;

  server_name www.mybrokensite.com mybrokensite.com;

  return 301 https://mybrokensite.com$request_uri;
}

# Redirect www https traffic to non-www https
server {
  listen 443 ssl;

  ssl_certificate_key /etc/sslmate/mybrokensite.com.key;
  ssl_certificate /etc/sslmate/mybrokensite.com.chained.crt;

  server_name www.mybrokensite.com;

  return 301 https://mybrokensite.com$request_uri;
}

server {
  listen 443 ssl;

  server_name mybrokensite.com;

  # Include defaults for allowed SSL/TLS protocols and handshake caches.
  include h5bp/directive-only/ssl.conf;

  # config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
  # to avoid ssl stripping https://en.wikipedia.org/wiki/SSL_stripping#SSL_stripping
  add_header Strict-Transport-Security "max-age=31536000; includeSubdomains;";

  ssl_certificate_key /etc/sslmate/mybrokensite.com.key;
  ssl_certificate /etc/sslmate/mybrokensite.com.chained.crt;

  # Path for static files
  root /sites/mybrokensite.com/public;

  #Specify a charset
  charset utf-8;

  # Include the basic h5bp config set
  include h5bp/basic.conf;

  location / {
    index index.php;
    try_files $uri $uri/ /index.php?$args;
  }

  location ~ \.(php|html|htm)$ {
    fastcgi_cache  microcache;
    fastcgi_cache_key $scheme$host$request_method$request_uri;
    fastcgi_cache_valid 200 304 10m;
    fastcgi_cache_use_stale updating;
    fastcgi_max_temp_file_size 1M;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
    include        fastcgi_params;


    # Local variables to track whether to serve a microcached page or not.
    set $no_cache_set 0;
    set $no_cache_get 0;

    # If a request comes in with a X-Nginx-Cache-Purge: 1 header, do not grab from cache
    # But note that we will still store to cache
    # We use this to proactively update items in the cache!
    if ( $http_x_nginx_cache_purge ) {
      set $no_cache_get 1;
    }

    # If the user has a user logged-in cookie, circumvent the microcache.
    if ( $http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
      set $no_cache_set 1;
      set $no_cache_get 1;
    }

    # fastcgi_no_cache means "Do not store this proxy response in the cache"
    fastcgi_no_cache $no_cache_set;
    # fastcgi_cache_bypass means "Do not look in the cache for this request"
    fastcgi_cache_bypass $no_cache_get;
  }
}

My site is mostly a Wordpress site with some legacy .htm files with php in them. I am new to NGINX and any help would be greatly appreciated.

Jesse Nickles
  • 1,435
  • 1
  • 17
  • 25
Dan
  • 13
  • 4
  • You restarted `nginx` and cleared the browser cache? Use `nginx -t` to check that the configuration is ok. – Richard Smith Aug 11 '18 at 08:52
  • Ya I restarted both using sudo service php-fpm restart and sudo service nginx restart. Nginx -t shows nginx.conf syntax is ok and test is successful. – Dan Aug 12 '18 at 00:15
  • If your site consists of mix of php and html, you should set `index index.php index.htm index.html;` to include .html files, and it should be within the server block, not within the location / block. – hcheung Aug 12 '18 at 08:33
  • Thanks for the advice, but adding index.htm and index.html to the index just makes it so that old folders with an index.htm file (ex https://mybrokensite.com/old-dot-html-file/) resolve to an index.htm or index.html file in that folder. My problem with php in a .htm file not getting interpreted still exists. For example, https://mybrokensite.com/old-html-folder/old-html-file-with-php.htm still does not get php interpreted. It is just served as a raw file. – Dan Aug 13 '18 at 21:30
  • @Dan did you find the solution? – psc Nov 26 '19 at 22:57

0 Answers0