Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed explanation of Nginx Core configuration

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

3.7Custom error page when we visit a page that does not exist, the browser page usually reports a large 404 Not Found, which obviously looks very unfriendly, so we can define the 404 error as the content written by ourselves [root@CentOS7-01 ~] # cat / apps/nginx/conf/vhosts/pc.conf server {listen 80; server_name pc.hechunping.tech. Error_page 404 / error.html; location = / error.html {root html;}} [root@CentOS7-01 ~] # echo "the page you visited is missing ~ ~" > / apps/nginx/html/error.html [root@CentOS7-01 ~] # systemctl reload nginx access Test

3.8 Custom access logs by default, logs are stored in the logs directory of the nginx installation path, but we can peel off the logs of each business, which makes it easy to count and manage [root@CentOS7-01 ~] # cat / apps/nginx/conf/vhosts/pc.conf server {listen 80; server_name pc.hechunping.tech; error_page 404 / error.html; access_log / data/nginx/logs/pc.hechunping.tech/access.log Error_log / data/nginx/logs/pc.hechunping.tech/error.log; location = / error.html {root html }} [root@CentOS7-01 ~] # systemctl reload nginx [root@CentOS7-01 ~] # ls / data/nginx/logs/pc.hechunping.tech/access.log error.log3.9 detects the existence of files. Try_files checks the existence of missing pieces in order, and returns the first found piece or folder (ending with "/" table "). If none of the pieces or folders can be found, it will be internally redirected to the last parameter. Only the last parameter can cause an internal redirection, and the previous parameter only sets the direction of the internal URI. The last parameter is a fallback URI and must exist, otherwise an internal 500 error will occur. [root@CentOS7-01 ~] # cat / apps/nginx/conf/vhosts/pc.conf server {listen 80; server_name pc.hechunping.tech; location / pc {root html; try_files $uri $uri/index.html $uri.html / pc/defautl.html }} [root@CentOS7-01 ~] # cat / apps/nginx/html/pc/defautl.html pc default page. [root@CentOS7-01 ~] # systemctl reload nginx access test displays the content of the defautl.html page when the accessed resource does not match all the previous uri, as shown in the following figure

You can also customize the last parameter to a status code, as shown in the following figure

3.10 persistent connection matching keepalive_timeout number; # sets keep connection timeout, 0 table "disable" connection, the default is 75s, usually configured in the http field as the site global configuration keepalive_requests number; # the maximum number of resources allowed to be requested on the "secondary" connection, default is 100th keepalive_timeout 6565 # after opening the connection, the session persistence time returned to the client is 65s, and the cumulative request for a single connection will be disconnected when it reaches the specified number of requests or 65 seconds. The 65 of the latter indicates that the timeout in the header of the response message sent to the client is set to 65s: if the client is not set, the timeout will not appear. Access test [root@CentOS7-01 ~] # curl-I pc.hechunping.tech/pcHTTP/1.1 200 OKServer: nginxDate: Sat, 04 Jan 2020 07:43:31 GMTContent-Type: text/html Charset=utf-8Content-Length: 7Last-Modified: Thu, 02 Jan 2020 11:32:27 GMTConnection: keep-aliveKeep-Alive: timeout=65ETag: "5e0dd4cb-7" Accept-Ranges: bytes if set to 0 table to turn off session persistence, as follows: curl-I pc.hechunping.tech/pcHTTP/1.1 2020 OKServer: nginxDate: Sat, 04 Jan 2020 07:45:23 GMTContent-Type: text/html Charset=utf-8Content-Length: 7Last-Modified: Thu, 02 Jan 2020 11:32:27 GMTConnection: closeETag: "5e0dd4cb-7" Accept-Ranges: byteskeepalive_requests 2; # the maximum number of resources allowed to be requested on a persistent connection, which is set to 2. When it reaches two times, the persistent connection is closed. Access test # use telnet command to test [root@CentOS7-01 ~] # telnet pc.hechunping.tech 80Trying 127.0.0.1...Connected to pc.hechunping.tech.Escape character is'^] '.GET / pc/index.html HTTP/1.1HOST: pc.hechunping.tech# response header information HTTP/1.1 200 OKServer: nginxDate: Sat, 04 Jan 2020 07:48:50 GMTContent-Type: text/html Charset=utf-8Content-Length: 7Last-Modified: Thu, 02 Jan 2020 11:32:27 GMTConnection: keep-aliveKeep-Alive: timeout=65ETag: "5e0dd4cb-7" Accept-Ranges: bytes# page content pc webGET / pc/index.html HTTP/1.1HOST: pc.hechunping.tech# response header information HTTP/1.1 200 OKServer: nginxDate: Sat, 04 Jan 2020 07:49:01 GMTContent-Type: text/html Charset=utf-8Content-Length: 7Last-Modified: Thu, 02 Jan 2020 11:32:27 GMTConnection: closeETag: "5e0dd4cb-7" Accept-Ranges: bytes# page content pc webConnection closed by foreign host. # after 2 requests for resources, the persistent connection is closed this time. 3.11 configure nginx as the download server [root@CentOS7-01 download] # cat / apps/nginx/conf/vhosts/pc.conf server {listen 80; server_name pc.hechunping.tech; location / download {autoindex on; autoindex_exact_size on; autoindex_localtime on; limit_rate 20k; root html/pc }} [root@CentOS7-01 download] # ls / apps/nginx/html/pc/download/ # there is no index.html file README.md ubuntu-18.04.3-server-amd64.iso [root@CentOS7-01 download] # systemctl reload nginx access test in this directory, as shown in the following figure

