Nginx Rewrite Rule For File Within Folder Not Working

Posted by user3620111 on Server Fault See other posts from Server Fault or by user3620111
Published on 2014-06-10T20:23:23Z Indexed on 2014/06/10 21:28 UTC
Read the original article Hit count: 177

Filed under:
|

Good evening everyone or possible early morning if you are in my neck of the woods.

My problem seems trivial but after several hours of testing, researching and fiddling I can't seem to get this simple nginx rewrite function to work.

There are several rewrites we need, some will have multiple parameters but I cant even get this simple 1 parameter current url to alter at all to the desired.

Current: website.com/public/viewpost.php?id=post-title

Desired: website.com/public/post/post-title

Can someone kindly point me to as what I have done wrong, I am baffled / very tired...

For testing purposes before we launch we were just using a simple port on the server. Here is that section.

# Listen on port 7774 for dev test
server {
    listen          7774;
    server_name     localhost;
    root        /usr/share/nginx/html/paa;
    index       index.php home.php index.html index.htm /public/index.php;

location ~* /uploads/.*\.php$ {
    if ($request_uri ~* (^\/|\.jpg|\.png|\.gif)$ ) {
      break;
    }
        return 444;
    }


    location ~ \.php$ {
        try_files $uri @rewrite =404;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_pass php5-fpm-sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

location @rewrite { 
    rewrite ^/viewpost.php$ /post/$arg_id? permanent;
}

}

I have tried countless attempts such as above @rewrite and simpler:

location / {
    rewrite ^/post/(.*)$ /viewpost.php?id=$1 last;
}

    location ~ \.php$ {
    try_files $uri =404;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_pass php5-fpm-sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    }

I can not seem to get anything to work at all, I have tried changing the location tried multiple rules...

Please tell me what I have done wrong.

Pause for facepalm

[relocated from stack overflow as per mod suggestion]

© Server Fault or respective owner

Related posts about nginx

Related posts about rewrite