The secret of billion-level traffic---Nginx (1)

installation (abbreviated)

# 第一步:进入/opt目录
cd /opt

# 第二步:解压到当前目录
tar -zxvf /opt/nginx-1.12.2.tar.gz

# 第三步:进入解压目录
cd /opt/nginx-1.12.2

# 第四步:生成Makefile文件
./configure

# 第五步:编译
make

# 第六步:安装
make install

# 第七步:验证
/usr/local/nginx/sbin/nginx -v

After the installation is complete, you can go to /usr/local/nginx to view the Nginx installation directory:
insert image description here

Start Access Home

Start the Nginx server

/usr/local/nginx/sbin/nginx

Access Nginx server

Nginx server listens on port 80 by default
insert image description here

Stop Nginx server

/usr/local/nginx/sbin/nginx -s stop

reload config file

/usr/local/nginx/sbin/nginx -s reload

Nginx configuration file

general description

Nginx contains many configuration files, but the main configuration file is: /usr/local/nginx/conf/nginx.conf. After removing all comments, the main structure of the configuration file is:

worker_processes  1;

events {
    
    
    worker_connections  1024;
}

http {
    
    
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
    
    
        listen       80;
        server_name  localhost;
        location / {
    
    
            root   html;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }
    }
}

global block

From the beginning of the configuration file to the contents of the events block, some configuration directives that affect the overall operation of the Nginx server are mainly set. It mainly includes configuring the user (group) running the Nginx server, the number of worker processes allowed to be generated, the storage path of the process PID file, the storage path and type of the log file, and the introduction of the configuration file. For example, the first line of configuration above:

worker_processes  1;

This is the key configuration of the Nginx server concurrent processing service. The larger the worker_processes value is, the more concurrent processing it can support, but it will be restricted by hardware, software and other equipment.

Configuration example:

#user  nobody;
worker_processes  1;

# 错误日志文件保存的位置以及日志的级别
#error_log  logs/error.log;
error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

# PID 文件保存的位置
pid        logs/nginx.pid;

events block

The instructions involved in the events block mainly affect the network connection between the Nginx server and the user. Commonly used settings include whether to enable serialization of network connections under multiple work processes, whether to allow multiple network connections to be received at the same time, and which event-driven model to choose for processing. Connection requests, the maximum number of connections that each work process can support at the same time, etc. This part of the configuration has a great impact on the performance of Nginx, and should be flexibly configured in practice.

events {
    
    
    worker_connections  1024;
}

The above example shows that the maximum number of connections supported by each work process is 1024.

The number of concurrent requests that the current Nginx server can handle = the maximum number of connections each worker process can handle × the number of worker processes

http block

The most frequent part of Nginx server configuration, most functions such as proxy, cache and log definitions and configuration of third-party modules are here.

http global block

The directives of http global block configuration include file import, MIME-TYPE definition, log customization, connection timeout time, upper limit of single-link requests, etc.

server block

web hosting

A virtual host literally means a virtual host. The host here refers to the website server. For small and micro websites, it is not necessary to rent the entire host considering the cost, so the virtual host provides the basic configuration for running a small website, which can be used to directly deploy and run a small website (PHP website or static website). From the user's point of view, the virtual host is exactly the same as an independent hardware host. This technology is produced to save the cost of Internet server hardware.

The server block is mainly responsible for configuring virtual hosts. Each http block can contain multiple server blocks, and each server block is equivalent to a virtual host.

Each server block is further divided into "global block part" and "location block" part.

The server global block
configuration example is as follows:

server {
    
    
	# 本虚拟主机监听的端口号
    listen       80;
    
    # 本虚拟主机的名称
    server_name  localhost;

	# 本虚拟主机的字符集设置
    #charset koi8-r;

	# 本虚拟主机专属的访问日志设置
    #access_log  logs/host.access.log  main;

location block
A server block can be configured with multiple location blocks. The main function of this block is based on the request string (such as server_name/uri-string) received by the Nginx server, to the string (such as the previous /uri-string) other than the virtual host name (or IP alias) Match to process a specific request. Functions such as address orientation, data caching, and response control, as well as the configuration of many third-party modules, are also performed here.

insert image description here

summary

Configuration file structure:

  • global block
  • events block
  • http block
    • http global block
    • server block (there can be more than one)
      • server global block
      • location block (there can be more than one)

reverse proxy

concept

forward proxy

proxy client
insert image description here

reverse proxy

proxy server
insert image description here

operate

Simple configuration

Let Nginx act as a proxy for Tomcat, that is, access Tomcat through Nginx.

Start Tomcat
In Linux systems, Tomcat's tar package can be decompressed to start. But the premise is that the JDK must be properly configured. The required configuration of JDK is as follows:

# 在/etc/profile文件末尾加入如下配置:
# 声明 JAVA_HOME 环境变量,等号两边不能有空格
JAVA_HOME=/opt/jdk1.8.0_121

# 给 PATH 环境变量附加 $JAVA_HOME/bin 部分
# $PATH 表示引用 PATH 环境变量以前的旧值
# 使用英文冒号连接
# $JAVA_HOME 表示对 JAVA_HOME 变量的引用
# $JAVA_HOME/bin 表示指向可执行文件
PATH=$JAVA_HOME/bin:$PATH

# 发布
# 发布是发布变量名,不是引用变量的值,所以前面不写 $ 符号
export JAVA_HOME PATH

Command to start Tomcat:

/opt/apache-tomcat-7.0.75/bin/startup.sh

Access via browser:
insert image description here
Configure Nginx

# 实验一:配置简单的反向代理
server {
    
    
    # 监听的端口号
    listen 1111;
    
    # server_name 不仅仅是当前虚拟主机的名称,而且是要和请求地址的IP地址部分进行匹配
    # localhost 和 127.0.0.1 都代表本机;也可以写域名。
    server_name www.atguigu.shuai;

    # 配置 location 部分
    # / 代表域名(或IP地址):端口号之后服务器的根目录
    location / {
    
    
        # proxy是代理的意思;pass是传送的意思;合起来,proxy_pass是请求转发的意思。
        # 现在这个具体配置的意思就是 Nginx 接收到请求后,将这个请求转发给 Linux 本机的 Tomcat 服务器。
        proxy_pass http://localhost:8080;
        
        # index 配置默认的欢迎页
        index index.jsp index.html index.htm;
    }
}

Configuring Domain Name Mapping
Originally, the DNS server should implement the mapping from domain names to IP addresses. It is too troublesome for us to build our own DNS server, so we directly configure it locally on Windows.

This feature should have been configured through the C:\Windows\System32\drivers\etc file. However, the directory where this file is located is very deep and requires administrator rights, so the operation is very inconvenient. So it is done with a tool software: SwitchHosts.exe
insert image description here
insert image description here
advanced configuration
of the further syntax of the location block:

location [ = | ~ | ~* | ^~ ] uri {
    
    

}

Guess you like

Origin blog.csdn.net/GBS20200720/article/details/124059484