E
E
Ext72019-10-13 22:03:13
Nginx
Ext7, 2019-10-13 22:03:13

Nginx - how to get rid of ERR_TOO_MANY_REDIRECTS?

I have been struggling with the issue for more than an hour, tried a bunch of everything, but still did not solve such a problem with the nginx config.
There is a rewrite rule that displays files in the browser without an extension. It works fine.
There is also a rule for one special page, on which the display of the argument is also rewritten from /page.php?param=arg to /page/arg And, apparently, this creates a problem: instead of showing the given page, the 404 error turns into circular redirect 301.
How to restore the normal operation of error_page 404, leaving the rule for a special page? There is a solution?
Here is the config itself:

server {
  listen 443 ssl http2;
  server_name example.com www.example.com;
  root /var/www/example.com/www;

  index index.php$is_args$args index.html index.htm;

  ssl_certificate /etc/nginx/ssl/self.crt;
  ssl_certificate_key /etc/nginx/ssl/self.key;

  location ~\.(php|html)$ {
    include fastcgi.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;

    try_files $uri $uri.php $uri/ @extensionless-php;
  }

  location / {
    try_files $uri $uri/ @extensionless-php;
    index index.php index.html index.htm;
  }

  location @extensionless-php {
    rewrite ^/(.*/)?index.(php|html|py)$ /$1 permanent;
    rewrite ^/(.*)\.(php|html|py) /$1 permanent;

    rewrite ^/(.*)/$ $1 permanent;		
    rewrite ^(.*)$ $1.php?$args;
  }

  location /page {
    rewrite ^/page/([^/]*)$ /page?param=$1;
  }

  error_page 404 /error;
  error_page 403 =404 /error;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-10-14
@Ext7

rewrite ^/(?:(.*)/|)index\.(?:php|htm) /$1 permanent;
rewrite ^/(.*)\.(php|htm) /$1 permanent;
rewrite ^/(.*)/$ /$1 permanent;

rewrite ^/page/([^/]*)$ /page.php?param=$1;
rewrite ^/$ /index.php;

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

location / {
    try_files $uri @extensionless-php;
}

location @extensionless-php {
    try_files $uri.php $uri/index.php =404;

    include fastcgi.conf;
    fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}

error_page 404 /error;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question