Home Operation and Maintenance Nginx Nginx 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; # 静态文件路径

  }

}
Copy after login

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地址

  }

}
Copy after login

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;

  }

}
Copy after login

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;

  }

}
Copy after login

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;

  }

}
Copy after login

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

  }

}
Copy after login

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;

  }

}
Copy after login

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 of this Website
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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to configure cloud server domain name in nginx How to configure cloud server domain name in nginx Apr 14, 2025 pm 12:18 PM

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

How to check nginx version How to check nginx version Apr 14, 2025 am 11:57 AM

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to start nginx server How to start nginx server Apr 14, 2025 pm 12:27 PM

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to configure nginx in Windows How to configure nginx in Windows Apr 14, 2025 pm 12:57 PM

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

How to check whether nginx is started How to check whether nginx is started Apr 14, 2025 pm 01:03 PM

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to run nginx apache How to run nginx apache Apr 14, 2025 pm 12:33 PM

To get Nginx to run Apache, you need to: 1. Install Nginx and Apache; 2. Configure the Nginx agent; 3. Start Nginx and Apache; 4. Test the configuration to ensure that you can see Apache content after accessing the domain name. In addition, you need to pay attention to other matters such as port number matching, virtual host configuration, and SSL/TLS settings.

See all articles