Six methods for nginx optimization

Six methods for nginx optimization

1. Optimize Nginx concurrency

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/
Benchmarking 192.168.4.5 (be patient)
socket: Too many open files (24) //Prompt that there are too many open files

Modify the Nginx configuration file to increase concurrency

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
worker_processes 2; //Same as the number of CPU cores events {
worker_connections 65535; //The maximum number of concurrent connections per worker use epoll;
}
.. ..
[root@proxy ~]# nginx -s reload

2. Optimize Linux kernel parameters (maximum number of files)

[root@proxy ~]# ulimit -a //View all attribute values ​​[root@proxy ~]# ulimit -Hn 100000 //Set hard limit (temporary rule)
[root@proxy ~]# ulimit -Sn 100000 //Set soft limit (temporary rule)
[root@proxy ~]# vim /etc/security/limits.conf
 .. ..
* soft nofile 100000
* hard nofile 100000
#The configuration file is divided into 4 columns, as follows:
#User or group hard limit or soft limit The value of the item limit that needs to be restricted

Test server concurrency after optimization

[root@proxy ~]# ab -n 2000 -c 2000 http://192.168.4.5/

3. Optimize Nginx packet header cache

[root@proxy ~]# cat lnmp_soft/buffer.sh 
#!/bin/bash
URL=http://192.168.4.5/index.html?
for i in {1..5000}
do
 URL=${URL}v$i=$i
done
curl $URL //After 5000 cycles, a long URL address bar is generated [root@proxy ~]# ./buffer.sh
.. ..
<center><h1>414 Request-URI Too Large</h1></center> //Prompt that the header information is too large

Modify the Nginx configuration file to increase the packet header cache size

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
client_header_buffer_size 1k; //Default request header information cache large_client_header_buffers 4 4k; //Number and capacity of caches for large request header information.. ..
}
[root@proxy ~]# nginx -s reload

4. Compress the page

[root@proxy ~]# cat /usr/local/nginx/conf/nginx.conf
http {
.. ..
gzip on; //Turn on compression gzip_min_length 1000; //Small files are not compressed gzip_comp_level 4; //Compression ratio gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
         //Compress specific files, refer to mime.types for types
.. ..

5. Server Memory Cache

http { 
open_file_cache max=2000 inactive=20s;
  open_file_cache_valid 60s;
  open_file_cache_min_uses 5;
  open_file_cache_errors off;
//Set the server to cache a maximum of 2000 file handles, and close file handles that have no requests within 20 seconds. //The validity period of a file handle is 60 seconds, and it expires after 60 seconds. //Only files accessed more than 5 times will be cached.}

6. Browser local cache static data

[root@proxy ~]# vim /usr/local/nginx/conf/nginx.conf
server {
  listen 80;
  server_name localhost;
  location / {
   root html;
   index index.html index.htm;
  }
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 30d; //Define the client cache time to 30 days}
}
[root@proxy ~]# cp /usr/share/backgrounds/day.jpg /usr/local/nginx/html
[root@proxy ~]# nginx -s reload 

This concludes this article on the six methods of nginx optimization. For more relevant nginx optimization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • nginx basic tutorial
  • Nginx Configuration Getting Started Tutorial
  • Getting Started Tutorial on nginx HTTP Server under Windows
  • What is Nginx load balancing and how to configure it
  • How to implement web page compression in Nginx optimization service
  • Detailed explanation of how Nginx solves the problem of cross-domain access to front-end resources
  • Solve the problem of Nginx returning 404 after configuring proxy_pass
  • Limiting the number of short-term accesses to a certain IP based on Nginx
  • Nginx configuration and compatibility with HTTP implementation code analysis
  • Nginx Service Quick Start Tutorial

<<:  Robots.txt detailed introduction

>>:  Summary of 4 methods of div+css layout to achieve 2-end alignment of css

Recommend

Color matching techniques and effect display for beauty and styling websites

Color is one of the most important elements for a...

How to copy MySQL table

Table of contents 1.mysqldump Execution process: ...

Modify the default scroll bar style in the front-end project (summary)

I have written many projects that require changin...

How to install and configure Docker nginx

Download Nginx image in Docker docker pull nginx ...

A detailed introduction to the Linux directory structure

When you first start learning Linux, you first ne...

An Incomplete Guide to JavaScript Toolchain

Table of contents Overview Static type checking C...

B2C website user experience detail design reference

Recently, when using Apple.com/Ebay.com/Amazon.co...

JavaScript implements countdown on front-end web page

Use native JavaScript to simply implement the cou...

Javascript basics about built-in objects

Table of contents 1. Introduction to built-in obj...

How to install mysql6 initialization installation password under centos7

1. Stop the database server first service mysqld ...

Common problems in implementing the progress bar function of vue Nprogress

NProgress is the progress bar that appears at the...

How to change the dot in the WeChat applet swiper-dot into a slider

Table of contents background Target Effect Ideas ...

Markup Language - Phrase Elements

Click here to return to the 123WORDPRESS.COM HTML ...

How to change the domestic source of Ubuntu 20.04 apt

UPD 2020.2.26 Currently Ubuntu 20.04 LTS has not ...