In short, I need to run a specific php file, patch.php, every time someone requests an html page on this site...

We recently moved from an Apache server to an Nginx server, and everything has been great. However, this one site that looks like it was coded ages ago has a very peculiar schema:

All the links on all of the pages link to .html files, but the html files are missing the header and the footer

I'm not sure how this site worked before, but the way I see to fix it is to simply tell nginx that any time someone requests an html file, simply serve them a php file that will include the header, the page they want, and the footer.

But my attempts so far have not been successful. From this question, I made the following:

    location ~\.(htm|html)$ {
            root /home/www-dev/domain.com/web;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index patch.php; #the patch file I want to run for all html
            include fastcgi_params;

Which was much better than my first couple of attempts, but, it does not work -- Oddly enough, it tells me "Access Denied" - which is not what the usual error page looks like, but the ownership and permissions are correct, so I don't think that is related.

So, I would really like to know what I'm doing wrong --- thanks in advance - !

  • What was the original Apache configuration for the server? – Michael Hampton May 30 '14 at 18:43
  • I have no idea. I could not find an .htaccess file, but I guess the next step is to look harder. – rm-vanda May 30 '14 at 19:29
  • I'm not sure how this site worked before - probably by using auto_prepend_file and auto_append_file. If you look at the php fpm logs most likely it's telling you that files don't exist - because SCRIPT_NAME SCRIPT_FILENAME are going to be set from the request. – AD7six May 30 '14 at 19:30
  • Hmmm. But wouldn't that only append to *.php files? – rm-vanda May 30 '14 at 19:37
  • Showing the apache config would clarify. That means the files in /etc/apache/.... Also, you should show your whole nginx config (the server block). not just the one location block that's currently in the question. – AD7six May 30 '14 at 19:40

You can redirect all request to a single php file/url:

  1. setup nginx to serve your patch.php (somthing like):

location /patch\.php { fastcgi_pass unix:/path/to/your/php-fastcgi.socket; fastcgi_param SCRIPT_FILENAME /path/to/your/file/patch.php; }

  1. redirect all requests to your patch.php

rewrite ^(.*)$ /patch.php break;

| improve this answer | |
  • Can you explain what the break would do ? -- I'm not sure how that would impact our SEO efforts... – rm-vanda May 30 '14 at 18:54
  • let's have a look at the nginx docs (@ nginx.org/en/docs/http/ngx_http_rewrite_module.html) break => Stops processing the current set of ngx_http_rewrite_module directives. – deagh May 30 '14 at 18:56
  • 1
    rewrite ^(.*)$ - that'll make it impossible to serve any static files - such as /favicon.ico – AD7six May 30 '14 at 19:34
  • I'm sorry, I read the docs, but I don't understand it; it doesn't talk about how that is different from permanent which is the only other one I know. – rm-vanda May 30 '14 at 19:34
  • 1
    it would be easy enough to save the REQUEST_URI into a Session variable - Above you say you understand the rewrite to be a redirect which if it were true REQUEST_URI would be /patch.php - there's a lot of assumption and guess work going on instead of editing the question to add the information that would make it answerable. – AD7six May 31 '14 at 8:48

One possible solution to your problem.

This solution assumes that your patch.php is in /home/www-dev/nasa.gov/web/patch.php file.

server {
    server_name your.domain.name;
    root /home/www-dev/nasa.gov/web;

    rewrite \.(htm|html)$ /patch.php break;

    location /patch.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;

The first location block matches all files ending with htm/html suffix, and makes an internal rewrite to pass them all to /patch.php. This has nothing to do with HTTP redirects that are sent when using permanent (301) or redirect (302) modifier, so this has no effect on SEO.

The second location tells to process /patch.php via the PHP instance. If you want to enable general processing of PHP scripts, you should use this instead:

location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
| improve this answer | |
  • 1
    Good point, the location block is not needed here. I'll edit the answer. – Tero Kilkanen Jun 1 '14 at 9:53

Change the configuration of your webserver in a way that .html files are parsed as PHP. I won't go into details on how to do that, there are plenty of guides out there already.

Then, change your php.ini and add auto-prepend-file and auto-append-file.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.