search
HomeBackend DevelopmentPHP TutorialPHP uses Nginx to implement reverse proxy

PHP uses Nginx to implement reverse proxy

Apr 09, 2018 pm 05:33 PM
nginxphpacting

This article mainly introduces in detail the method of using Nginx to implement reverse proxy in PHP. It has certain reference value. Friends in need can refer to it

1. Proxy server

1. What is a proxy server?

#Proxy server, when the client sends a request, it will not directly send it to the destination host, but It is first sent to the proxy server. After the proxy service accepts the client's request, it sends it to the host, receives the data returned by the destination host, stores it in the hard disk of the proxy server, and then sends it to the client.


2. Why use a proxy server

1) Improve access speed

Since the data returned by the target host will be stored in the hard disk of the proxy server, the next time the customer accesses the same site data, it will be read directly from the hard disk of the proxy server, which plays a cache role, especially for Popular sites can significantly increase request speeds.

2) Firewall function

Since all client requests must access remote sites through the proxy server, restrictions can be set on the proxy server to filter certain unsafe information.

3) Access inaccessible target sites through proxy servers

There are many developed proxy servers on the Internet. When access is restricted, the client can access the target through unrestricted proxy servers. Site, in layman's terms, the circumvention browser we use uses a proxy server. Although it cannot go abroad, it can also directly access the external network.

2. Reverse proxy VS forward proxy

1. What is a forward proxy? What is a reverse proxy?

Forward proxy is set up between the client and the target host. It is only used to proxy the connection request from the internal network to the Internet. The client must specify the proxy server and send the request directly to the Web server. HTTP requests are sent to the proxy server.


The reverse proxy server is set up on the server side. It relieves the workload of the server by buffering frequently requested pages and forwards client requests to the internal network. The target server; and returns the results obtained from the server to the client requesting a connection on the Internet. At this time, the proxy server and the target host appear as a server to the outside world.


#2. What are the main applications of reverse proxy?

Many large web sites now use reverse proxies. In addition to preventing vicious attacks from the external network on intranet servers, caching to reduce server pressure and access security control, it can also perform load balancing and distribute user requests to multiple servers.

3. Directional proxy server Nginx

Nginx, as a popular reverse proxy server in recent years, is installed on the destination host and is mainly used to forward client requests. There are multiple HTTP servers in the background to provide services. The function of nginx is to forward the request to the subsequent server and decide which target host to handle the current request. The following demonstrates how to configure Nginx to function.

1. Simulate n http servers as target hosts

For testing, simply use 2 tomcat instances to simulate two http servers, and change the tomcat ports to 8081 and 8082 respectively

2. Configure IP domain name

192.168.72.49 8081.max.com

192.168.72.49 8082.max.com

3. Configure nginx.conf

upstream tomcatserver1 {   
server 192.168.72.49:8081;   

 } 

upstream tomcatserver2 {    
server 192.168.72.49:8082;   

} 

 server {    

     listen    80;      

    server_name 8081.max.com;      
    #charset koi8-r;      
    #access_log logs/host.access.log main;      
    location / {       
     proxy_pass  http://tomcatserver1;       

     index index.html index.htm;     

 }      

} 

server {     

      listen    80;     
     server_name 8082.max.com;      
      #charset koi8-r;      
      #access_log logs/host.access.log main;      
     location / {       
     proxy_pass  http://tomcatserver2;       
     index index.html index.htm;     

  }       

 }

Process:

1) Access 8081.max.com with the browser and find the 192.168.72.49 server through local host file domain name resolution (Installing nginx)

2) The nginx reverse proxy accepts the client request, finds the server node with server_name 8081.max.com, and forwards the request to upstream tomcatserver1 according to the http path corresponding to proxy_pass, which is the port Tomcat server number 8081.

4. Effect display

Request 8081.max.com, tomcat1 receives and returns to the home page

Request 8082.max.com, tomcat2 Receive and return to the homepage


## IV. Summary

Through analysis, it is not difficult to conclude that, taking Baidu as an example, if the customer If the IP of the computer and the IP of Baidu server (target host) are in the same network segment, it will be like sending a request within the LAN, and the speed is extremely fast.

But if this demand cannot be met and you still want to achieve a better request response, Baidu server can provide a public network IP in the same network segment as the target server, which is the IP of the reverse proxy service, through The proxy server forwards the client request and determines which of the N servers behind the scenes will handle the request. Since the reverse proxy server is on the same network segment as the target host, the access speed will be very fast.

When Nginx is used as a reverse proxy server, it is one of many reverse proxy servers. Through simple configuration, the client request can be forwarded to the designated server for processing by specifying the server IP or domain name address. ask.


The above is the detailed content of PHP uses Nginx to implement reverse proxy. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
A Comprehensive CI/CD Pipeline Strategy for Modern PHP DevelopmentA Comprehensive CI/CD Pipeline Strategy for Modern PHP DevelopmentJul 24, 2025 am 11:38 AM

