search
HomeOperation and MaintenanceNginxNginx basic function example analysis

Nginx basic function example analysis

May 11, 2023 pm 11:31 PM
nginx

1. Static http server

First of all, nginx is an http server that can display static files (such as html and pictures) on the server to the client through the http protocol. Configuration:

server {

  listen 80; # 端口号

  location / {

    root /usr/share/nginx/html; # 静态文件路径

  }

}

2. Reverse proxy server

What is a reverse proxy?

The client can directly access a website application server through the http protocol. The website administrator can add an nginx in the middle. The client requests nginx, nginx requests the application server, and then returns the result to the client. This nginx is a reverse proxy server.

Nginx basic function example analysis

Configuration:

server {

  listen 80;

  location / {

    proxy_pass http://192.168.20.1:8080; # 应用服务器http地址

  }

}

Since the server can be directly accessed via http, why should we add a reverse proxy in the middle? Isn’t it unnecessary? What does a reverse proxy do?

Continuing to look down, the following load balancing, virtual host, etc. are all based on reverse proxy. Of course, the functions of reverse proxy are not only these.

3. Load Balancing

When the website traffic is very large, the webmaster is happy to make money, but at the same time he is also in trouble. Because the website is getting slower and slower, one server is no longer enough.

So the same application is deployed on multiple servers, and requests from a large number of users are distributed to multiple machines for processing. At the same time, the benefit is that if one of the servers crashes, as long as other servers are running normally, it will not affect the user's use. nginx can achieve load balancing through reverse proxy.

Nginx basic function example analysis

Configuration:

upstream myapp {

  server 192.168.20.1:8080; # 应用服务器1

  server 192.168.20.2:8080; # 应用服务器2

}

server {

  listen 80;

  location / {

    proxy_pass http://myapp;

  }

}

The above configuration will allocate request polling to the application server, that is, multiple requests from a client may be processed by multiple processed by different servers. You can use ip-hash to assign requests to a fixed server for processing based on the hash value of the client's IP address.

Configuration:

upstream myapp {

  ip_hash; # 根据客户端ip地址hash值将请求分配给固定的一个服务器处理

  server 192.168.20.1:8080;

  server 192.168.20.2:8080;

}

server {

  listen 80;

  location / {

    proxy_pass http://myapp;

  }

}

In addition, the hardware configuration of the server may be good or bad. If you want to allocate most requests to good servers and a small number of requests to poor servers, you can use weight to control.

Configuration:

upstream myapp {

  server 192.168.20.1:8080 weight=3; # 该服务器处理3/4请求

  server 192.168.20.2:8080; # weight默认为1,该服务器处理1/4请求

}

server {

  listen 80;

  location / {

    proxy_pass http://myapp;

  }

}

4. Virtual host

Some websites have a large number of visits and require load balancing. However, not all websites are so excellent. Some websites need to save costs by deploying multiple websites on the same server because the number of visits is too small.

For example, if two websites www.aaa.com and www.bbb.com are deployed on the same server, the two domain names resolve to the same IP address, but the user can open both through the two domain names. Completely different websites do not affect each other, just like accessing two servers, so they are called two virtual hosts.

Configuration:

server {

  listen 80 default_server;

  server_name _;

  return 444; # 过滤其他域名的请求,返回444状态码

}

server {

  listen 80;

  server_name www.aaa.com; # www.aaa.com域名

  location / {

    proxy_pass http://localhost:8080; # 对应端口号8080

  }

}

server {

  listen 80;

  server_name www.bbb.com; # www.bbb.com域名

  location / {

    proxy_pass http://localhost:8081; # 对应端口号8081

  }

}

Open an application on servers 8080 and 8081 respectively. The client accesses through different domain names and can reverse proxy to the corresponding application server according to the server_name.

The principle of virtual host is realized by whether the host in the http request header matches the server_name. Interested students can study the http protocol.

In addition, the server_name configuration can also filter out someone who maliciously points certain domain names to your host server.

5, fastcgi

nginx itself does not support languages ​​​​such as php, but it can throw requests to certain languages ​​​​or frameworks through fastcgi (such as php, python , perl).

server {

  listen 80;

  location ~ \.php$ {

    include fastcgi_params;

    fastcgi_param script_filename /php文件路径$fastcgi_script_name; # php文件路径

    fastcgi_pass 127.0.0.1:9000; # php-fpm地址和端口号

    # 另一种方式:fastcgi_pass unix:/var/run/php5-fpm.sock;

  }

}

