ablazq
ablazq

Reputation: 35

Nginx server not redirecting properly

I'm getting a 404 error in nginx, when I should be getting a redirection instead. I come from Apache and this is the first time I play with Nginx. I suppose it has to be a minor mistake that I'm overlooking.

This is the code:

server {
    listen   80 default_server;
    server_name  localhost;
    root   /var/www/resizing/public_html;
    index index.php index.html index.htm;

    location ~ ^/(.*)/cache/(.*)$
    {
            try_files $uri @resize;
            expires 4h;
    }

    location / {
            expires 4h;
    }

    location ~ \.php(.*)$  {
           #fastcgi_pass 127.0.0.1:8000;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
    }

    location @resize {
            rewrite ^/(.*)/cache/(.*)$ /resize.php?dir=$1&path=$2;
            include fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
    }



location /doc/ {
    alias /usr/share/doc/;
    autoindex on;
    allow 127.0.0.1;
    allow ::1;
    deny all;
}

# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
#   proxy_pass http://127.0.0.1:8080;    
#}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
#   root /usr/share/nginx/html;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
#   fastcgi_split_path_info ^(.+\.php)(/.+)$;
#   # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
#
#   # With php5-cgi alone:
#   fastcgi_pass 127.0.0.1:9000;
#   # With php5-fpm:
#   try_files $uri =404;
#   fastcgi_pass unix:/var/run/php5-fpm.sock;
#   fastcgi_index index.php;
#   fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#   include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
#   deny all;
#}

}

For instance if I type: localhost/cache/150x200-0/originals/1.jpg I get a 404error.

Upvotes: 0

Views: 1137

Answers (1)

Maxim Dounin
Maxim Dounin

Reputation: 6755

The problem is that the URI is /cache/150x200-0/originals/1.jpg, and it isn't matched by the location ~ ^/(.*)/cache/(.*)$ in your configuration.

Correct location will look like (assuming the "dir" component is actually needed):

location ~ ^(/.*)?/cache/(.*)$ {
    ...
}

Note well that the same problem is present in the rewrite used in the location @resize.

Upvotes: 1

Related Questions