Nginx minimalist introductory tutorial!

Last article we talked for a Spring Session and Session achieve a shared problem, some small partner after seeing told Nginx is still very ignorant, and therefore with this article, be regarded as a Nginx entry literacy it!

<!--more-->

basic introduction

NginxIt is a high-performance HTTPand reverse proxy webserver, but also provides a IMAP/POP3/SMTPservice.

NginxBy Igor Saisuoyefu Russia visited the second Rambler.rusite development, the first public version 0.1.0released in 2004on 10May 4Day.

Nginx It characterized by possession of less memory, high concurrency.

In fact nginxconcurrent ability is definitely in the same type of web server performance is better, generally speaking, if we introduce in the project Nginx, our project architecture might look like this:

Nginx minimalist introductory tutorial!

In this architecture, Nginxthe role of the representatives called load balancing server or reverse proxy server, all requests arrive first Nginxon, and then by Nginxone forwarded to advance according to the configured forwarding rules, client requests sent Tomcatup.

This involves two concepts:

  • Server load balancing

It is carried forward the request to reduce the pressure of a particular server. Many load balancing strategy, there are a lot of layers, for some large sites basically from DNSstart load balancing, load balancing hardware and software it has divided, each representing respectively F5and Nginx(currently Nginxhas been F5acquired), the early years, it can also be used Apacheto do load balancing, but the efficiency is better Nginx, so now the mainstream scheme Nginx.

  • Reverse proxy server:

Another concept is a reverse proxy server, you must first say forward proxy, a look at the following chart:

Nginx minimalist introductory tutorial!

在这个过程中,Google 并不知道真正访问它的客户端是谁,它只知道这个中间服务器在访问它。因此,这里的代理,实际上是中间服务器代理了客户端,这种代理叫做正向代理。

那么什么是反向代理呢?看下面一张图:

Nginx minimalist introductory tutorial!

在这个过程中,10086 这个号码相当于是一个代理,真正提供服务的,是话务员,但是对于客户来说,他不关心到底是哪一个话务员提供的服务,他只需要记得 10086 这个号码就行了。

所有的请求打到 10086 上,再由 10086 将请求转发给某一个话务员去处理。因此,在这里,10086 就相当于是一个代理,只不过它代理的是话务员而不是客户端,这种代理称之为反向代理。

Nginx 的优势

在 Java 开发中,Nginx 有着非常广泛的使用,随便举几点:

  1. 使用 Nginx 做静态资源服务器:Java 中的资源可以分为动态和静态,动态需要经过 Tomcat 解析之后,才能返回给浏览器,例如 JSP 页面、Freemarker 页面、控制器返回的 JSON 数据等,都算作动态资源,动态资源经过了 Tomcat 处理,速度必然降低。对于静态资源,例如图片、HTML、JS、CSS 等资源,这种资源可以不必经过 Tomcat 解析,当客户端请求这些资源时,之间将资源返回给客户端就行了。此时,可以使用 Nginx 搭建静态资源服务器,将静态资源直接返回给客户端。
  2. 使用 Nginx 做负载均衡服务器,无论是使用 Dubbo 还是 Spirng Cloud ,除了使用各自自带的负载均衡策略之外,也都可以使用 Nginx 做负载均衡服务器。
  3. 支持高并发、内存消耗少、成本低廉、配置简单、运行稳定等。

Nginx 安装:

由于基本上都是在 Linux 上使用 Nginx,因此松哥这里主要向大家展示 CentOS 7 安装 Nginx:

  1. 首先下载 Nginx
wget http://nginx.org/download/nginx-1.17.0.tar.gz

然后解压下载的目录,进入解压目录中,在编译安装之前,需要安装两个依赖:

yum -y install pcre-devel
yum -y install openssl openssl-devel

然后开始编译安装:

./configure
make
make install

装好之后,默认安装位置在 :

/usr/local/nginx/sbin/nginx

Into the directory sbindirectory, execute nginxto start Nginx:

Nginx minimalist introductory tutorial!

After a successful start Nginx, visit Nginx address directly in the browser:

Nginx minimalist introductory tutorial!

See above page indicates Nginx been installed successfully.

If you modify the Nginx configuration, you can reload Nginx configuration file with the following command:

./nginx -s reload

to sum up

This article can be considered a simple text Nginx literacy, we hope that after reading a basic understanding of Nginx. This article first said that, there are questions please leave a message discussion.

Public concern number code kid, animal husbandry, micro-focus on Spring Boot + service, regular video tutorials to share, after concerns reply Java, Java dry Song Ge receive carefully prepared for you!

Nginx minimalist introductory tutorial!

Guess you like

Origin blog.51cto.com/9806927/2405076