routing nginx request through index file

Brian FitzGerald asked:

I would like for nginx to process: as

While loading the index.cfm file, Nginx must also pass along the “cgi path info” (/about-us) for server processing. I have made many attempts, but I am apparently world’s worst with mod_rewrite, nginx congigs, etc.

This all works in apache with the following rewrite rules:

RewriteEngine On

# exclude files from rewriting
RewriteCond %{REQUEST_FILENAME} !-f

# rewrite rules, NS skips rewrite rule for internal sub-requests
# removes index.cfm from the URL
RewriteRule ^(.*)$ /index.cfm/$1 [NS,L]

============== nginx config file ============

This is my config file (with many failed attempts): Please don’t look at it directly or you may be turned to stone. Quick glances and use of peripheral vision highly recommended.

server {

    listen 80 default_server;
    server_name  _;
    index  index.cfm index.html index.htm;
    root   /var/www;
    server_name_in_redirect off;

    set $path_info "";

    #test url:

    #attempt1 -> server 404 not found
    #if (!-e $request_filename){
        #rewrite ^(.*)$ /index.cfm/$1 break;

    #attempt2 -> loads index.cfm file, CGI path_info is blank
    #location / {
        #try_files $uri /index.cfm$1;

    #attempt3 (server 500 error)
    #try_files $uri /index.cfm/$args;

    #if (!-e $request_filename){
       #try_files $uri /index.cfm$args;
       #set $path_info $args;

    #attempt5 > 404 not found
    #rewrite ^(.*)$ /index.cfm$1;
    #set $path_info $1;

    #attempt6 > 404 not found
    #rewrite ^(.*)$ /index.cfm$1 break;
    #set $path_info $1;

    #attempt7 > loading index.cfm file but not passing $path_info
    #if ($uri ~ "^(.+.cfm)(/.+)") {
        #set $path_info $2;

    #try_files $uri /index.cfm$args;

    try_files $uri /index.cfm$args;

    # if the extension .cfm or .cfc is found, followed by a slash and optional extra
    if ($uri ~ "^(.+?.cf[mc])(/.*)") {
        # remember the filepath without path_info
        set $script $1;
        set $path_info $2;
        # rewrite the url to match the filepath wthout path_info
        # rewrite ^.+$ $script break;
    # set the custom path_info header
    proxy_set_header XAJP-PATH-INFO $path_info;

    location ~* .(cfm|cfc|cfr)$ {
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_connect_timeout 600;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        send_timeout 600;


My answer:

You’re on the right track with try_files. But you want to append the complete URL path, not the query string as you had been doing. For instance:

try_files $uri /index.cfm$uri;

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.