Tigran
Tigran

Reputation: 653

Nginx rewrite or internal redirection cycle while internally redirecting

I am having troubles with nginx.

Here is my config.

server {
    listen                  80;
    listen                  [::]:80;
    server_name             www.test.local;
    return                  301 http://test.local$request_uri;
}
server {
    server_name             test.local;
    root                    /usr/share/nginx/test/htdocs/web;

    # error_log               /var/log/nginx/test.error.log;
    # access_log              /var/log/nginx/test.access.log;

    rewrite                 ^/app\.php/?(.*)$ /$1 permanent;

    location / {
        index app.php;
        try_files           $uri @rewriteapp;
    }

    location @rewriteapp {
        rewrite ^(.*)$      /app.php/$1 last;
    }

    location ~ ^/app\.php(/|$) {
        fastcgi_pass        172.17.0.1:48000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;

        include             fastcgi_params;
        fastcgi_param       SCRIPT_FILENAME  $realpath_root$fastcgi_script_name;
        fastcgi_param       DOCUMENT_ROOT $realpath_root;
        internal;
    }

    location /uploads/ {
        root                /usr/share/nginx/test/htdocs/web/uploads;
        try_files           $uri $uri/;
        access_log          off;
        expires             30d;
    }

    location /images/ {
        root                /usr/share/nginx/test/htdocs/web/images;
        try_files           $uri $uri/;
        access_log          off;
        expires             30d;
    }

    location /css/ {
        root                /usr/share/nginx/test/htdocs/web/css;
        try_files           $uri $uri/;
        access_log          off;
        expires             30d;
    }

    location /js/ {
        root                /usr/share/nginx/test/htdocs/web/js;
        try_files           $uri $uri/;
        access_log          off;
        expires             30d;
    }

    location /fonts/ {
        root                /usr/share/nginx/test/htdocs/web/fonts;
        try_files           $uri $uri/;
        access_log          off;
        expires             30d;
    }

    location = /favicon.ico {
        return              204;
        access_log          off;
        log_not_found       off;
    }
}

I want my locations as http://test.local/css/ link to /usr/share/nginx/test/htdocs/styles/ as i did it in my config.

But when im entering for example http://test.local/css/flow.css im getting next error:

2016/05/13 15:55:30 [error] 5#5: *64 rewrite or internal redirection cycle while internally redirecting to "/css/flow.css///////////", client: 192.168.99.1, server: test.local, request: "GET /css/flow.css HTTP/1.1", host: "test.local", referrer: "http://test.local/"

Whats the problem here?

Upvotes: 3

Views: 7873

Answers (1)

Richard Smith
Richard Smith

Reputation: 49842

You have two problems with your configuration.

The first problem is that your root directives are wrong, so the file is not found.

The second problem is that the default action is to add another / to the end of the URI.

You need to add a valid default action to your try_files directive, such as a 404 response:

try_files $uri $uri/ =404;

So, back to the first problem. Your configuration states that /css/example.css is located at /usr/share/nginx/test/htdocs/web/css/css/example.css. Notice that /css/ is replicated. Same goes for /images/, /js/ and /fonts/. See this document for details of the root directive.

In most configurations, it is sufficient to specify the root at the server block level and allow it to be inherited by almost all location blocks, rather than repeating it in each location.

Your question states that /css/ files should be found in /usr/share/nginx/test/htdocs/styles/. This requires an alias directive. See this document for details of the alias directive. For example:

location /css/ {
    alias       /usr/share/nginx/test/htdocs/styles/;
    access_log  off;
    expires     30d;
}

Upvotes: 4

Related Questions