ArobustCI/CDpipelineisessentialformodernPHPdevelopmenttoensurereliability,reduceerrors,andacceleratedelivery.1.Structureyourrepositorywithsemanticversioning,properbranching(e.g.,GitHubFlow),andincludecomposer.json,tests/,linterconfigs,anddocker-compo

Laravel routing parameter delivery and controller method definition: Avoiding common errors and best practicesLaravel routing parameter delivery and controller method definition: Avoiding common errors and best practicesJul 23, 2025 pm 07:27 PM

This tutorial details the correct method of parameter passing in Laravel routing, and corrects common errors in writing parameter placeholders into controller method names. The article provides examples of standardized routing definitions and controller methods, and emphasizes that deletion operations should prioritize the use of HTTPDELETE methods to enhance routing semantics and maintainability.

Guide to matching Laravel routing parameter passing and controller methodGuide to matching Laravel routing parameter passing and controller methodJul 23, 2025 pm 07:24 PM

This article aims to resolve common errors in the Laravel framework where routing parameter passing matches controller methods. We will explain in detail why writing parameters directly to the controller method name in the routing definition will result in an error of "the method does not exist", and provide the correct routing definition syntax to ensure that the controller can correctly receive and process routing parameters. In addition, the article will explore best practices for using HTTPDELETE methods in deletion operations.

How to use PHP to develop a Q&A community platform Detailed explanation of PHP interactive community monetization modelHow to use PHP to develop a Q&A community platform Detailed explanation of PHP interactive community monetization modelJul 23, 2025 pm 07:21 PM

1. The first choice for the Laravel MySQL Vue/React combination in the PHP development question and answer community is the first choice for Laravel MySQL Vue/React combination, due to its maturity in the ecosystem and high development efficiency; 2. High performance requires dependence on cache (Redis), database optimization, CDN and asynchronous queues; 3. Security must be done with input filtering, CSRF protection, HTTPS, password encryption and permission control; 4. Money optional advertising, member subscription, rewards, commissions, knowledge payment and other models, the core is to match community tone and user needs.

Efficiently use JSON data to implement cascading drop-down menus in Laravel Blade templatesEfficiently use JSON data to implement cascading drop-down menus in Laravel Blade templatesJul 23, 2025 pm 07:18 PM

This article details how to load a local JSON file in a Laravel application and pass its data to a Blade template. By processing JSON parsing by the controller, the view layer uses Blade's @foreach instruction to traverse the data, thereby realizing dynamically generating drop-down menus. In particular, the article also explores in-depth how to combine JavaScript to implement multi-level linkage drop-down menu functions to provide users with dynamic content display based on selection, and provides practical code examples and precautions for implementing such interactions.

Deep analysis of matching Laravel routing parameter transfer and controller methodDeep analysis of matching Laravel routing parameter transfer and controller methodJul 23, 2025 pm 07:15 PM

This article deeply explores the correct transmission of routing parameters and the matching mechanism of controller methods in the Laravel framework. In response to the common "method does not exist" error caused by writing routing parameters directly to the controller method name, the article elaborates on the correct way to define routing, that is, declare parameters in the URI and receive them as independent parameters in the controller method. At the same time, the article also provides code examples and suggestions on best practices for HTTP methods, aiming to help developers build more robust and RESTful Laravel applications.

PHP integrated AI intelligent image processing PHP image beautification and automatic editingPHP integrated AI intelligent image processing PHP image beautification and automatic editingJul 23, 2025 pm 07:12 PM

PHP integrated AI image processing requires the help of a third-party API or local model, which cannot be directly implemented; 2. Use ready-made services such as Google CloudVision API to quickly realize facial recognition, object detection and other functions. The advantages are fast development and strong functions. The disadvantages are that they need to pay, rely on the network and have data security risks; 3. Deploy local AI models through PHP image library such as Imagick or GD combined with TensorFlowLite or ONNXRuntime. It can be customized, the data is safer, and the cost is low, but the development is difficult and requires AI knowledge; 4. Mixed solutions can combine the advantages of API and local model, such as using API for detection and beautification of local models; 5. Selecting AI image processing API should be comprehensive

Twilio Voice Call Maintenance and Recovery: Meeting Functions and Independent Call Leg Management PracticeTwilio Voice Call Maintenance and Recovery: Meeting Functions and Independent Call Leg Management PracticeJul 23, 2025 pm 07:09 PM

This article discusses in-depth two main strategies for realizing voice call holding (Hold) and recovery (Unhold) on the Twilio platform. First, we introduce the detailed introduction to leveraging the Twilio Conference feature to easily manage call retention by updating the Participant resources, and provide corresponding code examples. Second, for scenarios where more detailed control of independent call legs (CallLeg) is required, how to combine TwiML instructions (such as and/) to handle call reconnection, while highlighting the complexity of this approach. The article aims to provide professional and practical guidance to help developers choose the most suitable implementation solution according to specific needs.

See all articles

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment