1

The only thing I do was

    mv test.php betterName.php 

no permission changes, nothing changed. Them rename in the NGINX configuration... FROM

    location  @rule {
            rewrite ^rule([a-z0-9]+)$
            /test.php?obj=$1            last;
    }

TO

    location  @rule {
            rewrite ^rule([a-z0-9]+)$
            /betterName.php?obj=$1            last;
    }

and service nginx restart.

It is magic: when I "undo" the rename process, mv betterName.php test.php and back old location, all working fine again.


Using fresh UBUNTU 16 LTS with fresh NGINX.


NOTES

The real-life conf-nginx-script that I am using seems,

server {
        server_name etc.etc;
        access_log /var/log/nginx/etc.etc.access_log;
        root /var/www/etc.etc/;
        index  index.php index.html index.htm;

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

        location ^~ /issn {
                try_files $uri @issnResolver;
        }

        location  @idResolver {
                rewrite ^/?([a-zA-Z0-9\-]+)/?$
                /index.php?obj=$1            last;
        }
        location  @issnResolver {
                rewrite ^/?issn[/:]?([xX0-9\-]+)[/:]?([a-zA-Z0-9]+)$
                /test.php?obj=$1&cmd=$2            last;
        }

        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
} #end server

... More real-life dumps for @RichardSmith comment:

ll /var/www/etc.etc/
drwxr-xr-x 7 www-data www-data  4096 Feb 15 09:41 ./
drwxr-xr-x 4 www-data www-data  4096 Feb  9 09:07 ../
-rw-rw-r-- 1 www-data www-data  5860 Feb 14 21:04 index.php
-rw-rw-r-- 1 www-data www-data   291 Feb 15 09:24 test.php

And real-life name is issn_resolver.php, so I do mv test.php issn_resolver.php.

5
  • Is that the only place where test.php is mentioned in your configuration files? Your regular expression as written will not match any URIs. Commented Feb 15, 2018 at 10:11
  • Hi @SebastianBrosch , I think I not understand what you say about N, I copy/paste the full conf, see edited question. Commented Feb 15, 2018 at 10:24
  • Thanks @RichardSmith, yes, the "only place" ... the other place that I can imagine is the default conf., there are no grep test. Commented Feb 15, 2018 at 10:27
  • What's the "better name"? I bet it starts with /issn... Commented Feb 15, 2018 at 11:52
  • Hi @RichardSmith, I edited for you see the ls dumps. Commented Feb 15, 2018 at 12:04

1 Answer 1

1

The URI /issn_resolver.php will match the location ^~ /issn block and not the location ~ \.php$ block as intended. Which results in the PHP script being downloaded rather than being executed.

The ^~ operator makes a prefix location take higher precedence that all of the regular expression location blocks.

If the ^~ operator is unnecessary - remove it - otherwise, find a name for the PHP script that does not begin with issn.

See this document for more.


To redirect all URIs that begin with issn to a given script, either:

1) Combine the two locations into one, and use a PHP script name that does not begin with issn:

location ^~ /issn {
    rewrite ^/?issn[/:]?([xX0-9\-]+)[/:]?([a-zA-Z0-9]+)$
    /test.php?obj=$1&cmd=$2            last;
}

Or:

2) Hardwire the name of the PHP file:

location ^~ /issn {
    rewrite ^/?issn[/:]?([xX0-9\-]+)[/:]?([a-zA-Z0-9]+)$
    /?obj=$1&cmd=$2            break;

    include snippets/fastcgi-php.conf;
    fastcgi_param  SCRIPT_FILENAME $document_root/test.php        
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

Notice the rewrite...break is used to capture the query string for the PHP script, but no longer required to name the script file. The script file is named by overriding the value of SCRIPT_FILENAME from that imported from the snippets file.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, now is making sense!
If I never use $uri as a file, how to express location to obtain same behaviour? (need only to redirect the string to the php).
I have added a couple of options.
Thanks a lot, perfect!
PS: I have problems on implement-and-test your suggestions, so there are a new quetion, now about NGINX-caching stackoverflow.com/q/48845250/287948

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.