Proxy_pass to Apache config not working like fastcgi_pass

I setup Nginx as the frontend server, and Apache as the backend to serve
only PHP for Wordpress, however, after checking the headers I found
Apache is sending cache-control, and other headers that should only be
handled from the Nginx configuration. This leads me to believe that
Apache may be serving the static as well as PHP files, when it should
only be serving the PHP files.

To isolate the issue I tried using fastcgi_pass with php-fcgi, and found
the headers were only generated by Nginx as it should be.

When using Nginx as Frontend, and Apache as the backend proxy to serve
PHP I configured (The full example at the bottom of this post shows the
entire configuration):

#static files
location ~
.(js|css|jpg|jpeg|gif|png|ico|flv|mp3|mpg|mpeg|zip|tgz|rar|bz2|doc|xls|exe|pdf|ppt|tar|mid|midi|wav|bmp|rtf)$
{
#statics files handled by WP Super Cache
location ~ .(gz|html|meta|htm|xml|txt)$ {

dynamic PHP files handled by Apache

    location / {
        proxy_pass         http://127.0.0.1:8008;
    }

The above config allows Nginx to act as the frontend, and Apache to act
as the backend, but Apache and Nginx both send cache-control and other
headers, so I am not sure if Apache is handling all the content and not
just PHP.

To Isolate Apache to serve only PHP files I tried this configuration:

#static files
location ~
.(js|css|jpg|jpeg|gif|png|ico|flv|mp3|mpg|mpeg|zip|tgz|rar|bz2|doc|xls|exe|pdf|ppt|tar|mid|midi|wav|bmp|rtf)$
{
#statics files handled by WP Super Cache
location / {

dynamic PHP files handled by Apache

    location ~ \.php$ {
        proxy_pass         http://127.0.0.1:8008;
    }

Note the WP Supercache files are now “/” and the apache proxy now has
the regular expression “~ .php$” After loading this configuration the
homepage would load, but when I tried to go to a single post page I
would be forwarded back to the homepage.

When I configure Nginx as the frontend and php-fcgi to serve PHP I am
able to use the regular expression “~ .php$” with fastcgi_pass that
does not work with proxy_pass:

fastcgi_pass

location ~ .php$ {
#static files
location ~
.(js|css|jpg|jpeg|gif|png|ico|flv|mp3|mpg|mpeg|zip|tgz|rar|bz2|doc|xls|exe|pdf|ppt|tar|mid|midi|wav|bmp|rtf)$
{
#statics files handled by WP Super Cache
location ~ .(gz|html|meta|htm|xml|txt)$ {

With fastcgi_pass I can now switch from “/” for the WP Super Cache files
to a more specific regular expression “~ .(gz|html|meta|htm|xml|txt)$”
I can also use the regular expression “~ .php$” with fastcgi_pass so I
can be 100% certain that php-fcgi is only serving PHP files, and nothing
else.

How can I configure Nginx as the fronend, and Apache as the backend to
serve only PHP, and nothing else, not even cache-control headers, or is
that a normal response that doesn’t mean Apache is serving anything but
PHP? How can I analyze the Nginx and Apache responses to be sure Apache
is only handling PHP?

Full examples below:

######### Nginx as Frontend, and Apache as the backend proxy to serve
PHP ######

server {
server_name www.mydomain.com;
listen 80;

error_page  404  http://www.mydomain.com/e404.php;

location ~ 

.(js|css|jpg|jpeg|gif|png|ico|flv|mp3|mpg|mpeg|zip|tgz|rar|bz2|doc|xls|exe|pdf|ppt|tar|mid|midi|wav|bmp|rtf)$
{
root /var/www/mydomain;
expires max;
gzip on;
gzip_http_version 1.0;
gzip_vary on;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/css application/json
application/x-javascript text/xml application/xml application/xml+rss
text/javascript;
gzip_buffers 16 8k;
gzip_disable “MSIE [1-6].”;
break;
}

    location ~ \.(gz|html|meta|htm|xml|txt)$ {
           root   /var/www/mydomain;
           index  index.php index.html index.htm;
  expires off;
  gzip  on;
        gzip_http_version 1.0;
        gzip_vary on;
        gzip_comp_level 3;
        gzip_proxied any;
        gzip_types text/plain text/css application/json 

application/x-javascript text/xml application/xml application/xml+rss
text/javascript;
gzip_buffers 16 8k;
gzip_disable “MSIE [1-6].”;

if the requested file exists, return it immediately

           if (-f $request_filename) {
                   break;
           }

           set $supercache_file '';
           set $supercache_uri $request_uri;

           if ($request_method = POST) {
                   set $supercache_uri '';
           }

Using pretty permalinks, so bypass the cache for any query string

           if ($query_string) {
                   set $supercache_uri '';
           }

           if ($http_cookie ~* 

“comment_author_|wordpress|wp-postpass_” ) {
set $supercache_uri ‘’;
}

if we haven’t bypassed the cache, specify our supercache file

           if ($supercache_uri ~ ^(.+)$) {
                   set $supercache_file 

/wp-content/cache/supercache/$http_host/$1index.html;
}

only rewrite to the supercache file if it actually exists

           if (-f $document_root$supercache_file) {
                   rewrite ^(.*)$ $supercache_file break;
           }

all other requests go to Wordpress

           if (!-e $request_filename) {
                   rewrite . /index.php last;
           }

    }

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
 location ~ /\.ht {
 deny  all;
 }

 # To Serve PHP files with Apache as a backend
    location / {
        proxy_pass         http://127.0.0.1:8008;
    }

}

######### Nginx as Frontend, and PHP-FCGI to serve PHP ######

server {
server_name www.mydomain.com;
listen 80;

error_page  404  http://www.mydomain.com/e404.php;

# For php-fcgi if implemented
    location ~ \.php$ {
           include /usr/local/nginx/conf/fastcgi_params;
            fastcgi_pass  127.0.0.1:9001;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME 

/var/www/mydomain$fastcgi_script_name;
}

location ~ 

.(js|css|jpg|jpeg|gif|png|ico|flv|mp3|mpg|mpeg|zip|tgz|rar|bz2|doc|xls|exe|pdf|ppt|tar|mid|midi|wav|bmp|rtf)$
{
root /var/www/mydomain;
expires max;
gzip on;
gzip_http_version 1.0;
gzip_vary on;
gzip_comp_level 3;
gzip_proxied any;
gzip_types text/plain text/css application/json
application/x-javascript text/xml application/xml application/xml+rss
text/javascript;
gzip_buffers 16 8k;
gzip_disable “MSIE [1-6].”;
break;
}

    location ~ \.(gz|html|meta|htm|xml|txt)$ {
           root   /var/www/mydomain;
           index  index.php index.html index.htm;
  expires off;
  gzip  on;
        gzip_http_version 1.0;
        gzip_vary on;
        gzip_comp_level 3;
        gzip_proxied any;
        gzip_types text/plain text/css application/json 

application/x-javascript text/xml application/xml application/xml+rss
text/javascript;
gzip_buffers 16 8k;
gzip_disable “MSIE [1-6].”;

if the requested file exists, return it immediately

           if (-f $request_filename) {
                   break;
           }

           set $supercache_file '';
           set $supercache_uri $request_uri;

           if ($request_method = POST) {
                   set $supercache_uri '';
           }

Using pretty permalinks, so bypass the cache for any query string

           if ($query_string) {
                   set $supercache_uri '';
           }

           if ($http_cookie ~* 

“comment_author_|wordpress|wp-postpass_” ) {
set $supercache_uri ‘’;
}

if we haven’t bypassed the cache, specify our supercache file

           if ($supercache_uri ~ ^(.+)$) {
                   set $supercache_file 

/wp-content/cache/supercache/$http_host/$1index.html;
}

only rewrite to the supercache file if it actually exists

           if (-f $document_root$supercache_file) {
                   rewrite ^(.*)$ $supercache_file break;
           }

all other requests go to Wordpress

           if (!-e $request_filename) {
                   rewrite . /index.php last;
           }

    }

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
 location ~ /\.ht {
 deny  all;
 }

}

Posted at Nginx Forum:

I can’t speak for supercache, other people use it here with nginx, but
WHY would you proxy to Apache for PHP? :slight_smile:

Take Apache out of the mix, you’re wasting resources. Use PHP-FPM,
spawn-fcgi, or php -b. Why have an entire webserver daemon and its
overhead around just to parse PHP files.

As far as supercache rewrites/configuration/etc. in nginx, there’s
been previous threads. All I know is I am usually able to do whatever
I need in nginx with minimal configuration. This thing is a monster.

I run Red Hat Enterprise Linux 5 server. If I were to install PHP-FPM,
which patches the PHP package, it would be replaced when PHP is updated
on the next yum update. I have spawn-cgi installed, and can run it only
from the command line as such:

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9001 -C 5 -u apache -g apache
-f “/usr/bin/php-cgi /maxconns=1024 /maxreqs=1024” -P
/var/run/fastcgi-php.pid

When I try running fastcgi from an init script I get the following
message when I go to any page on my blog:

The webpage at
https://my_IP_address_omitted/init/start_stop.cgi?file=%2Fetc%2Frc%2Ed%2Finit%2Ed%2Fphp%2Dfastcgi&name=php%2Dfastcgi&start=1&back=edit%5Faction%2Ecgi%3F0%2Bphp%2Dfastcgi
might be temporarily down or it may have moved permanently to a new web
address.

It doesn’t matter if I user the nginx user and group.

Apache can run PHP reliably on heavy loads, and it gets updated
automatically on a yum update, so for me it works, and it automatically
restarts if the sever does, because the init script works.

I’ve read that under heavy load Apache can handle PHP much more reliably
than fastcgi as well, which is what I’m after.

Apache running as a backend in the configuration I cited above uses as
much or less memory and CPU cycles as fastcgi, so the only issues for me
are reliability, and the ability to have an init script that works.

I am also concerned about SEO:

My config handles different files in different ways as you can see. I
have several high traffic blogs, and they have very specific needs.

Posted at Nginx Forum:

On Sat, Dec 5, 2009 at 9:28 PM, nerdgrind [email protected] wrote:

I run Red Hat Enterprise Linux 5 server. If I were to install PHP-FPM, which patches the PHP package, it would be replaced when PHP is updated on the next yum update. I have spawn-cgi installed, and can run it only from the command line as such:

well you don’t run php from an rpm package and php-fpm on top of it.
you pick one or the other :stuck_out_tongue:

if you are really glued to using php from a repository than spawn-fcgi
is your better choice for now

/usr/local/bin/spawn-fcgi -a 127.0.0.1 -p 9001 -C 5 -u apache -g apache -f “/usr/bin/php-cgi /maxconns=1024 /maxreqs=1024” -P /var/run/fastcgi-php.pid

if you’re having issues with spawn-fcgi (which i can’t tell if you are
or not) i would check if this is legit “/usr/bin/php-cgi
/maxconns=1024 /maxreqs=1024” - i was not aware that php-cgi took
command line arguments like a windows command (but maybe the behavior
has changed and i just don’t pay attention because i don’t need to)

Apache can run PHP reliably on heavy loads, and it gets updated automatically on a yum update, so for me it works, and it automatically restarts if the sever does, because the init script works.

Apache can easily choke on heavy loads. Been there, done that. As have
many people here. I’d cite benchmarking but it seems like everyone’s
got a page of benchmarks out there and Apache chokes, Lighty chokes
sometimes, nginx never chokes.

I’ve read that under heavy load Apache can handle PHP much more reliably than fastcgi as well, which is what I’m after.

Where?

Apache running as a backend in the configuration I cited above uses as much or less memory and CPU cycles as fastcgi, so the only issues for me are reliability, and the ability to have an init script that works.

Where?

I am also concerned about SEO: nginx Compatibility – WordPress plugin | WordPress.org

I run wordpress with full SEO (I assume “friendly URLs”) as do tons of
people on this list with a single line of nginx configuration code. I
have been for years now (well, the new single line option happened
somewhere along the way, before that it was 3 lines)

See:
http://michaelshadle.com/2009/03/19/finally-using-nginxs-try-files-directive/

My config handles different files in different ways as you can see. I have several high traffic blogs, and they have very specific needs.

You can probably simplify things a lot. Use includes for redundant
portions. I have one for expires headers for graphics/assets. etc.

Mike can you email me your config files, and include files, so I can
take a fresh look at your approach? nerdfinds at nerdgrind.com

Posted at Nginx Forum:

If someone else can help answer my configuration questions regarding how
to isolate proxy_pass using a regular expression that would be great.

Posted at Nginx Forum: