Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the Nginx+PHP+FastCGI acceleration Mode

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to realize the Nginx+PHP+FastCGI acceleration mode". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

I. the process of users visiting dynamic PHP pages

The user's browser initiates access to the web page: http://192.168.1.103/index.php

The user and the nginx server make a three-way handshake for TCP connection (ignoring access control policies including nginx access control policy, nginx firewall and other access control policies)

Step 1: the user sends the http request to the nginx server

Step 2: nginx judges the request based on the URI and suffix accessed by the user

1. For example, the index.php,nginx accessed by the user is matched according to the location in the configuration file, for example:

[email protected]: / data/web# cat / etc/nginx/conf.d/blog.conf server {root / data/web/blog/; index index.html index.htm; server_name www.fwait.com; location / {try_files $uri $uri/ / index.html;} location / blog/ {# alias / usr/share/doc/; auth_basic "authorized users only" Auth_basic_user_file / etc/nginx/passwd.conf; # autoindex on; allow 192.168.1.103; deny all;} location ~\ .php$ {include / etc/nginx/fastcgi_params; fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1 autoindex on; allow 9000;}}

If the user accesses index.php, it will match to location ~\ .php $, which means to match the size of the resources accessed by users through URI, and the accessed resources end in .php.

After nginx matches the resources requested by the user to a specific location, it executes the action corresponding to the location. The meaning of the action in location is:

Include / etc/nginx/fastcgi_params; # means that nginx will call the fastcgi interface

Fastcgi_intercept_errors on; # means to enable the recording of interrupts and error messages in fastcgi

Fastcgi_pass 127.0.0.1 nginx 9000; # means that nginx sends the resources requested by users to 127.0.0.1 nginx 9000 through fastcgi_pass for parsing. Here, the nginx script parsing server and the php script parsing server are on the same machine, so 127.0.1 nginx 9000 represents the local php script parsing server.

According to the configuration of the nginx server, we can see that the user accesses dynamic php resources, and nginx will call the php-related script parser to parse the resources accessed by the user.

Step 3: through the second step, we can see that the user requests dynamic content, and nginx will send the request to the fastcgi client, and send the user's request to php-fpm through fastcgi_pass

If the user accesses a static resource, it is simple. Nginx returns the static resource requested by the user directly to the user.

Step 4: after fastcgi_pass gives the dynamic resources to php-fpm, php-fpm transfers the resources to the wrapper of the php script parsing server

Step 5: after wrapper receives the request from php-fpm, wrapper will generate a new thread to call the php dynamic program parsing server

If the user requests that the database be read, such as MySQL database, the database read operation will be triggered.

If the user requests such as pictures / attachments, PHP will trigger a query to the back-end storage server such as the storage cluster stored through NFS

Step 6: php will return the query results to nginx

Step 7: nginx constructs a response message to return the result to the user

This is only one kind of nginx. The result of the user request and the return user request are carried out asynchronously, that is, the resources requested by the user are transferred in nginx, and the nginx can be synchronized, that is, for the parsed resources, the server returns the resources directly to the user without having to make a transfer in the nginx.

Second, related questions

1. Is it necessary to trigger a complete dynamic resource resolution process for each user's request for dynamic resources?

No, there are two ways to solve this problem:

First, enable the caching function of nginx itself to cache the results of dynamic resource parsing. The next time the user accesses the corresponding resource, nginx performs the cache query. If the query is successful, the static resource after the dynamic resource is parsed is returned to the user.

Second, deploy a cache machine on the nginx backend, such as deploying a varnish cache cluster to cache resources. The resources requested by users can be found on the cache cluster first.

two。 Is it feasible to use nginx for caching? Depending on the actual situation, if in the entire web architecture, nginx is not a bottleneck, nginx can be used for caching, but it is not recommended to do so, because nginx is the only way for users to request and respond to user requests. If there is a bottleneck in nginx, other backend performance such as storage cluster is useless, so in actual deployment, it is not recommended to enable nginx caching (if nginx is used as http server). Because enabling nginx caching will not only degrade nginx performance, but also consume the hardware resources of the corresponding server deploying nginx.

3. If you use a graph to show the relationship between nginx fastcgi wrapper php

What exactly is 4.fastcgi?

CGI full name Universal Gateway Interface Commmon Gateway Interface

A tool for program service communication on HTTP services. CGI programs must be run on a network server.

The performance of the traditional CGI interface is poor, because every time the HTTP server encounters a dynamic program, it needs to restart the parser to perform parsing, and then the result is returned to the HTTP server. This is almost impossible when dealing with high concurrency, so FastCGI is born. In addition, the security of traditional CGI interface is also very poor.

A retractable place. Interface for high-speed communication between HTTP servers and dynamic scripting languages

The interface is socket under linux (this socket can be file socket or ip socket)

The main advantages separate the dynamic language from the HTTP server. Most popular HTTP servers support FsatCGI, including Apache/Nginx/lighttpd.

The popular supporting language is PHP, and the interface adopts the Cmax S architecture, which can separate the HTTP server from the script parser, and start one or more script parsing daemons on the script parsing server.

Every time the HTTP server encounters a dynamic program, it can deliver it directly to the FastCGI process for execution, and then return the results to the browser. This approach allows the HTTP server to handle static requests exclusively or return the results of the dynamic script server to the client, which greatly improves the performance of the entire application system.

5. Specific nginx-related configurations of nginx + php

[email protected]: / data/web# cat / etc/nginx/nginx.conf | egrep-v "# | ^ $" user www-data; worker_processes 4; pid / var/run/nginx.pid; events {worker_connections 768;} http {sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include / etc/nginx/mime.types; default_type application/octet-stream Access_log / var/log/nginx/access.log; error_log / var/log/nginx/error.log; gzip on; gzip_disable "msie6"; include / etc/nginx/conf.d/*.conf; include / etc/nginx/sites-enabled/*;} [email protected]: / data/web#

[email protected]: / data/web# cat / etc/nginx/conf.d/blog.conf server {root / data/web/blog/; index index.html index.htm; server_name www.fwait.com; location / {try_files $uri $uri/ / index.html;} location / blog/ {# alias / usr/share/doc/; auth_basic "authorized users only" Auth_basic_user_file / etc/nginx/passwd.conf; # autoindex on; allow 192.168.1.103; deny all;} location ~\ .php$ {# include / usr/local/etc/nginx/fastcgi.conf; include / etc/nginx/fastcgi_params; fastcgi_intercept_errors on; fastcgi_pass 127.0.0.1 autoindex on; allow 9000 }} [email protected]: / data/web#

This is the end of the content of "how to implement Nginx+PHP+FastCGI acceleration mode". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report