In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article shows you how to optimize the high-performance configuration of Nginx, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Nginx is a good alternative to Apache services. It is characterized by less memory and strong concurrency ability. in fact, the concurrency ability of nginx performs better in the same type of web servers, so domestic well-known companies such as Taobao, JD.com, Baidu, Sina, NetEase, Tencent and so on are all using Nginx websites.
In our daily work and study, how should we optimize our Nginx server? How should we deal with the following problems?
First, how to customize the 404 error page returned to the client
1) before optimization, the client uses a browser to visit a page that does not exist and will prompt that the 404 file cannot be found
[root@client ~] # firefox http://192.168.4.5/xxxxx / / visit a page that does not exist
2) modify the Nginx configuration file and customize the error page
[root@proxy] # vim / usr/local/nginx/conf/nginx.conf. .. Charset utf-8; / / modify this option error_page 404 / 404.html only if you need Chinese; / / customize the error page. .. [root@proxy ~] # vim / usr/local/nginx/html/404.html / / generate error page Oops,No NO no page... [root@proxy ~] # nginx-s reload # Please make sure that nginx is started first, otherwise running the command will cause an error message as follows: # [error] open () "/ usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
3) after optimization, the client uses a browser to visit a page that does not exist and will prompt the 40x.html page defined by itself
[root@client ~] # firefox http://192.168.4.5/xxxxx / / visit a page that does not exist
Common http status codes can be shown in the reference table
Second, how to view server status information (a very important function)
1) use-- with-http_stub_status_module to open the status page module when compiling and installing
[root@proxy ~] # tar-zxvf nginx-1.12.2.tar.gz [root@proxy ~] # cd nginx-1.12.2 [root@proxy nginx-1.12.2] # / configure\ >-- with-http_ssl_module / / enable SSL encryption >-- with-stream / / enable TCP/ UDP Agent Module >-- with-http_stub_status_module / / Open the status status page [root@proxy nginx-1.12.2] # make & & make install / / compile and install
2) enable Nginx service and check the status of the listening port
The ss command can view the port information started on the system, and the common options for this command are as follows:
-a displays information for all ports
-n displays the port number in numeric format
-t displays the port of the TCP connection
-u shows the port to which UDP is connected
-l displays the port information that the service is listening to. For example, port 80 will be monitored all the time after httpd is started.
-p shows what the service name of the listening port is (that is, the program name)
Note: on RHEL7 systems, you can use the ss command instead of the netstat command with the same functions and options.
[root@proxy ~] # / usr/local/nginx/sbin/nginx [root@proxy ~] # netstat-anptu | grep nginx tcp 00 0.0.0.0 root@proxy 80 0.0.0.0 anptu * LISTEN 10441/nginx [root@proxy ~] # ss-anptu | grep nginx
3) modify the Nginx configuration file and define the status page
[root@proxy] # cat / usr/local/nginx/conf/nginx.conf... ... Location / status {stub_status on; # allow IP address; # deny IP address;}... ... [root@proxy] # / usr/local/nginx/sbin/nginx-s reload
4) after optimization, view the status page information
[root@proxy ~] # curl http://192.168.4.5/status Active connections: 1 server accepts handled requests 10 10 3 Reading: 0 Writing: 1 Waiting: 0
Active connections: the number of connections currently active.
Accepts: the total number of connections accepted by the client.
Handled: the total number of connections processed by the client. (generally consistent with accepts, unless the server limits the number of connections).
Requests: the number of requests sent by the client.
Reading: the current server is reading the number of client request headers.
Writing: the number of responses being written by the current server.
Waiting: how many clients are currently waiting for a response from the server.
Third, optimize Nginx concurrency
1) use ab for high concurrency testing before optimization
[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 to open too many files
2) modify Nginx configuration file to increase concurrency
[root@proxy] # vim / usr/local/nginx/conf/nginx.conf. .. Worker_processes 2; / / consistent with the number of CPU cores events {worker_connections 65535; / / maximum number of concurrent connections per worker}. .. [root@proxy] # / usr/local/nginx/sbin/nginx-s reload
3) optimize Linux kernel parameters (maximum number of files)
[root@proxy ~] # ulimit-a / / View all attribute values [root@proxy ~] # ulimit-Hn 100000 / / set hard limits (interim rules) [root@proxy ~] # ulimit-Sn 100000 / / set soft limits (interim rules) [root@proxy ~] # vim / etc/security/limits.conf.. .. * soft nofile 100000 * hard nofile 100000 # the profile is divided into four columns as follows: # the value of the item limit for which the user or group hard limit or soft limit needs to be limited
4) Test the concurrency of the server after optimization (tested in proxy because the client does not adjust the kernel parameters)
[ot@proroxy] # ab-n 2000-c 2000 http://192.168.4.5/
Fourth, optimize Nginx packet header cache
1) before optimization, use a script to test whether a long header request can get a response
[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 loops, a long URL address bar [root@proxy ~] #. / buffer.sh. .. 414 Request-URI Too Large / / indicates that the header information is too large
2) modify the Nginx configuration file to increase the cache size of the packet header
[root@proxy] # vim / usr/local/nginx/conf/nginx.conf. .. Http {client_header_buffer_size 1k; / / default request header information cache large_client_header_buffers 44k; / / cache number and capacity of large request packet header information. .. } [root@proxy ~] # / usr/local/nginx/sbin/nginx-s reload
3) after optimization, use the script to test whether the long header request can get a response
[root@proxy ~] # cat 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 [root@proxy ~] #. / buffer.sh
Fifth, the browser locally caches static data
1) use a Firefox browser to view the cache
Take the Firefox browser as an example, entering about:cache in the Firefox address bar will display the cache information of the Firefox browser, as shown in the figure. Click List Cache Entries to view the details.
2) clear the firefox local cache data, as shown in the figure.
3) change the Nginx configuration file to define the cache time for static pages
[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 as 30 days} [root@proxy ~] # cp / usr/share/backgrounds/day.jpg / usr/local/nginx/html [root@proxy ~] # / usr/local/nginx/sbin/nginx-s reload # Please make sure that nginx is started first Otherwise, running the command will result in an error message as follows: # [error] open () "/ usr/local/nginx/logs/nginx.pid" failed (2: No such file or directory)
4) after optimization, use Firefox browser to access the image and view the cache information again
[root@client ~] # firefox http://192.168.4.5/day.jpg
Enter about:cache in the firefox address bar to view the locally cached data to see if there are any pictures and whether the expiration time is correct.
The above content is how to optimize the high-performance configuration of Nginx. Have you learned the knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.