Skip to main content

Server portion for setting up a proxy to Lucee with rewrite to have index.cfm omitted for SES

Credit: Andy Jarrett

http {

  ...
  server {
    listen 80;
    server_name  example.com;
    root /var/www/exmaple.com/www/;
    index index.cfm;
    set $lucee_context "example.com";
    
    autoindex on;
    
    location / {
      # Rewrite rules and other criterias can go here
      # Remember to avoid using if() where possible (http://wiki.nginx.org/IfIsEvil)
      try_files $uri $uri/ @rewrites;
    }

    # This block will catch static file requests, such as images, css, js
    # The ?: prefix is a 'non-capturing' mark, meaning we do not require
    # the pattern to be captured into $1 which should help improve performance
    location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
      # Some basic cache-control for static files to be sent to the browser
      expires max;
      add_header Pragma public;
      add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    }

    location @rewrites {
      # Can put some of your own rewrite rules in here
      # for example rewrite ^/~(.*)/(.*)/? /users/$1/$2 last;
      rewrite ^/(.*)? /index.cfm/$1 last;
    }

    #block requests for Application.cfc/cfm
    location ~* Application.cf[mc]$ {
      return 404;
    }

    location ~* /lucee-server {
      return 404;
    }

    #block/ignore CFIDE requests
    location ~* /CFIDE {
      return 404;
    }

    location ~* /lucee/ {
      allow 10.0.0.1;
      deny all;
    }

    # Main Lucee proxy handler
    location ~ \.(cfm|cfml|cfc|jsp|cfr)(.*)$ {
      set $path_info $request_uri;
      proxy_pass http://127.0.0.1:8080;
      proxy_read_timeout 100s;
      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_set_header X-Forwarded-Proto $scheme;
      proxy_set_header https $cgi_https;
      proxy_set_header X-Tomcat-DocRoot $document_root;
      proxy_set_header X-ModCFML-SharedKey 8DoB0PzqdMn4zo0Y2bq7dzqynbqBVsK0piuNslpNU6982KaiCqVT8X;
      proxy_set_header X-Webserver-Context $lucee_context;
      #proxy_set_header XAJP-PATH-INFO $pathinfo;
    }
  } 
}