3.12 configure nginx as the upload server client_max_body_size 1m; # set the minimum value that allows the client to upload a single piece. The default value is 1mclient_body_buffer_size size; # which is the buffer for receiving the body portion of each client request report; the default is 16k When this quota is exceeded, it will be temporarily saved to the location client_body_temp_path path [level1 [level2 [level3]] on disk defined by the client_body_temp_path directive in the following directory. # set the temporary storage path and recording structure and quantity of the body part of the request report of the storage client. The recording name is hexadecimal, so that the values after recording hash intercept 1 bits, 2 bits, 2 bits as "item name 1 level" record occupies 1 bit hexadecimal, that is, 2 ^ 4 = 16 "recording 0-f2 level" records occupy 2-bit hexadecimal, that is, 2 ^ 8 = 256 "recording 00-ff3 level" records occupy 2-bit hexadecimal. That is, 2 ^ 8 = 256 "recording 00-ff configuration" example: client_max_body_size 10m Client_body_buffer_size 16kclientcreating bodybuilding temptation path / data/nginx/temp 1 22; # reload Nginx will "dynamically create temp entries 3.13 other configurations 3.13.1 which browser forbids connecting to keepalive_disable none | browser...; for example: disable ie6 browser, add the following parameters in the configuration file, you can configure keepalive_disable msie6 in the http,server,location block. 3.13.2 restrict clients to use only the specified request method, only on the location block limit _ except method. {...}; the method:GET,HEAD,POST,PUT,DELETE,MKCOL,COPY,MOVE,OPTIONS,PROPFIND,PROPPATCH,LOCK,UNLOCK,PATCH# restriction only allows clients of the 192.168.7.0 network segment to use the specified GET and HEAD methods [root@CentOS7-01 ~] # cat / apps/nginx/conf/vhosts/pc.conf server {listen 80 Server_name pc.hechunping.tech; location / pc {root html; limit_except GET {allow 192.168.7.0 Universe 24; deny all } [root@CentOS7-01 ~] # systemctl reload nginx [root@CentOS7-01 ~] # curl-XPUT / etc/issue pc.hechunping.tech/pccurl: (3) malformed403 Forbidden # nginx refuses to upload 403 Forbiddennginx# comment out the configuration of the specified method [root@CentOS7-01 ~] # cat / apps/nginx/conf/vhosts/pc.conf server {listen 80; server_name pc.hechunping.tech; location / pc {root html # limit_except GET {# allow 192.168.7.0 Compact 24; # deny all #}} [root@CentOS7-01 ~] # systemctl reload nginx [root@CentOS7-01 ~] # curl-XPUT / etc/issue pc.hechunping.tech/pccurl: (3) malformed405 Not Allowed # nginx has been allowed, but the program does not support upload function. Does 405Not Allowednginx3.13.3 enable asynchronous file I AIO function? Need to compile and enable linux 2.6 or above kernel to provide the following system callers to support aio:1, SYS_io_setup: build aio's context2, SYS_io_submit: submit an Imax O operation request 3, SYS_io_getevents: get the completed iUnip O event 4, SYS_io_cancel: cancel the aio O operation request 5, SYS_io_destroy: destroy aio's context3.13.4 enable direct I/Odirectio size | off # the operation is exactly the opposite of aio. Aio reads "directio" and writes to disk. It turns on direct I write O, and defaults to off. When the value is equal to a given memory, for example, directio 4m, write disk synchronously (directly), write cache "write". 3.13.5 whether to cache open file information open_file_cache off; # whether to cache open component information open_file_cache max=N [inactive=time] Nginx can cache the following three kinds of information: 1) metadata: the descriptor of the item, the last modification time of the item, 2) the open record structure 3) the information about the item that is not found or does not have access max=N: the maximum number of cache items that can be cached When the upper limit is reached, the LRU (Least recently used) algorithm can manage inactive=time: the "active time" of the cache item. If the cache item is missed or the number of hits is less than the number specified by the open_file_cache_min_uses instruction within the time limit specified here, the cache item will be deleted within the time specified by the inactive parameter of the 3.13.6 open_file_cache instruction. Less hit the number of times specified here can be classified as the active item open_file_cache_min_uses number # whether the default value is 13.13.7 whether to cache the information of the "item" class that issued an error when looking up | off; # default value is off3.13.8 cache item validity check frequency open_file_cache_valid time; # default value is 60sopen_file_cache max=10000 inactive=60s; # maximum cache 10000 pieces, "60sopen _ file_cache_valid 60s when the active data times out # check the validity of cached data open_file_cache_min_uses 5 every 60 seconds; the cache is marked as active data open_file_cache_errors on; # Cache error message 3.13.9 Hidden Nginx server version only if it is accessed less than 5 times within 60 seconds. Server_tokens off; # defaults to the result of on access test default [root@CentOS7-01 ~] # curl-I pc.hechunping.techHTTP/1.1 200OKServer: nginx/1.16.1... As a result of setting it to off, add the global configuration [root@CentOS7-01 ~] # curl-I pc.hechunping.techHTTP/1.1 200OKServer: nginx... in the http block

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.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report