nginx rewrites, a short guide

Collapse
X
Collapse
 

  • Zachery
    started a blog post nginx rewrites, a short guide

    nginx rewrites, a short guide

    The following is a guide on how to modify what would be a default install of ubuntu's nginx and php-fpm to allow vBulletin rewrites to work.
    The first step is adding a catch to handle some legacy requests for vBulletin to your generic php capture

    You should have something like the following:

    Code:
    	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    	#
    	location ~ \.php$ {
    		fastcgi_split_path_info ^(.+\.php)(/.+)$;
    		# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    	
    		# With php5-cgi alone:
    		#fastcgi_pass 127.0.0.1:9000;
    		# With php5-fpm:
    		fastcgi_pass unix:/var/run/php5-fpm.sock;
    		fastcgi_index index.php;
    		include fastcgi_params;
                    
    	}
    Or perhaps
    Code:
    	location ~ \.php$ {
    We need to add a rewrite directive at the bottom of this, so for this snipet of code, you need to find the } that closes it, and add the following code before it

    Code:
    		#vBulletin Legacy Rewrites
                    if (!-f $request_filename) {
    			rewrite ^[COLOR=red]$PATH$[/COLOR]/(.*)$ [COLOR=red]$PATH$[/COLOR]/index.php?routestring=$1 break;
                    }
    After, in my first example it should look something like this

    Code:
    	# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    	#
    	location ~ \.php$ {
    		fastcgi_split_path_info ^(.+\.php)(/.+)$;
    		# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    	
    		# With php5-cgi alone:
    		#fastcgi_pass 127.0.0.1:9000;
    		# With php5-fpm:
    		fastcgi_pass unix:/var/run/php5-fpm.sock;
    		fastcgi_index index.php;
    		include fastcgi_params;
    		#vBulletin Legacy Rewrites
                    if (!-f $request_filename) {
    			rewrite ^[COLOR=red]$PATH$[/COLOR]/(.*)$ [COLOR=red]$PATH$[/COLOR]/index.php?routestring=$1 break;
                    }
    	}
    Now, that we've got that sorted, you need to add the following code, somewhere in the vhosts file, either before or after the php setup, in my testing it didn't seem to matter much.

    Code:
    ## vbulletin rewrites
    	#CSS Rewrite
           location = [COLOR=red]$PATH$[/COLOR]/css\.php {
                    rewrite ^ [COLOR=red]$PATH$[/COLOR]/core/css.php break;
            }
    
            # make install available from presentation
            location ^~ [COLOR=red]$PATH$[/COLOR]/install {
                    rewrite ^[COLOR=red]$PATH$[/COLOR]/install/(.*)$ [COLOR=red]$PATH$[/COLOR]/core/install/$1 redirect;
            }
    
            # any request to not existing item gets redirected through routestring
            location [COLOR=red]$PATH$[/COLOR]/ {
                    if (!-f $request_filename) {
                            rewrite ^[COLOR=red]$PATH$[/COLOR]/(.*)$ [COLOR=red]$PATH$[/COLOR]/index.php?routestring=$1 last;
                    }
            }
    
            # make admincp available from presentation
            location ^~ [COLOR=red]$PATH$[/COLOR]/admincp {
                    if (!-f $request_filename) {
                            rewrite ^[COLOR=red]$PATH$[/COLOR]/admincp/(.*)$ [COLOR=red]$PATH$[/COLOR]/index.php?routestring=admincp/$1 last;
                    }
            }
    ## end vBulletin normal rewrites
    A note on paths: Some people may run vBulletin in the root of their nginx vhost, in which case, you can remove any occurance of $PATH$ and these rules should work fine.
    If you ran your site as example.com/forums/ You'd have to replace $PATH$ with /forums for every occurance, an example of the change would be

    Code:
            # make admincp available from presentation
            location ^~ /forums/admincp {
                    if (!-f $request_filename) {
                            rewrite ^/forums/admincp/(.*)$ /forums/index.php?routestring=admincp/$1 last;
                    }
            }
    A quick find and replace via your favorite text editor should let you update everything properly
      Posting comments is disabled.

    Related Topics

    Collapse

    Working...