proxy_pass to Apache config not working like fastcgi_pass

nerdgrind nginx-forum at nginx.us
Sun Dec 6 07:46:04 MSK 2009


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: http://forum.nginx.org/read.php?2,28327,28327#msg-28327




More information about the nginx mailing list