简体   繁体   中英

Nginx location block matching rule

I'm new to nginx server.
I'm gonna deploy the php framework such as codeigniter to the nginx server.
My config file is following.

server {
    index index.html index.php index.htm;

    # set expiration of assets to MAX for caching
    location ~* \.(ico|css|js|gif|jpe?g|png)(\?[0-9]+)?$ {  
        expires max;
        log_not_found off;
    }

    location / {  
        # Check if a file exists, or route it to index.php.
        try_files $uri $uri/ /index.php;
    }

    location ~* \.php$ {  
        fastcgi_pass unix:/var/run/php/php7-fpm.sock;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }
 }

My question is following.
1)The request uri is like this "www.domain.com/controllername/functionname/param1/param2/"
How does nginx work with this url?

2)The third location block matches the regular expression ".php$".
Is this true only if the uri has ended with ".php"?
(I think so , but that block's fastcgi_split_path_info has different regular expression.)

Question 1) Yes this should work, because the line

try_files $uri $uri/ /index.php;

is handled one by one for a request. First nginx tries to find the file described by the URI, if there's no match, it checks if it is a directory. If not it calls your index.php file. The original URI is handed over with a lot of other HTTP_REQUEST variables and the code from codeigniter takes care of parsing the url, if you config (codeigniter is correct). The call convention for codeigniter is "www.domain.com/controllername/public_function/param1/param2/" So normally you don't give the viewname, but the controller and the function name in your URI.

Question 2) The "location" directive only uses the URI path without any GET parameters. The split_path works differen and so it need a different regexp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM