Configure Nginx to render static files and rewrite file extension or proxy_pass

Posted by Pardoner on Server Fault See other posts from Server Fault or by Pardoner
Published on 2012-10-14T17:42:14Z Indexed on 2012/10/26 11:04 UTC
Read the original article Hit count: 219

Filed under:

I've set up Nginx to handle all my static files else proxy_pass to a Node.js server. It's working fine but I'm having difficulty rewriting the url so that it remove the .html file extension.

upstream my_upstream {
        server 127.0.0.1:8000;
        keepalive 64;
}


server {
        listen       80;
        server_name  staging.mysite.com;
        root /var/www/staging.mysite.org/public;

        access_log /var/logs/staging.mysite.org.access.log;
        error_log /var/logs/staging.mysite.org.error.log;

        location ~ ^/(images/|javascript/|css/|robots.txt|humans.txt|favicon.ico) {

            rewrite (.*)\.html $1 permanent;
            try_files  $uri.html  $uri/ /index.html;

            access_log off;
            expires max;
        }   

        location / {

                proxy_redirect off;
                proxy_set_header   X-Real-IP            $remote_addr;
                proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
                proxy_set_header   X-Forwarded-Proto $scheme;
                proxy_set_header   Host                   $http_host;
                proxy_set_header   X-NginX-Proxy    true;
                proxy_set_header   Connection "";
                proxy_http_version 1.1;
                proxy_cache one;
                proxy_cache_key sfs$request_uri$scheme;
                proxy_pass         http://my_upstream;
        }
}

© Server Fault or respective owner

Related posts about nginx