Nginx Study Notes

Nginx is a high-performance HTTP and reverse proxy server, known for its high stability, rich feature set, sample configuration files, and low system resource consumption.

Contents

Nginx

Common Versions

  • Nginx Open Source
  • Nginx Plus (commercial edition)
  • OpenResty
  • Tengine

Install NGINX (Open Source)

Official website https://nginx.org

Installation steps:

  • Install dependencies: gcc, pcre, pcre-devel, zlib, zlib-devel
  • ./configure --prefix=/usr/local/nginx
  • make
  • make install
  • Start Nginx: installation directory/sbin/nginx

Configuration File

By default, the nginx.conf file is located in the conf directory under the installation directory.

Minimal Nginx configuration

# 指定worker进程数  一般填写物理cpu内核数
# 在nginx中,启动后先由master进程解析配置文件,接收请求,由worker进程返回结果
work_processes	1;

events {
    worker_connections	1024;  # 一个worker进程可以同时建立多少个连接
}

http {
    # 引入mime.types  该文件处理请求头与文件类型的对应关系
    include	mime.types;  
    # 如果没有匹配到文件类型,默认设置二进制文件头,浏览器会提示下载
    default_type	application/octet-stream;
    # 开启数据0拷贝
    sendfile	on;
    # 请求保持时间
    keepalive_timeout	65;
    
    # 一个server代表一个主机,一个主机代表一个独立站点,每个主机互不干扰
    # 虚拟主机 vhost
    server {
        # 监听端口
        listen	80;
        # 主机名,域名
        server_name	localhost;
        # 路径 uri(域名之后)
        location / {
            # 资源根目录
            root	html;
            # 默认页
            index	index.html index.htm;
        }
        # 错误页
        error_page   500 502 503 504  /50x.html;
        # 文件位置
        location = /50x.html {
            root   html;
        }
    
    }
}

Zero-Copy is an I/O operation optimization technique that quickly and efficiently moves data from the file system to the network interface without copying it from kernel space to user space. It can significantly improve performance in protocols such as FTP or HTTP.

Virtual Host Configuration

server {
listen 80;
# You can use the wildcard * or the regular expression ~^[0,9]+\.reboots\.com$; matching proceeds from top to bottom.
server_name reboots.com;
}

These two parameters uniquely identify a virtual host. You cannot have two virtual hosts with the same port and domain name.

Practical use: Create two virtual hosts on port 80 and access them using different domain names

Simply write two server blocks that both listen on port 80, and set a different domain name for each.

If no match is found, the first site is selected based on the port.

Reverse Proxy and Load Balancing

To access through an Nginx proxy, configure it as follows:

location / {
proxy_pass  url;
}

Configure load balancing through an Nginx proxy. For servers with poor performance, you can reduce their load by assigning weights.

weight: Sets the traffic distribution ratio.

down: Takes the server offline; it will no longer receive traffic.

backup: Marks the server as a backup; it will receive traffic only when all other servers are down.

upsteam	urls{
    server 192.168.1.1:80 weight=1;
    server 192.168.1.2:80 weight=3;
    server 192.168.1.3:80 weight=3 down;
}

server{
    location / {
        proxy_pass  http://urls;
    }
    ...
}

Drawbacks of polling: unable to maintain sessions

Solution: don’t use sessions; use stateless tokens instead.

Other methods

  • ip_hash: route requests from the same IP address to the same service, but this cannot maintain sessions reliably today
  • least_conn: route requests to the server with the fewest connections
  • url_hash: route requests based on the URL the user visits
  • fair: route requests based on backend response times

Nginx Static and Dynamic Content Separation

For example, copy static files from Tomcat to Nginx. Nginx + 0-copy reduces unnecessary overhead.

UrlRewrite

rewrite	^/([0-9]+).html$	/index.jsp?pageNum=$1	break;

File Upload Issue

25.3.27
nginx defaults to a very small 1 MB limit. Requests exceeding this size return a 413 error, so special configuration is needed.

# 加到http中,http下的作用域都将生效
client_max_body_size 20m;