In the configuration, requests ending in .php are handed over to php-fpm for processing through fashcgi. php-fpm is a fastcgi manager of PHP. You can check other information about fashcgi, which will not be introduced in this article.

What is the difference between fastcgi_pass and proxy_pass? The following picture will help you understand:

Nginx basic function example analysis

The above is the detailed content of Nginx basic function example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
Using NGINX Unit: Deploying and Managing ApplicationsUsing NGINX Unit: Deploying and Managing ApplicationsApr 22, 2025 am 12:06 AM

NGINXUnit can be used to deploy and manage applications in multiple languages. 1) Install NGINXUnit. 2) Configure it to run different types of applications such as Python and PHP. 3) Use its dynamic configuration function for application management. Through these steps, you can efficiently deploy and manage applications and improve project efficiency.

NGINX vs. Apache: A Comparative Analysis of Web ServersNGINX vs. Apache: A Comparative Analysis of Web ServersApr 21, 2025 am 12:08 AM

NGINX is more suitable for handling high concurrent connections, while Apache is more suitable for scenarios where complex configurations and module extensions are required. 1.NGINX is known for its high performance and low resource consumption, and is suitable for high concurrency. 2.Apache is known for its stability and rich module extensions, which are suitable for complex configuration needs.

NGINX Unit's Advantages: Flexibility and PerformanceNGINX Unit's Advantages: Flexibility and PerformanceApr 20, 2025 am 12:07 AM

NGINXUnit improves application flexibility and performance with its dynamic configuration and high-performance architecture. 1. Dynamic configuration allows the application configuration to be adjusted without restarting the server. 2. High performance is reflected in event-driven and non-blocking architectures and multi-process models, and can efficiently handle concurrent connections and utilize multi-core CPUs.

NGINX vs. Apache: Performance, Scalability, and EfficiencyNGINX vs. Apache: Performance, Scalability, and EfficiencyApr 19, 2025 am 12:05 AM

NGINX and Apache are both powerful web servers, each with unique advantages and disadvantages in terms of performance, scalability and efficiency. 1) NGINX performs well when handling static content and reverse proxying, suitable for high concurrency scenarios. 2) Apache performs better when processing dynamic content and is suitable for projects that require rich module support. The selection of a server should be decided based on project requirements and scenarios.

The Ultimate Showdown: NGINX vs. ApacheThe Ultimate Showdown: NGINX vs. ApacheApr 18, 2025 am 12:02 AM

NGINX is suitable for handling high concurrent requests, while Apache is suitable for scenarios where complex configurations and functional extensions are required. 1.NGINX adopts an event-driven, non-blocking architecture, and is suitable for high concurrency environments. 2. Apache adopts process or thread model to provide a rich module ecosystem that is suitable for complex configuration needs.

NGINX in Action: Examples and Real-World ApplicationsNGINX in Action: Examples and Real-World ApplicationsApr 17, 2025 am 12:18 AM

NGINX can be used to improve website performance, security, and scalability. 1) As a reverse proxy and load balancer, NGINX can optimize back-end services and share traffic. 2) Through event-driven and asynchronous architecture, NGINX efficiently handles high concurrent connections. 3) Configuration files allow flexible definition of rules, such as static file service and load balancing. 4) Optimization suggestions include enabling Gzip compression, using cache and tuning the worker process.

NGINX Unit: Supporting Different Programming LanguagesNGINX Unit: Supporting Different Programming LanguagesApr 16, 2025 am 12:15 AM

NGINXUnit supports multiple programming languages ​​and is implemented through modular design. 1. Loading language module: Load the corresponding module according to the configuration file. 2. Application startup: Execute application code when the calling language runs. 3. Request processing: forward the request to the application instance. 4. Response return: Return the processed response to the client.

Choosing Between NGINX and Apache: The Right Fit for Your NeedsChoosing Between NGINX and Apache: The Right Fit for Your NeedsApr 15, 2025 am 12:04 AM

NGINX and Apache have their own advantages and disadvantages and are suitable for different scenarios. 1.NGINX is suitable for high concurrency and low resource consumption scenarios. 2. Apache is suitable for scenarios where complex configurations and rich modules are required. By comparing their core features, performance differences, and best practices, you can help you choose the server software that best suits your needs.

See all articles

Hot AI Tools

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.

Undress AI Tool

Undress AI Tool

Undress images for free

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

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.