[Turn] Nginx Basic Functions Quick Start

Original link: Nginx Basic Function Quick Start | Fork Brother’s Blog

This article mainly introduces some of the most basic functions and simple configuration of Nginx, but does not include the installation, deployment and implementation principles of Nginx. Not much nonsense, let’s get started.

Static HTTP server

First of all, Nginx is an HTTP server, which can display static files (such as HTML, pictures) on the server to the client through the HTTP protocol.

Configuration:

server {
     listen  80 ; # port number 
    location / {
         root /usr/share/nginx/html; # static file path
    }
}

reverse proxy server

What is a reverse proxy?

The client can directly access the application server of a website through the HTTP protocol. If the website administrator adds an Nginx in the middle, the client requests Nginx, Nginx requests the application server, and then returns the result to the client. At this time, Nginx is a reverse proxy server.

Configuration:

server {
     listen  80 ;
     location / {
         proxy_pass http://192.168.20.1:8080; # Application server HTTP address
    }
}

Since the server can directly access HTTP, why add a reverse proxy in the middle, isn’t it superfluous? What does a reverse proxy do? Continuing to look down, the following load balancing and virtual hosts are all implemented based on reverse proxy. Of course, the function of reverse proxy is not only these.

load balancing

When the website traffic is very large, and the webmaster is happy to make money, it is also a matter of time. Because websites are getting slower and slower, one server is no longer enough. Therefore, the same application is deployed on multiple servers, and the requests of a large number of users are distributed to multiple machines for processing. At the same time, the advantage is that if one of the servers hangs, as long as there are other servers running normally, it will not affect the use of users.

Nginx can achieve load balancing through reverse proxy.

Configuration:

upstream myapp {
     server  192.168.20.1:8080 ; # application server 1 
    server  192.168.20.2:8080 ; # application server 2
}
server {
    listen 80;
    location / {
        proxy_pass http://myapp;
    }
}

web hosting

The website has a lot of traffic and needs load balancing. However, not all websites are so good, and some websites need to save costs due to the small number of visits, and deploy multiple websites on the same server.

For example www.aaa.com, if www.bbb.comtwo websites are deployed on the same server, the two domain names resolve to the same IP address, but users can open two completely different websites through the two domain names without affecting each other, just like accessing two servers. , so called two virtual hosts.

Configuration:

server {
     listen  80 default_server;
     server_name _;
     return  444 ; # Filter requests from other domain names and return 444 status code
}
server {
     listen  80 ;
     server_name www.aaa.com; # www.aaa.com domain name 
    location / {
         proxy_pass http://localhost:8080; # Corresponding to port number 8080
    }
}
server {
     listen  80 ;
     server_name www.bbb.com; # www.bbb.com domain name 
    location / {
         proxy_pass http://localhost:8081; # Corresponding to port number 8081
    }
}

An application is opened on the server 8080and 8081the port respectively. The client accesses through different domain names, and server_namecan reverse proxy to the corresponding application server according to it.

The principle of virtual host is realized by Hostwhether the HTTP request header matches server_name. Interested students can study the HTTP protocol.

In addition, the server_nameconfiguration can also filter someone maliciously pointing certain domains to your hosting server.

FastCGI

Nginx itself does not support languages ​​such as PHP, but it can throw requests to certain languages ​​or frameworks (such as PHP, Python, Perl) through FastCGI.

server {
     listen  80 ;
     location  ~ \.php$ {
         include fastcgi_params;
         fastcgi_param SCRIPT_FILENAME /PHP file path $fastcgi_script_name ; # PHP file path 
        fastcgi_pass  127.0.0.1:9000 ; # PHP-FPM address and port number 
        # Another way: fastcgi_pass unix:/var/run/php5-fpm.sock;
    }
}

In the configuration, the .phpfinal request is handed over to PHP-FPM through FashCGI for processing. PHP-FPM is a FastCGI manager of PHP. Other information about FashCGI can be consulted, which will not be introduced in this article.

fastcgi_passand proxy_passwhat is the difference? The following picture will show you:

Reprinted in: /esofar/p/8580034.html

Leave a Comment

Your email address will not be published. Required fields are marked *