Nginx is a lightweight Web/reverse proxy server and email (IMAP/POP3) proxy server. It was developed by Igor Sysoev, a Russian programmer, for use by Rambler, a large Russian portal and search engine. It is characterized by less memory and strong concurrency.
Nginx is a powerful and high performance Web and reverse proxy server. It has many excellent features. In the case of high connection concurrency, Nginx is a good alternative to Apache server. Capable of supporting responses up to 50,000 concurrent connections.
In some large projects, Nginx is often used as a load balancer. Nginx distributes requests to the distributed Web Server according to certain rules, which can solve the problem that the Web Server is the bottleneck of project performance. Thus, LNMPA architecture is formed. Linux+Nginx+Mysql+PHP+Apache, LVS has the same function, but each has advantages and disadvantages, the best is F5 hardware, but the price is very expensive;
Nginx is also well suited to handling static pages, file uploads and downloads on servers that no other server software can match.
In fact, there are many projects directly using Nginx as a Web server,Nginx as a Web server in the processing of PHP business logic can not be as powerful as Apache, if the use of Nginx alone does not meet the needs of your project, then separate the business,Nginx load balancing, processing static pages, responsible for file upload and download, PHP business logic is handed over to Apache.
Reverse proxy and load balancing:
Web Server1: 172.22.28.237:80 Web Server2: 172.22.28.235:80 Web Server3: 172.22.28.234:80 Domain name: test.box.com Nginx Server: 47.252.83.23:80Copy the code
1. The Nginx configuration
user www; Run user worker_processes 1; {worker_connections 1024; } http { include mime.types; default_type application/octet-stream; charset utf-8; sendfile on; keepalive_timeout 65; server{ listen 80; Server_name test1.box.com; root /srv/box/site1; # location / {index index.php index.html index.htm; } location ~ \.php${fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 80; server_name test2.box.com root /srv/box/site2; # location / {index index.php index.html index.htm; } location ~ \.php${fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } server { listen 80; server_name test3.box.com root /srv/box/site3; # location / {index index.php index.html index.htm; } location ~ \.php${fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }}}Copy the code
2. Create directories site1 and site2 respectively in the web directory. Create index. PHP files in each directory.
site1 <? php echo json_encode(['type'=>'200','data'=>'test']); ? > site2 <? php echo json_encode(['type'=>'200','data'=>'test2']); ? > site3 <? php echo json_encode(['type'=>'200','data'=>'test3']); ? >Copy the code
Give two directory permissions to the WWW group WWW user:
chown -R www:www site1/
chown -R www:www site2/
chown -R www:www site3/
3 Reverse proxy and Load Balancing configuration for Nginx server:
Upstream test.box.com {server 172.22.28.237 weight=5; Server 172.22.28.235 weight = 5; Server 172.22.28.234 weight = 5; } # reverse proxy configuration server {listen 80; server_name test.box.com; charset utf-8; location /{ proxy_pass http://test.box.com; }}Copy the code
4. Restart the Nginx service
5. Visit test.box.com in the browser and visit it several times so that we can see that the content changes according to the weight.
At this point, Nginx load balancing and reverse proxy configurations are complete.
Nginx load balancing can be distributed in four ways:
1. Polling. This mode is adopted by default. Nginx will poll and distribute the Web Server according to the order of request time.
2. Weight refers to the probability of polling. The larger the value is, the more likely it is to be distributed.
3. Ip_hash: Each request is allocated according to the hash result of the access IP address. In this way, each visitor can access the same back-end server and solve the session sharing problem.
Upstream test.box.com {server 172.22.28.237 down; 4. Server 172.22.28.235 weight = 2; Server 172.22.28.236; Server 172.22.28.234 backup; }
Configuration details:
Down indicates that the current Web Server does not participate in the load weight. The default value is 1. The larger the weight is, the larger the weight is. Backup: all other non-backup Server down or busy, request backup machine. So this machine will have the least pressure.
Backup is to use the hot backup function of Nginx, which is also one of the most important benefits of the most typical application. When the non-backup Server can provide services for the Client, the Backup Server does not provide services to the outside world, and the Backup Server is in a cold state. When all non-backup servers fail to provide services for clients, the Backup Server provides services for clients to achieve hot backup. The failure of one or all non-Backup Web Servers does not affect the access function of the entire Web project. The Web project can still provide services for clients.
Nginx does load balancing and has no special requirements on the operating system and language environment of the Web Server. The Operating system of the Web Server can be Linux or Windows Server, and the Web program can be Java, PHP,.net, etc.
I hope the above content can help you. Many PHPer will encounter some problems and bottlenecks when they are advanced, and they have no sense of direction when writing too many business codes. I have sorted out some information, including but not limited to: Distributed architecture, high scalability, high performance, high concurrency, server performance tuning, TP6, Laravel, Redis, Swoft, Kafka, Mysql optimization, shell scripting, Docker, microservices, Nginx, etc. Many knowledge points can be free to share with you