nginx rewrite for /blah/(.*) /$1

Posted by skrewler on Server Fault See other posts from Server Fault or by skrewler
Published on 2012-04-04T20:56:10Z Indexed on 2012/04/04 23:32 UTC
Read the original article Hit count: 225

Filed under:
|
|

I'm migrating from mod_php to nginx. I got everything working except for this rewrite.. I'm just not familiar enough with nginx configuration to know the correct way to do this.

I came up with this by looking at a sample on the nginx site.

server {
    server_name test01.www.myhost.com;
    root /home/vhosts/my_home/blah;

    access_log /var/log/nginx/blah.access.log;
    error_log /var/log/nginx/blah.error.log;

    index index.php;

    location / {
            try_files $uri $uri/ @rewrites;
    }

    location @rewrites {
            rewrite ^ /index.php last;
            rewrite ^/ht/userGreeting.php /js/iFrame/index.php last;
            rewrite ^/ht/(.*)$ /$1 last;
            rewrite ^/userGreeting.php$ /js/iFrame/index.php last;
            rewrite ^/a$ /adminLogin.php last;
            rewrite ^/boom\/(.*)$ /boom/index.php?q=$1 last;
            rewrite ^favicon.ico$ favico_ry.ico last;

    }

    # 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";
    }
    include php.conf;
    }

The issue I'm having is with this rewrite:

rewrite ^ht\/(.*)$ /$1 last;

99% of requests that will hit this rewrite are static files. So I think maybe it's getting sent to the static files section and that's where things are being messed up?

I tried adding this but it didn't work:

    location ~* ^ht\/.*\.(?: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";
    }

Any help would be appreciated. I know the best thing to do would be to just change the references of /ht/whatever.jpg to /whatever.jpg in the code.. but that's not an option for now.

© Server Fault or respective owner

Related posts about nginx

Related posts about webserver