Home  >  Article  >  Backend Development  >  nginx configuration from scratch

nginx configuration from scratch

WBOY
WBOYOriginal
2016-08-08 09:27:381045browse

Reprinted from: http://oilbeater.com/nginx/2014/12/29/nginx-conf-from-zero.html

Basic concepts

The most common use of Nginx is to provide reverse proxy services, so what is reverse? What about agents? I believe that many mainland compatriots have used forward proxy in this magical land. The principle is roughly as follows:

The proxy server acts as an intermediary on the client side to accept requests, hide the real customer, and obtain the request from the server. resource. If the proxy server is outside the Great Wall, it can also help us achieve the purpose of crossing the Great Wall. As the name suggests, a reverse proxy is to use the proxy server as an intermediary for the server, hiding the server that actually provides services. The principle is roughly as follows:

Of course this is not to achieve the purpose of crossing the Great Wall, but to achieve security and load balancing and a series of functions. The so-called security means that the client's request will not fall directly to the intranet server but will be forwarded through a proxy. At this layer, a series of strategies such as security filtering, flow control, and DDOS prevention can be implemented. Load balancing means that we can horizontally expand the number of servers that actually provide services on the backend. The agent forwards requests to each server according to rules, so that the load of each server is close to balanced.

And nginx is such a popular reverse proxy service.

Installation

Under Ubuntu, you can skip the compilation and installation process and directly apt-get

<code>sudo apt-get install nginx
</code>

After installation, you can directly start the nginx service through:

<code>sudo service nginx start
</code>

. nginx is set to port 80 by default. Forwarding, we can visit http://locallhost in the browser to check.

Initial configuration

nginx’s default configuration file is located at

<code>/etc/nginx/nginx.conf
</code>

The best way to learn configuration is to start with examples. Let’s not look at other configurations first, but directly look at the configuration related to the nginx default page. There is a line in the configuration file:

<code>include /etc/nginx/sites-enabled/*;
</code>

This line loads an external configuration file. There is only one default file in the sites-enabled folder. This external configuration file is responsible for the default proxy of our nginx. After shrinking the configuration content, we get the following lines:

<code>server {
    server_name localhost;
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    location / {
    try_files $uri $uri/ =404;
    }
}
</code>

A large website usually has many subordinate sites, with their own servers providing corresponding services. In nginx we can use a concept called virtual host To isolate these different service configurations, this is the meaning of server in the above configuration. For example, Google has two products: translation and academic. We can configure two servers in the nginx configuration file. The server names are translate.google.com and scholar.google.com respectively. In this case, different URL requests will be It will correspond to the corresponding settings of nginx and forwarded to different back-end servers. here servername is matched with the host line in the client http request.

In this example, server_name is localhost, which is why we can access the page configuration through localhost in the browser. The following two listens correspond to the listening ports under ipv4 and ipv6 respectively. If set to 8080, then we can only access the default page through localhost:8080.

default_server means that if there are other http request host settings that do not exist in nginx, then the configuration of this server will be used. For example, if we access 127.0.0.1, it will also fall to this server for processing.

Each url request will correspond to a service, which nginx will process and forward, either a local file path, or a service path of other servers. The matching of this path is performed through location. We can think of server as the configuration corresponding to a domain name, and location as the configuration of a more granular path under a domain name.

All requests starting with location matching /, that is, /xxx or /yyy under localhost, must go through the following configuration. In addition to this simple and crude matching, nginx also supports regular, exact equality and other fine matching methods. And try

files means that nginx will access the files in the following order and return the first matching one. For example, if you request localhost/test, it will look for the /test file. If it cannot find it, it will look for /test/. If it cannot find it, it will return a 404. In addition, we can also use proxypass in the location configuration Implement reverse proxy and load balancing, but this simplest configuration is not involved.

where root refers to using a local folder as the root path for all url requests. For example, if the user requests a localhost/test, nginx will look for the test file in the /usr/share/nginx/html folder and return it.

And index is the default access page. When we access localhost, it will automatically search for index.html and index.htm in the root file path in order and return the first found result.

location advanced configuration

上面的配置只是将用户的 url 映射到本地的文件,并没有实现传说中的反向代理和负载均衡(当然 nginx 做静态文件的分发也是想到的厉害),下面我们就来进一步配置 location 看看怎么实现。

配置起来很简单比如我要将所有的请求到转移到真正提供服务的一台机器的 8080 端口,只要这样:

<code>location / {
    proxy_pass 123.34.56.67:8080;
}
</code>

这样所有的请求就都被反向代理到 123.34.56.67 去了。这样我们反向代理的功能是实现了,可是就能代理到一台服务器上哪有什么负载均衡呀?这就要用到 nginx 的 upstream 模块了。

<code>upstream backend {
    ip_hash;    
    server backend1.example.com;
    server backend2.example.com;
    server backend3.example.com;
    server backend4.example.com;
}
location / {
    proxy_pass http://backend;
}
</code>

我们在 upstream 中指定了一组机器,并将这个组命名为 backend,这样在 proxypass 中只要将请求转移到 backend 这个 upstream 中我们就实现了在四台机器的反向代理加负载均衡。其中的 iphash 指明了我们均衡的方式是按照用户的 ip 地址进行分配。

要让配置生效,我们不必重启 nginx 只需要 reload 配置即可。

<code>sudo service nginx reload
</code>

总结

以上是最简单的通过 nginx 实现静态文件转发、反向代理和负载均衡的配置。在 nginx 中所有的功能都是通过模块来实现的,比如当我们配置 upstream 时是对 upstream 模块,而 server 和 location 是在 http core 模块,其他的还有流控的 limt 模块,邮件的 mail 模块,https 的 ssl 模块。他们的配置都是类似的可以再 nginx 的模块文档中找到详细的配置说明。


以上就介绍了nginx 配置从零开始,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
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
Previous article:php(1)-Basic syntaxNext article:php(1)-Basic syntax