Call us Toll-Free:
1-800-218-1525
Live ChatEmail us

 Sponsors

NGinx html files as php

Dawn Rossi, 07-09-2009
A typical NGinx config file follows this format:


upstream backend
{
 
server unix:/tmp/fastcgi.sock;
 
server 127.0.0.1:88 backup;
}

 
server
 
{
   
listen  1.2.3.4:80;
   
server_name www.domain.com *.domain.com;
   
server_name_in_redirect off;

   
# set regular docroot
   
location /
    {
     
root /home/domain.com/;
     
index index.php index.html index.htm;
    }

   
location ~ .php$
    {
     
fastcgi_pass backend;
     
fastcgi_index index.php;
     
fastcgi_param SCRIPT_FILENAME /home/domain.com/$fastcgi_script_name;
      include    /
etc/nginx/fastcgi_top.conf;
     
fastcgi_param DOCUMENT_ROOT  /home/domain.com/;
      include    /
etc/nginx/fastcgi_bottom.conf;
    } 
  }

Such a setup tells NGinx to serve all files normally, but pass *.php files to FastiCGI.

At times you may want to treat html files as php. To do so, we simply have to tell NGinx to pass *.php, *.htm and *.html files to FastCGI:


  server
 
{
   
listen  1.2.3.4:80;
   
server_name www.domain.com *.domain.com;
   
server_name_in_redirect off;

   
# set regular docroot
   
location /
    {
     
root /home/domain.com/;
     
index index.php index.html index.htm;
    }

   
location ~* .(htm|html)$
    {
     
fastcgi_pass backend;
     
fastcgi_index index.php;
     
fastcgi_param SCRIPT_FILENAME /home/domain.com/$fastcgi_script_name;
      include    /
etc/nginx/fastcgi_top.conf;
     
fastcgi_param DOCUMENT_ROOT  /home/domain.com/;
      include    /
etc/nginx/fastcgi_bottom.conf;
    } 
  }

We recently had to configure NGinx to support a WordPress site where -

* html files are processed as php
* 404 (not found) *.html files are passed to WordPress (for the clean-urls plugin)
* All other 404's are passed to a php notfound script

This was a little tricky to setup, but we finally came up with this config:


  server
 
{
   
listen  1.2.3.4:80;
   
server_name www.domain.com *.domain.com;

   
server_name_in_redirect off;

   
# set regular docroot
   
location /
    {
     
root /home/domain.com/public_html/;
     
index index.php index.html index.htm;

     
error_page 404 = /blog/notfound.php;
    }

   
location /blog/
    {
     
root /home/domain.com/public_html/;
     
index index.php;
     
     
error_page 404 = /blog/notfound.php;
    }
   
   
location ~* .(php)$
    {
     
fastcgi_pass backend;
     
fastcgi_index index.php;
     
fastcgi_param SCRIPT_FILENAME /home/domain.com/public_html/$fastcgi_script_name;
      include    /
etc/nginx/fastcgi_top.conf;
     
fastcgi_param DOCUMENT_ROOT  /home/domain.com/public_html/;
      include    /
etc/nginx/fastcgi_bottom.conf;

     
error_page 404 = /notfound.php;
    }

   
location ~* .(htm|html)$
    {
     
fastcgi_pass backend;
     
fastcgi_index index.php;
     
fastcgi_param SCRIPT_FILENAME /home/domain.com/public_html/notfound.php;
      include    /
etc/nginx/fastcgi_top.conf;
     
fastcgi_param DOCUMENT_ROOT  /home/domain.com/public_html/;
      include    /
etc/nginx/fastcgi_bottom.conf;

     
error_page 404 = /notfound.php;
    }

  }

The /notfound.php file:


if (file_exists($DOCUMENT_ROOT.$PHP_SELF))
{
require_once(
$DOCUMENT_ROOT.$PHP_SELF);
}
else
{
if (
strstr($PHP_SELF,"/blog"))
{
chdir($DOCUMENT_ROOT."/blog");
require_once(
$DOCUMENT_ROOT."/blog/notfound.php");
}
else
{
Header("Location: /blog/notfound.php");
}
}

And the /blog/notfound.php file:


Header
("HTTP/1.0 200 OK");

$p = $GLOBALS['REQUEST_URI'] = $_SERVER['REQUEST_URI'] = $REQUEST_URI = str_replace(array("%scategory%","/blog"),array("",""),$REQUEST_URI);
require_once(
$DOCUMENT_ROOT."/blog/index.php");

Mike Peters, 07-09-2009
If you're doing 404 redirects, make sure you add these two lines to the /usr/local/nginx/conf/nginx.conf:

# set 404 error page
error_page 404 = /notfound.php;
fastcgi_intercept_errors on;

Here's a complete virtual host entry from our nginx.conf file:


# virtual host entry for spilnk.com
server {
listen 80;
server_name myemaildesk.com www.myemaildesk.com;

# pass all scripts to FastCGI server listening on 127.0.0.1:9000
#
location / {

# set 404 error page
error_page 404 = /notfound.php;

# set the doc root
root /home/myemaildesk/htdocs/;

# pass scripts to FastCGI
fastcgi_pass 127.0.0.1:8888;
fastcgi_index index.php;
include /
etc/nginx/fastcgi.conf;
fastcgi_param SCRIPT_FILENAME /home/myemaildesk/htdocs/$fastcgi_script_name;
fastcgi_intercept_errors on;
}


}

Dheeraj, 07-21-2009
Its basically a query.
I have installed nginx web server in FC4 under /usr/local/nginx/

I place my php pages in /usr/local/nginx/html/

But i am unable to access them.

.conf look like

location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html/$fastcgi_script_name;
include fastcgi_params;
}

What may be problem?

Dheeraj, 07-21-2009
Forgot to tell you.

It says page is temporarly unavailabe.

Regards
Dheeraj
Enjoyed this post?

Subscribe Now to receive new posts via Email as soon as they come out.

 Comments
Post your comments












Note: No link spamming! If your message contains link/s, it will NOT be published on the site before manually approved by one of our moderators.



About Us  |  Contact us  |  Privacy Policy  |  Terms & Conditions