Nginx configuration of ThinkPHP

Time:2020-9-1

Nginx configuration of ThinkPHP

Today, there are some problems when using a rapid development framework based on TP

Screenshot of nginx error report

Nginx configuration of ThinkPHP

Manual line wrap for illustration purposes

//Override or internal redirection loop when processing
2019/11/11 11:16:06 [error] 15164#15432: *1 rewrite or internal redirection cycle while processing 
    "/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index.php/index/user/index.html", 
    client: 127.0.0.1, 
    server: xxxxx, 
    request: "GET /index/user/index.html HTTP/1.1", 
    host: "xxxxx", 
    referrer: "xxxxx"

Wrong configuration

Refer to larvael configuration

server {
    .
    .
    .

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    .
    .
    .
    location ~ \.php$ {
        fastcgi_pass127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
    .
    .
    .

}

Found that all paths are the same, are home page effect

It is preliminarily judged that there is a problem with the rewriting rules of nginx

#Path/Go through this match after the beginning/index /index/user 
location / {#$URI return if you have it locally, or $URI/If you have a local directory, you can go back to it, or you can rewrite it later=Index refers to the index file or index under the root directory/catalog/ /index.php?$query_string;
}

Start reporting errors

solve the problem

After online query, the configuration of TP5 should be

    location / {
        try_files $uri $uri/ /index.php$uri?$query_string;
    }

After modification, it is found that the problem has not been solved

    # location ~ \.Change PHP $to location~ \.php(.*)$
    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include fastcgi_params;
    }

Solution, complete configuration

server {
    listen       80;
    server_name  xxxxxxx ;
    root  www;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    index index.html index.htm index.php;
    location / {
        try_files $uri $uri/ /index.php$uri?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;


    location ~ \.php(.*)$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_split_path_info  ^((?U).+\.php)(/?.+)$;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO  $fastcgi_path_info;
        fastcgi_param  PATH_TRANSLATED  $document_root$fastcgi_path_info;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Some explanations

For pseudo static configurationtry_filesInstead ofrewriteBecauserewriteAll matching (here /) requests will be matched once, such as:host.com/a.pngThis will waste some resources (computing power of the server, etc.); also refer tolaravelAdd some security settings to the official configuration of.

If you use online projects, it is best to add static resource cache configuration.

    location ~* ^.+\.(css|js|ico|gif|jpg|jpeg|png)$ {
        expires 30d;
    }

repair

Discovery on August 20, 2020getThere is no parameter, because there is no parameterQUERY_STRINGEmpty, solve:

    location / {#Bring query_ string/ /index.php$uri?$query_string;
    }

tp
nginx
thinkphp

Recommended Today

kpf4 gui.exe What is the process and what is it

Process file: kpf4gui or kpf4 gui.exeProcess name: Kerio personal firewall componentProcess category: processes with security risks English Description:kpf4gui.exe is a process that belongs to the Kerio Personal Firewall. This process should not be removed to ensure that your system security is not breached. Chinese reference:kpf4 gui.exe It is a process related to Kerio personal firewall.Produced […]