Quick Start to some basic functions of Nginx and quick start to basic functions of nginx

Source: Internet
Author: User

Quick Start to some basic functions of Nginx and quick start to basic functions of nginx

This article mainly introduces some basic functions and simple configurations of Nginx, but does not include the installation, deployment, and implementation principles of Nginx. Not much nonsense. Start directly.

1. Static HTTP Server

Nginx is an HTTP server that displays static files (such as HTML and images) on the server to the client over HTTP.

Configuration:

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

2. Reverse Proxy Server

What is 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, and the client requests Nginx and Nginx to request the application server, and then return the result to the client, nginx is the reverse proxy server.


Configuration:

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

Since the server can directly access HTTP, why do I need to add a reverse proxy in the middle? What is the role of reverse proxy? Further, the following Server Load balancer and virtual hosts are all implemented based on reverse proxy. Of course, the reverse proxy function is more than just that.

3. Server Load balancer

When the website traffic is very large and the website webmaster is happy to make money, it will be shared. Because the website is getting slower and slower, one server is not enough. Therefore, the same application is deployed on multiple servers, and a large number of user requests are distributed to multiple machines for processing. At the same time, the benefit is that if one of the servers fails, as long as there are other servers running normally, the user will not be affected.

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 ;}}
The preceding configuration will distribute requests to the application server through polling, that is, multiple requests from a client may be processed by multiple different servers. You can use ip-hash to allocate requests to a fixed Server Based on the hash value of the Client ip address.

Configuration:

Upstream myapp {ip_hash; # assign the request to a fixed server Based on the Hash value of the Client IP address to handle 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 poor. If you want to allocate most of the requests to good servers and assign a small number of requests to poor servers, you can use weight to control them.

Configuration:

Upstream myapp {server 192.168.20.1: 8080 weight = 3; # This server processes 3/4 requests server 192.168.20.2: 8080; # weight is 1 by default, and this server processes 1/4 requests} server {listen 80; location/{proxy_pass http: // myapp ;}}
4. VM

Some websites have a high traffic volume and need Server Load balancer. However, not all websites are so good. Some websites deploy multiple websites on the same server because their access volume is too small and they need to save costs.

For example, if you deploy two websites www.aaa.com and www.bbb.com on the same server and resolve the two domain names to the same IP address, but you can open two completely different websites through the two domain names without affecting each other, just like accessing two servers, it is called two virtual hosts.

Configuration:

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

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

The principle of a VM is implemented by checking whether the Host in the HTTP Request Header matches server_name. If you are interested, you can study the HTTP protocol.

In addition, the server_name configuration can also filter malicious domain names that point to your host server.

5. FastCGI

Nginx does not support PHP or other languages, but it can use FastCGI to throw requests to some languages or frameworks for processing (such as PHP, Python, and Perl ).

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 method: fastcgi_pass unix: /var/run/php5-fpm.sock ;}}

Configure to send requests ending with. php to the PHP-FPM through FashCGI, A FastCGI manager for PHP. For more information about FashCGI, see this document.

What is the difference between fastcgi_pass and proxy_pass? The following figure shows you:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.