About THINKPHP's NGINX configuration, it's only a pit after those years

THINKPHP NGINX configuration stepping pit

Today, I encountered some problems when using a tp based rapid development framework:

Screenshot of nginx error reporting

For the convenience of explanation, carry out manual line wrap

// Override or internal redirect loop while 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;
    }
    .
    .
    .

}

It is found that all paths are the same, which is the home page effect

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

# Follow this match after the path / start
# For example, / index / user 
location / {
    # $uri will be returned if there is a local directory, or $uri / will be returned if there is a local directory, or rewrite later
    # Local refers to the root directory of the website. For example, when $uri=index, there is an index file or index / directory under the root directory
    try_files $uri $uri/ /index.php?$query_string;
}

Start reporting errors

solve the problem

The configuration of tp5 after online query should be

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

After modification, it is found that the problem has not been solved; Comparison configuration discovery

  # 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 notes

Try in pseudo static configuration_ Files instead of rewrite, because rewrite will match all matching (here /) requests once, such as host COM / a.png, which will waste some resources (computing power of the server, etc.); It also refers to the official configuration of laravel and adds some security settings.

If online projects are used, it is best to add static resource cache configuration.

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

repair

On August 20, 2020, it was found that get had no parameters because there was no QUERY_STRING is null, solution:

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

Pay attention and don't get lost

Well, ladies and gentlemen, the above is the whole content of this article. The people who can see here are talents. As I said before, there are many technical points in PHP. It's also because there are too many. You can't write it and you won't read it too much. So I've sorted it into PDF and documents here. If necessary, you can

Click to enter the code: PHP + "platform"

More learning content can be accessed [benchmarking factory] complete catalogue of high-quality PHP architect tutorials. As long as you can read it, you can ensure that the salary will rise to a higher level (continuously updated)

I hope to help you with the above contents. Many PHPer will encounter some problems and bottlenecks when upgrading. There is no sense of direction when writing too much business code. I don't know where to start to improve. For this, I have sorted out some materials, including but not limited to: distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, laravel, YII2, Redis, Swoole, Swoft, Kafka, Mysql optimization, shell script Docker, micro services, Nginx and other knowledge points. Advanced dry goods can be shared with you free of charge, and you can join me if you need PHP Technology Exchange Group

Tags: PHP

Posted by shawjames on Sat, 07 May 2022 21:34:51 +0300