Nginx Basic Introductory __nginx

Source: Internet
Author: User
Tags fpm
1. Static HTTP Server

First, Nginx is an HTTP server that can present static files (such as HTML, pictures) on the server to the client via the HTTP protocol.

Configuration:

1
2
3
4
5
6
server {
Listen 80; # port number
Location/{
root/usr/share/nginx/html; # Static file path
}
}

2. Reverse Proxy Server

What is a reverse proxy.

The client can access a Web application server directly through the HTTP protocol, and if the webmaster adds a nginx in the middle, the client requests Nginx,nginx to request the application server and returns the result to the client, at which point Nginx is the reverse proxy server.
Reverse Proxy
Configuration:

1
2
3
4
5
6
server {
Listen 80;
Location/{
Proxy_pass http://192.168.0.112:8080; # Application Server HTTP Address
}
}

Since the server can directly HTTP access, why to add a reverse proxy in the middle, is not superfluous. What is the effect of the reverse proxy. Continue to look down, the following load balancing, virtual host, are based on reverse proxy implementation, of course, the reverse proxy function is not only these. 3. Load Balance

When the site is very large, it is also on the stand. Because the website is getting slower, a server is not enough. The same application is deployed on multiple servers, and a large number of user requests are allocated to multiple machines for processing. At the same time, the benefit is that one of the servers in case of hanging, as long as there are other servers running normally, it will not affect the user use.

Nginx can achieve load balancing by reverse proxy.
Load Balancing
Configuration:

1
2
3
4
5
6
7
8
9
10
Upstream MyApp {
Server 192.168.0.111:8080; # Application Server 1
Server 192.168.0.112:8080; # Application Server 2
}
server {
Listen 80;
Location/{
Proxy_pass Http://myweb;
}
}

4. Virtual Host

A large amount of web site traffic, the need for load balancing. However, not all sites are so good, some sites, due to the amount of traffic is too small, need to save costs, to deploy multiple sites on the same server.

For example, the www.aaa.com and www.bbb.com two Web sites deployed on the same server, two domain names resolved to the same IP address, but the user through two domain name can open two completely different sites, do not affect each other, like access to two servers, so called two virtual host.

Configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
Listen default_server;
server_name _;
return 444; # filter Other domain's request, return 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
}
}

In the server 8080 and 8081 opened an application, the client through a different domain name access, according to server_name can reverse proxy to the corresponding application server.

The principle of the virtual host is through the HTTP request header to match server_name to achieve, interested students can study the HTTP protocol.

In addition, the server_name configuration can also filter some of the domain names malicious to point to your host server. 5, FastCGI

Nginx itself does not support languages such as PHP, but it can be fastcgi to throw requests to certain language or framework processing (such as PHP, Python, Perl).

1
2
3
4
5
6
7
8
9
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 request for end of. PHP is passed through fashcgi to PHP-FPM processing, and PHP-FPM is a fastcgi manager in PHP. Other information about fashcgi can be consulted, which is not covered in this article.

What's the difference between Fastcgi_pass and Proxy_pass? Here is a picture that you can see:

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.